Repository navigation
[Bug]: Variant generation doesn't rewrite $ref in top-level oneOf/anyOf branches #34
Description
Describe the bug
When generating create/update request variants, the _create_single_variant function in preprocess_schemas.py only rewrites external $ref links inside properties. Schemas that use top-level oneOf or anyOf with external $refs (and no properties) have their references left pointing to the base (response) schema files instead of the corresponding request variant files.
This causes the generated Pydantic wrapper models to reference the wrong inner types — e.g. FulfillmentDestinationCreateRequest wraps ShippingDestination (response model, requires id) instead of ShippingDestinationCreateRequest (request model, id optional).
To Reproduce
Source schema for fulfillment_destination.json:
{
"title": "Fulfillment Destination",
"type": "object",
"ucp_shared_request": true,
"oneOf": [
{ "$ref": "shipping_destination.json" },
{ "$ref": "retail_location.json" }
]
}After variant generation, the generated fulfillment_destination_create_request.json should rewrite the $refs to:
{
"oneOf": [
{ "$ref": "shipping_destination_create_request.json" },
{ "$ref": "retail_location_create_request.json" }
]
}But the actual output keeps the original refs:
{
"oneOf": [
{ "$ref": "shipping_destination.json" },
{ "$ref": "retail_location.json" }
]
}Expected behavior
Generated Python model (fulfillment_destination_create_request.py):
class FulfillmentDestinationCreateRequest(
RootModel[
shipping_destination_create_request.ShippingDestinationCreateRequest
| retail_location_create_request.RetailLocationCreateRequest
]
):Screenshots
Actual generated output for fulfillment_destination_create_request.py:
class FulfillmentDestinationCreateRequest(
RootModel[
shipping_destination.ShippingDestination
| retail_location.RetailLocation
]
):Additional context
This affects any schema that defines a union type via oneOf/anyOf at the top level without properties. The rewrite_refs_to_variants function works correctly — it's just never called on the top-level oneOf/anyOf branches during variant generation.
Root Cause
In _create_single_variant, rewrite_refs_to_variants is only called inside the properties loop:
for name, data in schema.get("properties", {}).items():
if include:
prop_data = copy.deepcopy(data)
if isinstance(prop_data, dict):
rewrite_refs_to_variants(prop_data, op, file_path, ...)Schemas like fulfillment_destination.json have no properties — only oneOf with external $refs. These refs are copied verbatim into the variant without rewriting.
Fix
Add a post-processing step in _create_single_variant to rewrite $refs in top-level oneOf/anyOf branches:
variant["properties"] = new_props
variant["required"] = new_required
# Rewrite $refs in top-level oneOf/anyOf branches
for poly_key in ["oneOf", "anyOf"]:
if poly_key in variant:
rewrite_refs_to_variants(
variant[poly_key], op, file_path, global_variant_requirements
)
return variantCode of Conduct
- I agree to follow this project's Code of Conduct
Activity
vishkaty commentedon Aug 21, 2026
This still reproduces at current main, confirmed against the pinned 2026-04-08 release with the
exact repro in this report. A fix carrying forward the approach from #35, credited to
xiaoxuanz-hub, plus the test coverage damaz91 added, is up in #83 along with a fix for a
postprocessing defect that only appeared once the longer variant type references from that
approach reached a constraint enforcement pass added after #35 was last updated.