Skip to content

Commit 34c3107

Browse files
committed
test: add coverage for composition variant propagation and rewriting
1 parent e652462 commit 34c3107

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

tests/test_codegen_pipeline.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,49 @@ def test_array_variant_preserves_root_and_filters_nested_objects(
347347
self.assertEqual(set(item_schema["required"]), {"amount", "label"})
348348
self.assertEqual(variant["title"], "Totals Create Request")
349349

350+
def test_composition_variant_rewrites_refs(self) -> None:
351+
"""Composition variants (oneOf/anyOf/allOf) rewrite refs to variants."""
352+
schema = {
353+
"$id": "https://ucp.dev/schemas/poly.json",
354+
"title": "Poly",
355+
"oneOf": [{"$ref": "child_a.json"}, {"$ref": "child_b.json"}],
356+
"allOf": [{"$ref": "parent.json"}],
357+
"anyOf": [{"$ref": "other.json"}],
358+
}
359+
file_path = Path("/schemas/poly.json")
360+
child_a_path = str((file_path.parent / "child_a.json").resolve())
361+
child_b_path = str((file_path.parent / "child_b.json").resolve())
362+
parent_path = str((file_path.parent / "parent.json").resolve())
363+
other_path = str((file_path.parent / "other.json").resolve())
364+
365+
variant_needs = {
366+
child_a_path: {"create"},
367+
child_b_path: {"create"},
368+
parent_path: {"create"},
369+
other_path: {"create"},
370+
}
371+
372+
variant = preprocess_schemas._create_single_variant(
373+
schema,
374+
"create",
375+
"poly",
376+
file_path,
377+
variant_needs,
378+
)
379+
380+
self.assertEqual(
381+
variant["oneOf"][0]["$ref"], "child_a_create_request.json"
382+
)
383+
self.assertEqual(
384+
variant["oneOf"][1]["$ref"], "child_b_create_request.json"
385+
)
386+
self.assertEqual(
387+
variant["allOf"][0]["$ref"], "parent_create_request.json"
388+
)
389+
self.assertEqual(
390+
variant["anyOf"][0]["$ref"], "other_create_request.json"
391+
)
392+
350393
def test_generate_variants_writes_operation_specific_files(self) -> None:
351394
"""Variant generation writes one filtered file per operation."""
352395
schema = {
@@ -473,6 +516,26 @@ def test_variant_needs_propagate_transitively_and_respect_omit(
473516
self.assertEqual(variant_needs[child_path], {"create"})
474517
self.assertEqual(variant_needs[grandchild_path], {"create"})
475518

519+
def test_variant_needs_propagate_through_composition_keywords(self) -> None:
520+
"""Variant dependencies propagate unconditionally through oneOf/anyOf/allOf/items."""
521+
parent_path = "/schemas/parent.json"
522+
child_path = "/schemas/child.json"
523+
schemas = {
524+
parent_path: {"oneOf": [{"$ref": "child.json"}]},
525+
child_path: {"properties": {}},
526+
}
527+
schema_refs = {
528+
parent_path: [("oneOf", child_path)],
529+
child_path: [],
530+
}
531+
variant_needs = {parent_path: {"create", "update"}}
532+
533+
preprocess_schemas.propagate_needs_transitive(
534+
variant_needs, schema_refs, schemas
535+
)
536+
537+
self.assertEqual(variant_needs[child_path], {"create", "update"})
538+
476539
def test_main_preprocesses_schema_tree_end_to_end(self) -> None:
477540
"""The full pipeline normalizes schemas and writes linked variants."""
478541
with tempfile.TemporaryDirectory() as temp_dir:

0 commit comments

Comments
 (0)