fix(bundler): compose a file once when a sequence item references it - #646
Closed
daveshanley wants to merge 1 commit into
Closed
daveshanley wants to merge 1 commit into
daveshanley wants to merge 1 commit into
Conversation
The composed bundler keys each external reference by its target plus the component bucket it infers from the $ref's source path, so one file is composed once per bucket. The index records no position for sequence items, so a $ref directly in one ends its source path with the key that holds the sequence: [allOf] for a schema file whose root is an allOf, or [paths, /a, get, parameters] for an operation's parameter list. The inference expected an item index after those keys, found nothing, and left the key unscoped. The same file referenced from a mapping slot (a property, or a components/parameters entry) got a scoped key, so it was composed twice: Base and Base__schemas, or Limit and Limit__params. Every $ref pointed at the copy, and the schema holding the property ref was dropped from the bundle with "unable to locate reference anywhere in the rolodex". A $ref whose source path ends in allOf, anyOf, oneOf or prefixItems now infers schemas, and one ending in a parameters list infers parameters. components/parameters is a map, so it is left alone, and a component named after one of these keywords still takes its bucket from the components path. The issue-928 fixture hit the same bug through a oneOf: its composed bundle lifted Code2Map, Code3Map and CodeUp4Map twice, as X and X__CodeXMap. It is now composed once, and a test pins that. Fixes #644 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #646 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 300 300
Lines 38451 38462 +11
=========================================
+ Hits 38451 38462 +11
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #644.
The bug
Since v0.38,
BundleDocumentComposedcould compose the same external file twice. The trigger is a file referenced from an item of a sequence (allOf,anyOf,oneOf,prefixItems, or an operation'sparameterslist) and also from a mapping slot (a property, or acomponents/parametersentry). For example:BaseandBase__schemas, with every$refpointing at the copy.Wrapper, the schema holding the property ref, was dropped from the bundle, and the bundler loggedunable to locate reference anywhere in the rolodex.LimitandLimit__paramsfor a parameter file.The copies are identical, and code generators turn the suffix into type names. The reporter's
BaseErrorbecameBaseErrorErrorin both oapi-codegen and Speakeasy.Why
The composer keys each external reference by its target plus the component bucket it infers from the
$ref's source path. That way a file is composed once per bucket.The index records no position for sequence items. So a
$refdirectly in one has a source path ending in the key that holds the sequence:[allOf]for a schema file whose root is anallOf[paths, /a, get, parameters]for a parameter listThe inference expected an item index after those keys, found nothing, and left the key unscoped. The same file referenced from a property got a scoped key. Two keys means two compositions.
The reporter guessed right that the v0.38 source-contextual keys were involved.
The fix
sequenceItemComponentTypeinbundler/source_context.gohandles the index's real path shape:$refwhose source path ends inallOf,anyOf,oneOforprefixItemsinfersschemas.parameterslist infersparameters.components/parametersis a map, so it is left alone. A component named after one of these keywords (e.g.components/parameters/allOf) still takes its bucket from the components path.Tests
Name__<dir>) when one file is referenced from bothallOfandproperties#644 layout is run with each of the four schema keywords, with sequential and concurrent ref extraction. Each run asserts:components.schemasis exactlyWrapper,ExtendedandBaseBaseLimit, withPageLimitpointing at it.oneOf: its composed bundle liftedCode2Map,Code3MapandCodeUp4Maptwice, asXandX__CodeXMap. A new test pins the de-duplicated schema list.bundleris at 100%, andgo test ./...passes.main. Exactly one output changes: the issue-928 composed bundle above, now 63 lines shorter with the duplicates gone.🤖 Generated with Claude Code