diff --git a/preprocess_schemas.py b/preprocess_schemas.py index a9bba53..93b34a6 100644 --- a/preprocess_schemas.py +++ b/preprocess_schemas.py @@ -49,7 +49,11 @@ def iter_nodes(root): # Identify children for the next iteration children = [] if isinstance(curr, dict): - children = curr.values() + for k, v in curr.items(): + if k == "properties" and isinstance(v, dict): + children.extend(v.values()) + else: + children.append(v) elif isinstance(curr, list): children = curr diff --git a/tests/test_codegen_pipeline.py b/tests/test_codegen_pipeline.py index e708e3e..eba1f0c 100644 --- a/tests/test_codegen_pipeline.py +++ b/tests/test_codegen_pipeline.py @@ -55,6 +55,27 @@ class SchemaNormalizationTest(unittest.TestCase): """Tests schema flattening and reference normalization.""" + def test_iter_nodes_expands_properties_without_yielding_container( + self, + ) -> None: + """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.""" + schema = { + "type": "object", + "properties": { + "user": {"type": "string", "$ref": "user.json"}, + "allOf": {"type": "object"}, + }, + } + nodes = list(preprocess_schemas.iter_nodes(schema)) + + # The root object is yielded + self.assertIn(schema, nodes) + # The property subschemas are yielded + self.assertIn(schema["properties"]["user"], nodes) + self.assertIn(schema["properties"]["allOf"], nodes) + # The container map {"user": ..., "allOf": ...} itself is NOT yielded + self.assertNotIn(schema["properties"], nodes) + def test_resolve_local_ref_supports_objects_and_arrays(self) -> None: """Local JSON pointers resolve object keys and array indexes.""" schema = {"$defs": {"choices": [{"const": "first"}]}} @@ -1665,7 +1686,8 @@ def test_injects_cleanly_when_annotated_is_line_wrapped(self): wrap the annotation onto multiple lines with a trailing comma; a naive "insert before the closing bracket" splice then lands after that comma and produces "Field(...),\\n, AfterValidator(...)]" - - two commas with nothing between them, a SyntaxError).""" + two commas with nothing between them, a SyntaxError). + """ out = postprocess_models.inject_array_contains( self.MODULE_LINE_WRAPPED, "TotalsCreateRequest", self.GROUPS )