Skip to content

Commit 806df1e

Browse files
committed
parser / schemas / add indirect reference resolution
1 parent 032a4a4 commit 806df1e

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from .enum_property import EnumProperty
2323
from .model_property import ModelProperty, build_model_property
2424
from .property import Property
25-
from .schemas import Class, Schemas, parse_reference_path, update_schemas_with_data
25+
from .schemas import Class, Schemas, parse_reference_path, update_schemas_with
2626

2727

2828
@attr.s(auto_attribs=True, frozen=True)
@@ -558,18 +558,17 @@ def build_schemas(
558558
next_round = []
559559
# Only accumulate errors from the last round, since we might fix some along the way
560560
for name, data in to_process:
561-
if isinstance(data, oai.Reference):
562-
schemas.errors.append(PropertyError(data=data, detail="Reference schemas are not supported."))
563-
continue
564561
ref_path = parse_reference_path(f"#/components/schemas/{name}")
565562
if isinstance(ref_path, ParseError):
566563
schemas.errors.append(PropertyError(detail=ref_path.detail, data=data))
567564
continue
568-
schemas_or_err = update_schemas_with_data(ref_path=ref_path, data=data, schemas=schemas, config=config)
565+
566+
schemas_or_err = update_schemas_with(ref_path=ref_path, data=data, schemas=schemas, config=config)
569567
if isinstance(schemas_or_err, PropertyError):
570568
next_round.append((name, data))
571569
errors.append(schemas_or_err)
572570
continue
571+
573572
schemas = schemas_or_err
574573
still_making_progress = True
575574
to_process = next_round

openapi_python_client/parser/properties/schemas.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__all__ = ["Class", "Schemas", "parse_reference_path", "update_schemas_with_data"]
1+
__all__ = ["Class", "Schemas", "parse_reference_path", "update_schemas_with"]
22

33
from typing import TYPE_CHECKING, Dict, List, NewType, Union, cast
44
from urllib.parse import urlparse
@@ -63,7 +63,30 @@ class Schemas:
6363
errors: List[ParseError] = attr.ib(factory=list)
6464

6565

66-
def update_schemas_with_data(
66+
def update_schemas_with(
67+
*, ref_path: _ReferencePath, data: Union[oai.Reference, oai.Schema], schemas: Schemas, config: Config
68+
) -> Union[Schemas, PropertyError]:
69+
if isinstance(data, oai.Reference):
70+
return _update_schemas_with_reference(ref_path=ref_path, data=data, schemas=schemas, config=config)
71+
else:
72+
return _update_schemas_with_data(ref_path=ref_path, data=data, schemas=schemas, config=config)
73+
74+
75+
def _update_schemas_with_reference(
76+
*, ref_path: _ReferencePath, data: oai.Reference, schemas: Schemas, config: Config
77+
) -> Union[Schemas, PropertyError]:
78+
reference_pointer = parse_reference_path(data.ref)
79+
if isinstance(reference_pointer, ParseError):
80+
return PropertyError(detail=reference_pointer.detail, data=data)
81+
82+
resolved_reference = schemas.classes_by_reference.get(reference_pointer)
83+
if resolved_reference:
84+
return attr.evolve(schemas, classes_by_reference={ref_path: resolved_reference, **schemas.classes_by_reference})
85+
else:
86+
return PropertyError(f"Reference {ref_path} could not be resolved", data=data)
87+
88+
89+
def _update_schemas_with_data(
6790
*, ref_path: _ReferencePath, data: oai.Schema, schemas: Schemas, config: Config
6891
) -> Union[Schemas, PropertyError]:
6992
from . import build_enum_property, build_model_property

0 commit comments

Comments
 (0)