Skip to content

Commit fba962d

Browse files
committed
feat: Enable reference schema parsing
1 parent 655b218 commit fba962d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
Parameters,
4141
ReferencePath,
4242
Schemas,
43+
get_reference_simple_name,
4344
parse_reference_path,
4445
update_parameters_with_data,
4546
update_schemas_with_data,
@@ -324,17 +325,30 @@ def _create_schemas(
324325
while still_making_progress:
325326
still_making_progress = False
326327
errors = []
327-
next_round = []
328+
next_round: list[tuple[str, oai.Reference | oai.Schema]] = []
328329
# Only accumulate errors from the last round, since we might fix some along the way
329330
for name, data in to_process:
330-
if isinstance(data, oai.Reference):
331-
schemas.errors.append(PropertyError(data=data, detail="Reference schemas are not supported."))
332-
continue
331+
schema_data: oai.Reference | oai.Schema | None = data
333332
ref_path = parse_reference_path(f"#/components/schemas/{name}")
334333
if isinstance(ref_path, ParseError):
335334
schemas.errors.append(PropertyError(detail=ref_path.detail, data=data))
336335
continue
337-
schemas_or_err = update_schemas_with_data(ref_path=ref_path, data=data, schemas=schemas, config=config)
336+
if isinstance(data, oai.Reference):
337+
# Fully dereference reference schemas
338+
seen = [name]
339+
while isinstance(schema_data, oai.Reference):
340+
data_ref_schema = get_reference_simple_name(schema_data.ref)
341+
if data_ref_schema in seen:
342+
schemas.errors.append(PropertyError(detail="Circular schema references found", data=data))
343+
break
344+
# use derefenced schema definition for this schema
345+
schema_data = components.get(data_ref_schema)
346+
if isinstance(schema_data, oai.Schema):
347+
schemas_or_err = update_schemas_with_data(
348+
ref_path=ref_path, data=schema_data, schemas=schemas, config=config
349+
)
350+
else:
351+
schemas.errors.append(PropertyError(detail="Referent schema not found", data=data))
338352
if isinstance(schemas_or_err, PropertyError):
339353
next_round.append((name, data))
340354
errors.append(schemas_or_err)

0 commit comments

Comments
 (0)