Skip to content

Fix/flatten spec bugs - #238

Merged
fredbi merged 2 commits into
go-openapi:masterfrom
fredbi:fix/flatten-spec-bugs
Aug 24, 2026
Merged

Fix/flatten spec bugs#238
fredbi merged 2 commits into
go-openapi:masterfrom
fredbi:fix/flatten-spec-bugs

Conversation

@fredbi

@fredbi fredbi commented Aug 24, 2026

Copy link
Copy Markdown
Member

Change type

Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update

Short description

Fixes

Full description

Checklist

  • I have signed all my commits with my name and email (see DCO. This does not require a PGP-signed commit
  • I have rebased and squashed my work, so only one commit remains
  • I have added tests to cover my changes.
  • I have properly enriched go doc comments in code.
  • I have properly documented any breaking change.

fredbi and others added 2 commits August 24, 2026 14:39
A keyword the Swagger 2.0 model does not map - propertyNames, contains,
if/then/else, $defs - lands in spec.Schema.ExtraProps as raw JSON, and
analyzeSchema never looked there. Flatten imported the subtree holding such
a $ref and left the pointer untouched, so "#/definitions/leaf" ended up
naming a definition of the root document: nothing at all when the root had
no definition of that name, and an unrelated schema when it had one. The
second case validates, wrongly, and reports no error.

analyzeSchema now walks ExtraProps and records what it finds in
referenceAnalysis.unmappedRefs, keyed by the node holding the $ref
("#/definitions/deep/if/anyOf/0"). The index is deliberately kept out of
allRefs and schemas: AllRefs, AllReferences and AllDefinitionReferences
address schemas through the model, and a key naming a raw JSON node is of no
use to them or to what go-swagger builds on them. Their contents are
unchanged.

Flatten consults the new index where it has to: normalizeRef strips the
absolute base from those $ref too, importExternalReferences indexes them
alongside the mapped ones so the target is imported and named through
uniqifyName, importNewRef rebases them onto the document they came from, and
removeUnusedSinglePass counts them as a use - without that last one, the
definition just imported would be pruned again.

replace.UpdateRef gains two cases for raw JSON nodes. No spec-side change is
needed: jsonpointer hands back a copy of the any that holds the map, and a
copied map shares its storage, so writing to it reaches the document.

A document where a mapped $ref happens to pull the same target in was
already flattened correctly, by coincidence. Its output is unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi
fredbi force-pushed the fix/flatten-spec-bugs branch from f149442 to b7a8777 Compare August 24, 2026 18:17
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.63265% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.24%. Comparing base (765b296) to head (b7a8777).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
flatten.go 66.66% 7 Missing ⚠️
analyzer.go 95.23% 1 Missing ⚠️
internal/flatten/replace/replace.go 85.71% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #238       +/-   ##
===========================================
+ Coverage   50.57%   94.24%   +43.67%     
===========================================
  Files          17       29       +12     
  Lines        1750     3404     +1654     
===========================================
+ Hits          885     3208     +2323     
+ Misses        865      192      -673     
- Partials        0        4        +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@fredbi
fredbi merged commit 75424e4 into go-openapi:master Aug 24, 2026
26 checks passed
@fredbi
fredbi deleted the fix/flatten-spec-bugs branch August 24, 2026 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant