Skip to content

Commit 83773c0

Browse files
committed
Support refs
1 parent c17bc5f commit 83773c0

File tree

9 files changed

+32
-26
lines changed

9 files changed

+32
-26
lines changed

Diff for: bar.py

-5
This file was deleted.

Diff for: foo.py

-8
This file was deleted.

Diff for: json_schema_to_model/ast/class_def.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ def convert_object_schema_to_class_def(
2222
)
2323

2424
for k, v in schema.properties.items():
25-
if v.type == "boolean":
25+
if isinstance(v, json_schema.types.Ref):
26+
ref_name = v.ref.split("#")[-1]
27+
type_value = ast.Name(id=ref_name)
28+
elif v.type == "boolean":
2629
type_value = Name.bool
2730
elif v.type == "integer":
2831
type_value = Name.int

Diff for: json_schema_to_model/ast/test_class_def.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
BooleanSchema,
77
IntegerSchema,
88
ObjectSchema,
9+
Ref,
910
StringSchema,
1011
)
1112
from .class_def import convert_object_schema_to_class_def
@@ -20,19 +21,22 @@ def test_all_types(self) -> None:
2021
schema = ObjectSchema(
2122
id="#Person",
2223
properties={
23-
"first_name": StringSchema(),
24-
"last_name": StringSchema(),
25-
"age": IntegerSchema(),
26-
"is_active": BooleanSchema(),
24+
"first_name": StringSchema(type="string"),
25+
"last_name": StringSchema(type="string"),
26+
"age": IntegerSchema(type="integer"),
27+
"is_active": BooleanSchema(type="boolean"),
28+
"car": Ref(ref="Car"),
2729
},
28-
required=["first_name"]
30+
required=["first_name"],
31+
type="object",
2932
)
3033

3134
assert self._get_class_str(schema) == textwrap.dedent("""\
3235
class Person(TypedDict):
3336
first_name: str
3437
last_name: NotRequired[str]
3538
age: NotRequired[int]
36-
is_active: NotRequired[bool]"""
39+
is_active: NotRequired[bool]
40+
car: NotRequired[Car]"""
3741
)
3842

Diff for: json_schema_to_model/json_schema/load.py

-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ def _load_json_schema(path: str) -> ObjectSchema:
88

99
def load_schemas(path: str) -> list[Schema]:
1010
full_schema = _load_json_schema(path)
11-
# breakpoint()
1211

1312
return [schema for schema in full_schema.properties.values()]

Diff for: json_schema_to_model/json_schema/types.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@ class _BaseSchema(pydantic.BaseModel):
1414

1515
class BooleanSchema(_BaseSchema):
1616
id: None = None
17-
type: Literal["boolean"] = "boolean"
17+
type: Literal["boolean"]
1818

1919
class IntegerSchema(_BaseSchema):
2020
id: None = None
21-
type: Literal["integer"] = "integer"
21+
type: Literal["integer"]
2222

2323

2424
class ObjectSchema(_BaseSchema):
2525
properties: dict[str, Schema]
2626
required: list[str] = []
27-
type: Literal["object"] = "object"
27+
type: Literal["object"]
2828

2929

3030
class StringSchema(_BaseSchema):
3131
id: None = None
32-
type: Literal["string"] = "string"
32+
type: Literal["string"]
3333

3434

35-
Schema = BooleanSchema | IntegerSchema | ObjectSchema | StringSchema
35+
class Ref(pydantic.BaseModel):
36+
ref: str = pydantic.Field(alias='$ref')
37+
38+
class Config:
39+
allow_population_by_field_name = True
40+
41+
42+
Schema = Ref | BooleanSchema | IntegerSchema | ObjectSchema | StringSchema
3643

3744
ObjectSchema.update_forward_refs()

Diff for: mypy.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[mypy]
2+
plugins = pydantic.mypy

Diff for: output.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Person(TypedDict):
55
first_name: str
66
last_name: NotRequired[str]
77
age: NotRequired[int]
8+
car: NotRequired[Car]
89

910
class Car(TypedDict):
1011
color: str

Diff for: schema.json

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"description": "Age in years which must be equal to or greater than zero.",
1919
"type": "integer",
2020
"minimum": 0
21+
},
22+
"car": {
23+
"$ref": "#Car"
2124
}
2225
},
2326
"required": ["first_name"]

0 commit comments

Comments
 (0)