Skip to content

Commit 84d3109

Browse files
committed
Follow references in responses.
1 parent f94c281 commit 84d3109

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

openapi_python_client/parser/openapi.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,12 @@ def from_dict(data: Dict[str, Any], *, config: Config) -> Union["GeneratorData",
568568
schemas = Schemas()
569569
parameters = Parameters()
570570
if openapi.components and openapi.components.schemas:
571-
schemas = build_schemas(components=openapi.components.schemas, schemas=schemas, config=config)
571+
schemas = build_schemas(
572+
input_schemas=openapi.components.schemas,
573+
input_responses=openapi.components.responses,
574+
schemas=schemas,
575+
config=config,
576+
)
572577
if openapi.components and openapi.components.parameters:
573578
parameters = build_parameters(
574579
components=openapi.components.parameters, parameters=parameters, config=config

openapi_python_client/parser/properties/__init__.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,12 @@ def property_from_data(
811811

812812

813813
def _create_schemas(
814-
*, components: Dict[str, Union[oai.Reference, oai.Schema]], schemas: Schemas, config: Config
814+
*,
815+
input_schemas: Dict[str, Union[oai.Reference, oai.Schema]],
816+
schemas: Schemas,
817+
config: Config,
815818
) -> Schemas:
816-
to_process: Iterable[Tuple[str, Union[oai.Reference, oai.Schema]]] = components.items()
819+
to_process: Iterable[Tuple[str, Union[oai.Reference, oai.Schema]]] = input_schemas.items()
817820
still_making_progress = True
818821
errors: List[PropertyError] = []
819822

@@ -844,6 +847,32 @@ def _create_schemas(
844847
return schemas
845848

846849

850+
def _process_responses(
851+
*,
852+
input_responses: Dict[str, oai.Response],
853+
schemas: Schemas,
854+
config: Config,
855+
) -> Schemas:
856+
for name, data in input_responses.items():
857+
if not isinstance(data, oai.Response):
858+
schemas.errors.append(PropertyError(data=data, detail="Only reference schemas are supported."))
859+
continue
860+
861+
schema_ref_path = parse_reference_path(f"#/components/schemas/{name}")
862+
if isinstance(schema_ref_path, ParseError):
863+
schemas.errors.append(PropertyError(detail=schema_ref_path.detail, data=data))
864+
continue
865+
response_ref_path = parse_reference_path(f"#/components/responses/{name}")
866+
if isinstance(response_ref_path, ParseError):
867+
schemas.errors.append(PropertyError(detail=response_ref_path.detail, data=data))
868+
continue
869+
870+
prop = schemas.classes_by_reference.get(schema_ref_path)
871+
if prop:
872+
schemas = evolve(schemas, classes_by_reference={response_ref_path: prop, **schemas.classes_by_reference})
873+
return schemas
874+
875+
847876
def _propogate_removal(*, root: Union[ReferencePath, utils.ClassName], schemas: Schemas, error: PropertyError) -> None:
848877
if isinstance(root, utils.ClassName):
849878
schemas.classes_by_name.pop(root, None)
@@ -904,10 +933,16 @@ def _process_models(*, schemas: Schemas, config: Config) -> Schemas:
904933

905934

906935
def build_schemas(
907-
*, components: Dict[str, Union[oai.Reference, oai.Schema]], schemas: Schemas, config: Config
936+
*,
937+
input_schemas: Dict[str, Union[oai.Reference, oai.Schema]],
938+
input_responses: Optional[Dict[str, Union[oai.Reference, oai.Schema]]],
939+
schemas: Schemas,
940+
config: Config,
908941
) -> Schemas:
909942
"""Get a list of Schemas from an OpenAPI dict"""
910-
schemas = _create_schemas(components=components, schemas=schemas, config=config)
943+
schemas = _create_schemas(input_schemas=input_schemas, schemas=schemas, config=config)
944+
if input_responses:
945+
schemas = _process_responses(input_responses=input_responses, schemas=schemas, config=config)
911946
schemas = _process_models(schemas=schemas, config=config)
912947
return schemas
913948

openapi_python_client/parser/responses.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ def response_from_data(
6969

7070
response_name = f"response_{status_code}"
7171
if isinstance(data, oai.Reference):
72-
return (
73-
empty_response(status_code=status_code, response_name=response_name, config=config, description=None),
74-
schemas,
72+
prop, schemas = property_from_data(
73+
name=response_name,
74+
required=True,
75+
data=data,
76+
schemas=schemas,
77+
parent_name=parent_name,
78+
config=config,
7579
)
7680

81+
if isinstance(prop, PropertyError):
82+
return prop, schemas
83+
84+
return Response(status_code=status_code, prop=prop, source=None), schemas
85+
7786
content = data.content
7887
if not content:
7988
return (

0 commit comments

Comments
 (0)