Skip to content

Commit 0d7b4b7

Browse files
p1-raNementon
authored andcommitted
parser / schemas / add indirect reference resolution
1 parent 2efe878 commit 0d7b4b7

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)
@@ -559,18 +559,17 @@ def build_schemas(
559559
next_round = []
560560
# Only accumulate errors from the last round, since we might fix some along the way
561561
for name, data in to_process:
562-
if isinstance(data, oai.Reference):
563-
schemas.errors.append(PropertyError(data=data, detail="Reference schemas are not supported."))
564-
continue
565562
ref_path = parse_reference_path(f"#/components/schemas/{name}")
566563
if isinstance(ref_path, ParseError):
567564
schemas.errors.append(PropertyError(detail=ref_path.detail, data=data))
568565
continue
569-
schemas_or_err = update_schemas_with_data(ref_path=ref_path, data=data, schemas=schemas, config=config)
566+
567+
schemas_or_err = update_schemas_with(ref_path=ref_path, data=data, schemas=schemas, config=config)
570568
if isinstance(schemas_or_err, PropertyError):
571569
next_round.append((name, data))
572570
errors.append(schemas_or_err)
573571
continue
572+
574573
schemas = schemas_or_err
575574
still_making_progress = True
576575
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
@@ -61,7 +61,30 @@ class Schemas:
6161
errors: List[ParseError] = attr.ib(factory=list)
6262

6363

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

0 commit comments

Comments
 (0)