Skip to content

rewrite $ref in top-level oneOf/anyOf branches during variant generation - #35

Closed
xiaoxuanz-hub wants to merge 3 commits into
Universal-Commerce-Protocol:mainfrom
xiaoxuanz-hub:u/xiaoxuan/fix-rewrite-ref-one-of
Closed

rewrite $ref in top-level oneOf/anyOf branches during variant generation#35
xiaoxuanz-hub wants to merge 3 commits into
Universal-Commerce-Protocol:mainfrom
xiaoxuanz-hub:u/xiaoxuan/fix-rewrite-ref-one-of

Conversation

@xiaoxuanz-hub

@xiaoxuanz-hub xiaoxuanz-hub commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Description

Fix _create_single_variant in preprocess_schemas.py to rewrite external $ref links in top-level oneOf/anyOf branches when generating request variant schemas.

Previously, rewrite_refs_to_variants was only called on $refs found inside properties. Schemas that define union types via top-level oneOf/anyOf (with no properties), such as fulfillment_destination.json, had their $refs left pointing to the base response schemas instead of the corresponding request variant files. This caused generated wrapper models like FulfillmentDestinationCreateRequest to incorrectly reference ShippingDestination (response, requires id) instead of ShippingDestinationCreateRequest (request, id optional).

The fix adds a post-processing step after property handling to also rewrite $refs in the variant's top-level oneOf/anyOf branches.

Affected generated models:

  • FulfillmentDestinationCreateRequest — now wraps ShippingDestinationCreateRequest | RetailLocationCreateRequest
  • FulfillmentDestinationUpdateRequest — now wraps ShippingDestinationUpdateRequest | RetailLocationUpdateRequest

Category (Required)

Please select one or more categories that apply to this change.

  • Core Protocol: Changes to the base communication layer, global context, or breaking refactors. (Requires Technical Council approval)
  • Governance/Contributing: Updates to GOVERNANCE.md, CONTRIBUTING.md, or CODEOWNERS. (Requires Governance Council approval)
  • Capability: New schemas (Discovery, Cart, etc.) or extensions. (Requires Maintainer approval)
  • Documentation: Updates to README, or documentations regarding schema or capabilities. (Requires Maintainer approval)
  • Infrastructure: CI/CD, Linters, or build scripts. (Requires DevOps Maintainer approval)
  • Maintenance: Version bumps, lockfile updates, or minor bug fixes. (Requires DevOps Maintainer approval)
  • SDK: Language-specific SDK updates and releases. (Requires DevOps Maintainer approval)
  • Samples / Conformance: Maintaining samples and the conformance suite. (Requires Maintainer approval)
  • UCP Schema: Changes to the ucp-schema tool (resolver, linter, validator). (Requires Maintainer approval)
  • Community Health (.github): Updates to templates, workflows, or org-level configs. (Requires DevOps Maintainer approval)

Related Issues

Fixes #34

Checklist

  • I have followed the Contributing Guide.
  • I have updated the documentation (if applicable).
  • My changes pass all local linting and formatting checks.
  • (For Core/Capability) I have included/updated the relevant JSON schemas.
  • I have regenerated Python Pydantic models by running generate_models.sh under python_sdk.

Screenshots / Logs (if applicable)

@xiaoxuanz-hub
xiaoxuanz-hub requested review from a team as code owners April 30, 2026 00:40

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the schema preprocessing logic to handle top-level oneOf and anyOf branches, ensuring that external references are correctly rewritten to their corresponding request or update variants. This change is reflected in the updated FulfillmentDestination models. Feedback suggests that the current implementation is too narrow and should be expanded to cover the entire variant object, including allOf and items fields, to prevent base models from being incorrectly referenced in generated request variants.

Comment thread preprocess_schemas.py Outdated
@damaz91
damaz91 removed request for dwdii, jingyli and wry-ry June 26, 2026 12:14
@damaz91 damaz91 added the status:needs-triage Signal that the PR is ready for human triage label Jul 2, 2026
@damaz91
damaz91 removed the request for review from westeezy July 2, 2026 12:24
@damaz91
damaz91 requested review from damaz91 and removed request for a team July 10, 2026 14:36
@damaz91 damaz91 added status:under-review and removed status:needs-triage Signal that the PR is ready for human triage labels Jul 10, 2026
@ShuoRen-TT

