diff --git a/preprocess_schemas.py b/preprocess_schemas.py index a4fe2e9..9d433ab 100644 --- a/preprocess_schemas.py +++ b/preprocess_schemas.py @@ -104,6 +104,10 @@ def _process_all_of_item(item, node, root, state): if not isinstance(item, dict): return + if any(key in item for key in ("if", "then", "else")): + state["remaining_refs"].append(item) + return + # Extract polymorphic branches (anyOf, oneOf) to keep the node flat for poly_key in ["anyOf", "oneOf"]: if poly_key in item: diff --git a/tests/test_codegen_pipeline.py b/tests/test_codegen_pipeline.py index fe4d863..8519a41 100644 --- a/tests/test_codegen_pipeline.py +++ b/tests/test_codegen_pipeline.py @@ -91,6 +91,29 @@ def test_preprocess_flattens_and_distributes_properties(self) -> None: self.assertEqual(set(branch["required"]), {"id", "kind"}) self.assertEqual(branch["type"], "object") + def test_preprocess_preserves_multiple_conditional_branches(self) -> None: + """Each conditional allOf branch survives schema flattening.""" + negative = { + "if": {"properties": {"type": {"const": "discount"}}}, + "then": {"properties": {"amount": {"exclusiveMaximum": 0}}}, + } + non_negative = { + "if": {"properties": {"type": {"const": "subtotal"}}}, + "then": {"properties": {"amount": {"minimum": 0}}}, + } + schema = { + "type": "object", + "properties": { + "type": {"type": "string"}, + "amount": {"type": "integer"}, + }, + "allOf": [negative, non_negative], + } + + preprocess_schemas.preprocess_full_schema(schema) + + self.assertEqual(schema["allOf"], [negative, non_negative]) + def test_preprocess_inlines_entity_fields(self) -> None: """The shared entity definition is inlined without its metadata.""" entity = {