|
1 |
| -__all__ = ["Class", "Schemas", "parse_reference_path", "update_schemas_with_data"] |
| 1 | +__all__ = ["Class", "Schemas", "parse_reference_path", "update_schemas_with"] |
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING, Dict, List, NewType, Union, cast
|
4 | 4 | from urllib.parse import urlparse
|
@@ -63,7 +63,30 @@ class Schemas:
|
63 | 63 | errors: List[ParseError] = attr.ib(factory=list)
|
64 | 64 |
|
65 | 65 |
|
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( |
67 | 90 | *, ref_path: _ReferencePath, data: oai.Schema, schemas: Schemas, config: Config
|
68 | 91 | ) -> Union[Schemas, PropertyError]:
|
69 | 92 | from . import build_enum_property, build_model_property
|
|
0 commit comments