Skip to content

Commit 5cae9bc

Browse files
committed
Support content-type with attributes
1 parent c93dfb0 commit 5cae9bc

File tree

3 files changed

+78
-35
lines changed

3 files changed

+78
-35
lines changed

end_to_end_tests/openapi.json

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,9 @@
756756
},
757757
"/responses/unions/simple_before_complex": {
758758
"post": {
759-
"tags": ["responses"],
759+
"tags": [
760+
"responses"
761+
],
760762
"description": "Regression test for #603",
761763
"responses": {
762764
"200": {
@@ -765,12 +767,18 @@
765767
"application/json": {
766768
"schema": {
767769
"type": "object",
768-
"required": ["a"],
770+
"required": [
771+
"a"
772+
],
769773
"properties": {
770774
"a": {
771775
"oneOf": [
772-
{"type": "string"},
773-
{"type": "object"}
776+
{
777+
"type": "string"
778+
},
779+
{
780+
"type": "object"
781+
}
774782
]
775783
}
776784
}
@@ -877,20 +885,20 @@
877885
},
878886
"parameters": [
879887
{
880-
"name": "param",
881-
"in": "query",
882-
"schema": {
883-
"type": "string"
884-
}
885-
},
886-
{
887-
"name": "param",
888-
"in": "path",
889-
"required": true,
890-
"schema": {
891-
"type": "string"
892-
}
888+
"name": "param",
889+
"in": "query",
890+
"schema": {
891+
"type": "string"
892+
}
893+
},
894+
{
895+
"name": "param",
896+
"in": "path",
897+
"required": true,
898+
"schema": {
899+
"type": "string"
893900
}
901+
}
894902
]
895903
},
896904
"/same-name-multiple-locations/{param}": {
@@ -939,7 +947,9 @@
939947
},
940948
"/tag_with_number": {
941949
"get": {
942-
"tags": ["1"],
950+
"tags": [
951+
"1"
952+
],
943953
"responses": {
944954
"200": {
945955
"description": "Success"
@@ -1182,7 +1192,9 @@
11821192
"/naming/property-conflict-with-import": {
11831193
"description": "Ensure that property names don't conflict with imports",
11841194
"post": {
1185-
"tags": ["naming"],
1195+
"tags": [
1196+
"naming"
1197+
],
11861198
"requestBody": {
11871199
"content": {
11881200
"application/json": {
@@ -1239,9 +1251,15 @@
12391251
{
12401252
"$ref": "#/components/parameters/integer-param"
12411253
},
1242-
{"$ref": "#/components/parameters/header-param"},
1243-
{"$ref": "#/components/parameters/cookie-param"},
1244-
{"$ref": "#/components/parameters/path-param"}
1254+
{
1255+
"$ref": "#/components/parameters/header-param"
1256+
},
1257+
{
1258+
"$ref": "#/components/parameters/cookie-param"
1259+
},
1260+
{
1261+
"$ref": "#/components/parameters/path-param"
1262+
}
12451263
],
12461264
"responses": {
12471265
"200": {
@@ -1272,7 +1290,7 @@
12721290
"200": {
12731291
"description": "Successful Response",
12741292
"content": {
1275-
"application/json": {
1293+
"application/json; version=1.2.3": {
12761294
"schema": {}
12771295
}
12781296
}
@@ -1572,7 +1590,11 @@
15721590
},
15731591
"Body_upload_file_tests_upload_post": {
15741592
"title": "Body_upload_file_tests_upload_post",
1575-
"required": ["some_file", "some_object", "some_nullable_object"],
1593+
"required": [
1594+
"some_file",
1595+
"some_object",
1596+
"some_nullable_object"
1597+
],
15761598
"type": "object",
15771599
"properties": {
15781600
"some_file": {
@@ -1614,7 +1636,10 @@
16141636
"some_object": {
16151637
"title": "Some Object",
16161638
"type": "object",
1617-
"required": ["num", "text"],
1639+
"required": [
1640+
"num",
1641+
"text"
1642+
],
16181643
"properties": {
16191644
"num": {
16201645
"type": "number"
@@ -1627,7 +1652,9 @@
16271652
"some_optional_object": {
16281653
"title": "Some Optional Object",
16291654
"type": "object",
1630-
"required": ["foo"],
1655+
"required": [
1656+
"foo"
1657+
],
16311658
"properties": {
16321659
"foo": {
16331660
"type": "string"
@@ -1850,7 +1877,10 @@
18501877
},
18511878
"type_enum": {
18521879
"type": "integer",
1853-
"enum": [0, 1]
1880+
"enum": [
1881+
0,
1882+
1
1883+
]
18541884
}
18551885
}
18561886
},
@@ -1863,11 +1893,15 @@
18631893
},
18641894
"type": {
18651895
"type": "string",
1866-
"enum": ["submodel"]
1896+
"enum": [
1897+
"submodel"
1898+
]
18671899
},
18681900
"type_enum": {
18691901
"type": "integer",
1870-
"enum": [0]
1902+
"enum": [
1903+
0
1904+
]
18711905
}
18721906
}
18731907
},
@@ -1882,7 +1916,10 @@
18821916
},
18831917
"type_enum": {
18841918
"type": "integer",
1885-
"enum": [0, 1]
1919+
"enum": [
1920+
0,
1921+
1
1922+
]
18861923
}
18871924
}
18881925
},
@@ -2025,7 +2062,7 @@
20252062
}
20262063
}
20272064
},
2028-
"ModelWithDateTimeProperty" : {
2065+
"ModelWithDateTimeProperty": {
20292066
"type": "object",
20302067
"properties": {
20312068
"datetime": {
@@ -2203,10 +2240,10 @@
22032240
"type": "string",
22042241
"format": "byte"
22052242
},
2206-
"import": {
2243+
"import": {
22072244
"type": "object"
22082245
},
2209-
"None": {
2246+
"None": {
22102247
"type": "object"
22112248
},
22122249
"model.reference.with.Periods": {
@@ -2385,4 +2422,4 @@
23852422
}
23862423
}
23872424
}
2388-
}
2425+
}

openapi_python_client/parser/responses.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from attrs import define
77

8+
from openapi_python_client import utils
9+
810
from .. import Config
911
from .. import schema as oai
1012
from ..utils import PythonIdentifier
@@ -22,6 +24,8 @@ class Response:
2224

2325

2426
def _source_by_content_type(content_type: str) -> Optional[str]:
27+
content_type = utils.get_content_type(content_type)
28+
2529
known_content_types = {
2630
"application/json": "response.json()",
2731
"application/octet-stream": "response.content",

tests/test_parser/test_responses.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def test_response_from_data_unsupported_content_type():
6868
def test_response_from_data_no_content_schema(any_property_factory):
6969
from openapi_python_client.parser.responses import Response, response_from_data
7070

71-
data = oai.Response.model_construct(description="", content={"application/json": oai.MediaType.model_construct()})
71+
data = oai.Response.model_construct(
72+
description="", content={"application/vnd.api+json; version=2.2": oai.MediaType.model_construct()}
73+
)
7274
response, schemas = response_from_data(
7375
status_code=200, data=data, schemas=Schemas(), parent_name="parent", config=MagicMock()
7476
)

0 commit comments

Comments
 (0)