From 461a081208fa70ce4feddeef8ceb7a756d99c265 Mon Sep 17 00:00:00 2001 From: Daniele Esposti Date: Thu, 16 Feb 2023 20:40:20 +0000 Subject: [PATCH 1/2] Fixed parsing endpoint content type with semicolon separator --- openapi_python_client/parser/openapi.py | 4 +++- openapi_python_client/utils.py | 13 +++++++++++++ tests/test_parser/test_openapi.py | 8 +++++++- tests/test_utils.py | 13 +++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index b7c4a8142..e4af95a68 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -11,7 +11,7 @@ from .. import schema as oai from .. import utils from ..config import Config -from ..utils import PythonIdentifier +from ..utils import PythonIdentifier, get_content_type from .errors import GeneratorError, ParseError, PropertyError from .properties import ( Class, @@ -178,6 +178,8 @@ def parse_request_json_body( """Return json_body""" json_body = None for content_type, schema in body.content.items(): + content_type = get_content_type(content_type) + if content_type == "application/json" or content_type.endswith("+json"): json_body = schema break diff --git a/openapi_python_client/utils.py b/openapi_python_client/utils.py index c16237533..5e99628b5 100644 --- a/openapi_python_client/utils.py +++ b/openapi_python_client/utils.py @@ -1,5 +1,6 @@ import builtins import re +from email.message import Message from keyword import iskeyword from typing import Any, List @@ -94,3 +95,15 @@ def remove_string_escapes(value: str) -> str: - https://github.com/openapi-generators/openapi-python-client/security/advisories/GHSA-9x4c-63pf-525f """ return value.replace('"', r"\"") + + +def get_content_type(content_type: str) -> str: + """ + Given a string representing a conten type with optional parameters, returns the content type only + """ + message = Message() + message.add_header("Content-Type", content_type) + + content_type = message.get_content_type() + + return content_type diff --git a/tests/test_parser/test_openapi.py b/tests/test_parser/test_openapi.py index 9cc7398d9..8ef6b0a6a 100644 --- a/tests/test_parser/test_openapi.py +++ b/tests/test_parser/test_openapi.py @@ -247,7 +247,13 @@ def test_parse_multipart_body_no_data(self): assert prop is None @pytest.mark.parametrize( - "content_type", ("application/json", "application/vnd.api+json", "application/yang-data+json") + "content_type", + ( + "application/json", + "application/vnd.api+json", + "application/yang-data+json", + "application/json;charset=utf-8", + ), ) def test_parse_request_json_body(self, mocker, content_type): from openapi_python_client.parser.openapi import Endpoint, Schemas diff --git a/tests/test_utils.py b/tests/test_utils.py index 97f0bee2a..3cd213488 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -121,3 +121,16 @@ def test__fix_reserved_words(reserved_word: str, expected: str): ) def test_pascalcase(before, after): assert utils.pascal_case(before) == after + + +@pytest.mark.parametrize( + "content_type, expected", + [ + pytest.param("application/json", "application/json"), + pytest.param("application/vnd.api+json", "application/vnd.api+json"), + pytest.param("application/json;charset=utf-8", "application/json"), + pytest.param("application/vnd.api+json;charset=utf-8", "application/vnd.api+json"), + ], +) +def test_get_content_type(content_type: str, expected: str) -> None: + assert utils.get_content_type(content_type) == expected From aaffff3045934c7dabc80f1887d66ce6362e1ed1 Mon Sep 17 00:00:00 2001 From: Dylan Anthony <43723790+dbanty@users.noreply.github.com> Date: Tue, 28 Mar 2023 16:55:37 -0600 Subject: [PATCH 2/2] chore: fix typo in docstring --- openapi_python_client/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_python_client/utils.py b/openapi_python_client/utils.py index 5e99628b5..8d54de096 100644 --- a/openapi_python_client/utils.py +++ b/openapi_python_client/utils.py @@ -99,7 +99,7 @@ def remove_string_escapes(value: str) -> str: def get_content_type(content_type: str) -> str: """ - Given a string representing a conten type with optional parameters, returns the content type only + Given a string representing a content type with optional parameters, returns the content type only """ message = Message() message.add_header("Content-Type", content_type)