Skip to content

Commit ac2d6f7

Browse files
committed
Fixed simple types for response_model.
1 parent c438fd5 commit ac2d6f7

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

fastopenapi/core/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
float: "number",
1414
bool: "boolean",
1515
str: "string",
16+
dict: "object",
1617
}
1718

1819

fastopenapi/openapi/generator.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,15 @@ def _add_response_model(
643643
inner_type = typing.get_args(response_model)[0]
644644
if self._is_pydantic_model(inner_type):
645645
inner_schema = self.schema_builder.get_model_schema(inner_type)
646-
array_schema = {"type": "array", "items": inner_schema}
647-
responses[status_code]["content"] = {
648-
"application/json": {"schema": array_schema}
649-
}
646+
schema = {"type": "array", "items": inner_schema}
647+
else:
648+
schema = self.schema_builder.build_parameter_schema(response_model)
650649
elif self._is_pydantic_model(response_model):
651650
schema = self.schema_builder.get_model_schema(response_model)
652-
responses[status_code]["content"] = {"application/json": {"schema": schema}}
651+
else:
652+
schema = self.schema_builder.build_parameter_schema(response_model)
653+
654+
responses[status_code]["content"] = {"application/json": {"schema": schema}}
653655

654656
def _add_security_error_responses(
655657
self, responses: dict, route, has_security: bool = False

tests/openapi/test_openapi_generator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,26 +1281,26 @@ def test_response_builder_add_response_model_list_pydantic(self):
12811281
assert "$ref" in schema["items"]
12821282

12831283
def test_response_builder_add_response_model_list_non_pydantic(self):
1284-
"""Test adding list of non-Pydantic types"""
1284+
"""Test adding list of non-Pydantic types generates array schema"""
12851285
builder = ResponseBuilder(self.generator.schema_builder)
12861286

12871287
responses = {"200": {"description": "OK"}}
12881288

12891289
builder._add_response_model(responses, "200", list[str])
12901290

1291-
# Should not add content for non-Pydantic types
1292-
assert "content" not in responses["200"]
1291+
schema = responses["200"]["content"]["application/json"]["schema"]
1292+
assert schema == {"type": "array", "items": {"type": "string"}}
12931293

12941294
def test_response_builder_add_response_model_non_pydantic(self):
1295-
"""Test adding non-Pydantic response model"""
1295+
"""Test adding non-Pydantic response model generates schema"""
12961296
builder = ResponseBuilder(self.generator.schema_builder)
12971297

12981298
responses = {"200": {"description": "OK"}}
12991299

13001300
builder._add_response_model(responses, "200", str)
13011301

1302-
# Should not add content for non-Pydantic types
1303-
assert "content" not in responses["200"]
1302+
schema = responses["200"]["content"]["application/json"]["schema"]
1303+
assert schema == {"type": "string"}
13041304

13051305
def test_response_builder_add_custom_error_responses(self):
13061306
"""Test adding custom error responses"""

0 commit comments

Comments
 (0)