Skip to content

Commit e4e4da4

Browse files
csemanish12zmievsa
andauthored
enhance: implement json_schema_extra support for .had() (#112) (#298)
### Summary This PR fixes the missing support for `json_schema_extra` in the `.had()` function. Both callable and dict forms are now properly applied when creating runtime schemas. ### Changes - `.had()` now correctly applies `json_schema_extra`. - Unit tests updated to verify this behavior. ### Issue Closes #112 --------- Co-authored-by: Stanislav Zmiev <[email protected]>
1 parent 3857a0b commit e4e4da4

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

cadwyn/structure/schemas.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"allow_mutation",
5959
"pattern",
6060
"discriminator",
61+
"json_schema_extra",
6162
]
6263

6364

@@ -100,6 +101,7 @@ class FieldChanges:
100101
allow_mutation: bool
101102
pattern: str
102103
discriminator: str
104+
json_schema_extra: Union[dict[str, Any], Callable[[dict[str, Any]], None], None]
103105

104106

105107
@dataclass(**DATACLASS_SLOTS)
@@ -178,6 +180,7 @@ def had(
178180
allow_mutation: bool = Sentinel,
179181
pattern: str = Sentinel,
180182
discriminator: str = Sentinel,
183+
json_schema_extra: Union[dict[str, Any], Callable[[dict[str, Any]], None], None] = Sentinel,
181184
) -> FieldHadInstruction:
182185
return FieldHadInstruction(
183186
is_hidden_from_changelog=False,
@@ -222,6 +225,7 @@ def had(
222225
allow_mutation=allow_mutation,
223226
pattern=pattern,
224227
discriminator=discriminator,
228+
json_schema_extra=json_schema_extra,
225229
),
226230
)
227231

tests/test_schema_generation/test_schema_field.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
CadwynStructureError,
1111
InvalidGenerationInstructionError,
1212
)
13+
from cadwyn.schema_generation import SchemaGenerator
1314
from cadwyn.structure import schema
1415
from tests.conftest import (
1516
CreateRuntimeSchemas,
@@ -181,7 +182,7 @@ def assert_field_had_changes_apply(
181182
attr: str,
182183
attr_value: Any,
183184
create_runtime_schemas: CreateRuntimeSchemas,
184-
):
185+
) -> dict[str, SchemaGenerator]:
185186
schemas = create_runtime_schemas(version_change(schema(model).field("foo").had(**{attr: attr_value})))
186187

187188
field_info = schemas["2000-01-01"][model].model_fields["foo"]
@@ -191,6 +192,8 @@ def assert_field_had_changes_apply(
191192
else:
192193
assert getattr(field_info, attr) == attr_value
193194

195+
return schemas
196+
194197

195198
@pytest.mark.parametrize(
196199
("attr", "attr_value"),
@@ -295,6 +298,46 @@ class SchemaWithOneFloatField(BaseModel):
295298
)
296299

297300

301+
def test_schema_field_had__json_schema_extra_as_dict(create_runtime_schemas: CreateRuntimeSchemas):
302+
class SchemaWithFooHadDictJsonSchemaExtra(BaseModel):
303+
foo: str
304+
305+
schemas = assert_field_had_changes_apply(
306+
SchemaWithFooHadDictJsonSchemaExtra,
307+
"json_schema_extra",
308+
{"example": "bar"},
309+
create_runtime_schemas,
310+
)
311+
312+
# Validate the JSON Schema produced by Pydantic contains the extra
313+
model_cls = schemas["2000-01-01"][SchemaWithFooHadDictJsonSchemaExtra] # pyright: ignore
314+
json_schema = model_cls.model_json_schema()
315+
assert json_schema["properties"]["foo"]["example"] == "bar"
316+
317+
318+
def test__schema_field_had__json_schema_extra_as_callable(create_runtime_schemas: CreateRuntimeSchemas):
319+
def modify_schema(schema_dict: dict[str, Any]):
320+
schema_dict["example"] = "bar"
321+
schema_dict["custom"] = 42
322+
323+
class SchemaWithFooHadCallableJsonSchemaExtra(BaseModel):
324+
foo: str
325+
326+
schemas = assert_field_had_changes_apply(
327+
SchemaWithFooHadCallableJsonSchemaExtra,
328+
"json_schema_extra",
329+
modify_schema,
330+
create_runtime_schemas,
331+
)
332+
333+
# Validate the JSON Schema produced by Pydantic contains changes from the callable
334+
model_cls = schemas["2000-01-01"][SchemaWithFooHadCallableJsonSchemaExtra] # pyright: ignore
335+
json_schema = model_cls.model_json_schema()
336+
props = json_schema["properties"]["foo"]
337+
assert props["example"] == "bar"
338+
assert props["custom"] == 42
339+
340+
298341
def test__schema_field_didnt_have__removing_default(create_runtime_schemas: CreateRuntimeSchemas):
299342
class SchemaWithDefaults(BaseModel):
300343
foo: str = "hewwo"

0 commit comments

Comments
 (0)