Skip to content

fix: Parsing endpoint content types with semicolon separator #727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions openapi_python_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import builtins
import re
from email.message import Message
from keyword import iskeyword
from typing import Any, List

Expand Down Expand Up @@ -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 content 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
8 changes: 7 additions & 1 deletion tests/test_parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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