Copy link
Copy Markdown
Contributor

Hi @xiaoxuanz-hub, I opened #52 to address the separate array-root request variant issue tracked in #37.

The two PRs are complementary: #35 fixes request-variant $ref rewriting for top-level composition branches, while #52 preserves array-root schema shapes and applies nested ucp_request filtering under items and allOf.

I intentionally kept #52 scoped and excluded the whole-variant $ref rewrite and fulfillment model changes already covered by #35. Since both PRs touch _create_single_variant, whichever merges second may need a small mechanical rebase, but their functional scopes do not overlap.

@damaz91
damaz91 force-pushed the u/xiaoxuan/fix-rewrite-ref-one-of branch from 2e99557 to 34c3107 Compare July 17, 2026 08:26
@damaz91 damaz91 added status:stale-review Applied if a PR is waiting on a reviewer for too long and removed status:under-review labels Aug 1, 2026
@vishkaty

Copy link
Copy Markdown
Contributor

Thank you for this fix. The approach here, walking the whole variant tree for external refs rather
than special casing oneOf/anyOf, is the right shape and is carried forward with your authorship
intact in #83, along with the test coverage damaz91 added on this branch and a fix for
a postprocessing issue the rebase surfaced.

damaz91 added a commit that referenced this pull request Aug 24, 2026
…ant generation (#83)

* rewrite $ref in top-level oneOf/anyOf branches during variant generation

(cherry picked from commit 2a9beb9)

* update

(cherry picked from commit e652462)

* test: add coverage for composition variant propagation and rewriting

(cherry picked from commit 34c3107)

* fix: tolerate line-wrapped Annotated[...] in inject_array_contains

_create_single_variant now rewrites $ref inside top-level oneOf/anyOf/
allOf/items branches (see the preceding two commits, cherry-picked from
#35), which lengthens some array-root item type references, e.g.
total.Total becomes total_create_request.TotalCreateRequest for
TotalsCreateRequest. That extra length pushes ruff's formatter to wrap
Annotated[...] onto multiple lines with a trailing comma before the
closing bracket.

inject_array_contains spliced AfterValidator(...) in right before that
closing bracket without checking for the trailing comma, landing the
new element after an orphaned comma with nothing between them - a
SyntaxError, not just a formatting nit. Because tests/test_codegen_pipeline.py
imported the generated Totals*Request classes inside a bare
"except ImportError", the SyntaxError went uncaught and took the whole
test module down at collection time, failing every test in the file.

Fix the splice to insert after the last real token before the closing
bracket instead of blindly before it, reusing an existing trailing
comma when present. Also widen the import guard to catch SyntaxError
so one broken generated file degrades to HAVE_SDK = False instead of
failing collection for the whole module.

* chore: regenerate models against pinned UCP spec 2026-04-08

Runs generate_models.sh 2026-04-08 (README compat table: SDK 0.4.x ->
UCP schema 2026-04-08) against the three preceding commits (the #35
ref-rewrite cherry-picks plus the inject_array_contains fix). Only
totals_create_request.py/totals_update_request.py change beyond what
the cherry-picks already carried, because those two needed both fixes
together: the ref rewrite (to point at total_create_request.json
instead of total.json) and the trailing-comma-safe splice (to still
parse once that longer reference pushes Annotated[...] onto multiple
lines).

---------

Co-authored-by: xiaoxuan-hub <i.travel@live.com>
Co-authored-by: damaz91 <federico.damato91@gmail.com>
Co-authored-by: Vishal Katyal <vishal@katyal.ai>
@damaz91

damaz91 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Closed by #83

@damaz91 damaz91 closed this Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:stale-review Applied if a PR is waiting on a reviewer for too long

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Variant generation doesn't rewrite $ref in top-level oneOf/anyOf branches

5 participants