Skip to content

Commit 582fe41

Browse files
authored
fix: preprocess to expand properties dict instead of add as a node to … (#86)
* fix preprocess to expand properties dict instead of add as a node to avoid traversing properties with names that match json schema keywords. * adding test case * running generate mocdles with latest version isntead of draft * running uv run ruff format * fix end of files * fix precomit lint failure
1 parent 51bf73c commit 582fe41

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

preprocess_schemas.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ def iter_nodes(root):
4949
# Identify children for the next iteration
5050
children = []
5151
if isinstance(curr, dict):
52-
children = curr.values()
52+
for k, v in curr.items():
53+
if k == "properties" and isinstance(v, dict):
54+
children.extend(v.values())
55+
else:
56+
children.append(v)
5357
elif isinstance(curr, list):
5458
children = curr
5559

tests/test_codegen_pipeline.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@
5555
class SchemaNormalizationTest(unittest.TestCase):
5656
"""Tests schema flattening and reference normalization."""
5757

58+
def test_iter_nodes_expands_properties_without_yielding_container(
59+
self,
60+
) -> None:
61+
"""iter_nodes yields "properties" map values directly rather than the "properties" map as a container. Avoids traversal of property names that match json schema keywords."""
62+
schema = {
63+
"type": "object",
64+
"properties": {
65+
"user": {"type": "string", "$ref": "user.json"},
66+
"allOf": {"type": "object"},
67+
},
68+
}
69+
nodes = list(preprocess_schemas.iter_nodes(schema))
70+
71+
# The root object is yielded
72+
self.assertIn(schema, nodes)
73+
# The property subschemas are yielded
74+
self.assertIn(schema["properties"]["user"], nodes)
75+
self.assertIn(schema["properties"]["allOf"], nodes)
76+
# The container map {"user": ..., "allOf": ...} itself is NOT yielded
77+
self.assertNotIn(schema["properties"], nodes)
78+
5879
def test_resolve_local_ref_supports_objects_and_arrays(self) -> None:
5980
"""Local JSON pointers resolve object keys and array indexes."""
6081
schema = {"$defs": {"choices": [{"const": "first"}]}}
@@ -1665,7 +1686,8 @@ def test_injects_cleanly_when_annotated_is_line_wrapped(self):
16651686
wrap the annotation onto multiple lines with a trailing comma; a
16661687
naive "insert before the closing bracket" splice then lands after
16671688
that comma and produces "Field(...),\\n, AfterValidator(...)]" -
1668-
two commas with nothing between them, a SyntaxError)."""
1689+
two commas with nothing between them, a SyntaxError).
1690+
"""
16691691
out = postprocess_models.inject_array_contains(
16701692
self.MODULE_LINE_WRAPPED, "TotalsCreateRequest", self.GROUPS
16711693
)

0 commit comments

Comments
 (0)