fix(preprocess): rename dotted $defs to keep generated paths flat#29
Merged
damaz91 merged 7 commits intoJul 10, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to flatten dotted names within JSON schema $defs to prevent datamodel-codegen from creating nested directory structures. It adds flatten_dotted_defs to rename keys and _rewrite_local_defs_refs to update internal references. Feedback highlights that the reference rewriting logic is limited to local paths and may miss cross-file references. Additionally, a code suggestion was provided to handle edge cases where a dotted name might end in a period, which could result in an invalid empty string key.
Upstream UCP uses reverse-DNS extension mount points such as "dev.ucp.shopping.checkout" in $defs. datamodel-codegen treats dots in def names as path separators, so those defs were being emitted as nested directory trees (e.g. shopping/fulfillment/dev/ucp/shopping.py) instead of inline classes in the parent schema's module. This added a normalization pass that renames such keys before codegen. Strategy: prefer the last dotted component (giving a clean class name like "Checkout"); fall back to dot-replaced-with-underscore if the bare tail would collide with an existing def in the same file (the fulfillment.json case, where the dotted key shares its tail with a sibling "fulfillment" def). Local $ref pointers to renamed defs are rewritten in the same pass so no internal references break. No upstream schemas currently reference the dotted defs by $ref; the rewrite is a safety net. Effect on regen: extension classes return to their natural import paths. For example, ucp_sdk.models.schemas.shopping.fulfillment.Checkout was being relocated to ucp_sdk.models.schemas.shopping.fulfillment.dev.ucp. shopping.Checkout under current upstream; this restores the flat path.
deea90b to
bcda715
Compare
Address review comments by collecting all renames and rewriting external references in a separate pass. Regenerated models using UCP version 2026-04-08. TAG=agy CONV=b06606c8-326a-4573-b94e-4cdb020d3003
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Resolve merge conflicts in generated models by regenerating them. TAG=agy CONV=b06606c8-326a-4573-b94e-4cdb020d3003
…otted-defs TAG=agy CONV=b06606c8-326a-4573-b94e-4cdb020d3003
damaz91
approved these changes
Jul 10, 2026
TAG=agy CONV=b06606c8-326a-4573-b94e-4cdb020d3003
TAG=agy CONV=b06606c8-326a-4573-b94e-4cdb020d3003
This was referenced Jul 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Schema names with dots (e.g.
dev.ucp.shopping.checkout) trip updatamodel-codegen. It reads the dots as path separators and emits stuff likeshopping/fulfillment/dev/ucp/shopping.py. See #28 for live examples:ap2_mandate,buyer_consent,discount, andfulfillmentall hit it.This adds a 64-line pass to
preprocess_schemas.pythat walks each schema, renames dotted$defskeys to their last segment, and patches the$refs. Runs before the existingallOfflattening. Generated paths come out clean.Heads up: a few extension classes change import paths. They only landed at the nested paths because of the bug, and nothing released uses them, but worth flagging.
Category
Checklist
Logs
pre-commit run --all-filespasses../generate_models.sh 2026-04-08with this fix produces clean paths (nodev/ucp/shopping.py).Checkoutpayload round-trips throughmodel_validate/model_dump.