Skip to content

Commit cc4db16

Browse files
committed
content_type_overrides
1 parent a73dfd4 commit cc4db16

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

openapi_python_client/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ConfigFile(BaseModel):
3535
"""
3636

3737
class_overrides: Optional[Dict[str, ClassOverride]] = None
38+
content_type_overrides: Optional[Dict[str, str]] = None
3839
project_name_override: Optional[str] = None
3940
package_name_override: Optional[str] = None
4041
package_version_override: Optional[str] = None
@@ -70,6 +71,7 @@ class Config:
7071
http_timeout: int
7172
document_source: Union[Path, str]
7273
file_encoding: str
74+
content_type_overrides: Dict[str, str]
7375

7476
@staticmethod
7577
def from_sources(
@@ -91,6 +93,7 @@ def from_sources(
9193
config = Config(
9294
meta_type=meta_type,
9395
class_overrides=config_file.class_overrides or {},
96+
content_type_overrides=config_file.content_type_overrides or {},
9497
project_name_override=config_file.project_name_override,
9598
package_name_override=config_file.package_name_override,
9699
package_version_override=config_file.package_version_override,

openapi_python_client/parser/responses.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class Response:
3737
data: Union[oai.Response, oai.Reference] # Original data which created this response, useful for custom templates
3838

3939

40-
def _source_by_content_type(content_type: str) -> Optional[_ResponseSource]:
41-
parsed_content_type = utils.get_content_type(content_type)
40+
def _source_by_content_type(content_type: str, config: Config) -> Optional[_ResponseSource]:
41+
parsed_content_type = utils.get_content_type(
42+
config.content_type_overrides.get(content_type, content_type)
43+
)
4244
if parsed_content_type is None:
4345
return None
4446

@@ -114,7 +116,7 @@ def response_from_data(
114116
)
115117

116118
for content_type, media_type in content.items():
117-
source = _source_by_content_type(content_type)
119+
source = _source_by_content_type(content_type, config)
118120
if source is not None:
119121
schema_data = media_type.media_type_schema
120122
break

tests/test_parser/test_responses.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ def test_response_from_data_unsupported_content_type():
6363
from openapi_python_client.parser.responses import response_from_data
6464

6565
data = oai.Response.model_construct(description="", content={"blah": None})
66+
config = MagicMock()
67+
config.content_type_overrides = {}
6668
response, schemas = response_from_data(
6769
status_code=200,
6870
data=data,
6971
schemas=Schemas(),
7072
parent_name="parent",
71-
config=MagicMock(),
73+
config=config,
7274
)
7375

7476
assert response == ParseError(data=data, detail="Unsupported content_type {'blah': None}")
@@ -81,12 +83,14 @@ def test_response_from_data_no_content_schema(any_property_factory):
8183
description="",
8284
content={"application/vnd.api+json; version=2.2": oai.MediaType.model_construct()},
8385
)
86+
config = MagicMock()
87+
config.content_type_overrides = {}
8488
response, schemas = response_from_data(
8589
status_code=200,
8690
data=data,
8791
schemas=Schemas(),
8892
parent_name="parent",
89-
config=MagicMock(),
93+
config=config,
9094
)
9195

9296
assert response == Response(
@@ -111,6 +115,7 @@ def test_response_from_data_property_error(mocker):
111115
content={"application/json": oai.MediaType.model_construct(media_type_schema="something")},
112116
)
113117
config = MagicMock()
118+
config.content_type_overrides = {}
114119

115120
response, schemas = responses.response_from_data(
116121
status_code=400,
@@ -141,6 +146,7 @@ def test_response_from_data_property(mocker, any_property_factory):
141146
content={"application/json": oai.MediaType.model_construct(media_type_schema="something")},
142147
)
143148
config = MagicMock()
149+
config.content_type_overrides = {}
144150

145151
response, schemas = responses.response_from_data(
146152
status_code=400,
@@ -164,3 +170,35 @@ def test_response_from_data_property(mocker, any_property_factory):
164170
parent_name="parent",
165171
config=config,
166172
)
173+
174+
175+
def test_response_from_data_content_type_overrides(any_property_factory):
176+
from openapi_python_client.parser.responses import Response, response_from_data
177+
178+
data = oai.Response.model_construct(
179+
description="",
180+
content={"application/zip": oai.MediaType.model_construct()},
181+
)
182+
config = MagicMock()
183+
config.content_type_overrides = {
184+
"application/zip": "application/octet-stream"
185+
}
186+
response, schemas = response_from_data(
187+
status_code=200,
188+
data=data,
189+
schemas=Schemas(),
190+
parent_name="parent",
191+
config=config,
192+
)
193+
194+
assert response == Response(
195+
status_code=200,
196+
prop=any_property_factory(
197+
name="response_200",
198+
default=None,
199+
required=True,
200+
description=data.description,
201+
),
202+
source=NONE_SOURCE,
203+
data=data,
204+
)

0 commit comments

Comments
 (0)