Skip to content

feat: Support any content type ending in +json [#706]. #709

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
merged 2 commits into from
Dec 17, 2022
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
2 changes: 1 addition & 1 deletion end_to_end_tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"application/yang-data+json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
Expand Down
8 changes: 6 additions & 2 deletions openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ def parse_request_json_body(
*, body: oai.RequestBody, schemas: Schemas, parent_name: str, config: Config
) -> Tuple[Union[Property, PropertyError, None], Schemas]:
"""Return json_body"""
body_content = body.content
json_body = body_content.get("application/json")
json_body = None
for content_type, schema in body.content.items():
if content_type == "application/json" or content_type.endswith("+json"):
json_body = schema
break

if json_body is not None and json_body.media_type_schema is not None:
return property_from_data(
name="json_body",
Expand Down
21 changes: 13 additions & 8 deletions openapi_python_client/parser/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ class Response:
source: str


_SOURCE_BY_CONTENT_TYPE = {
"application/json": "response.json()",
"application/vnd.api+json": "response.json()",
"application/octet-stream": "response.content",
"text/html": "response.text",
}
def _source_by_content_type(content_type: str) -> Optional[str]:
known_content_types = {
"application/json": "response.json()",
"application/octet-stream": "response.content",
"text/html": "response.text",
}
source = known_content_types.get(content_type)
if source is None and content_type.endswith("+json"):
# Implements https://www.rfc-editor.org/rfc/rfc6838#section-4.2.8 for the +json suffix
source = "response.json()"
return source


def empty_response(
Expand Down Expand Up @@ -75,8 +80,8 @@ def response_from_data(
)

for content_type, media_type in content.items():
if content_type in _SOURCE_BY_CONTENT_TYPE:
source = _SOURCE_BY_CONTENT_TYPE[content_type]
source = _source_by_content_type(content_type)
if source is not None:
schema_data = media_type.media_type_schema
break
else:
Expand Down
9 changes: 5 additions & 4 deletions tests/test_parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@ def test_parse_multipart_body_no_data(self):

assert prop is None

def test_parse_request_json_body(self, mocker):
@pytest.mark.parametrize(
"content_type", ("application/json", "application/vnd.api+json", "application/yang-data+json")
)
def test_parse_request_json_body(self, mocker, content_type):
from openapi_python_client.parser.openapi import Endpoint, Schemas

schema = mocker.MagicMock()
body = oai.RequestBody.construct(
content={"application/json": oai.MediaType.construct(media_type_schema=schema)}
)
body = oai.RequestBody.construct(content={content_type: oai.MediaType.construct(media_type_schema=schema)})
property_from_data = mocker.patch(f"{MODULE_NAME}.property_from_data")
schemas = Schemas()
config = MagicMock()
Expand Down