Skip to content

fix: ignore kin-openapi source locations when comparing type definitions - #2601

Merged
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
eyoshioka:fix/2599-ignore-origin-in-type-equivalence
Oct 31, 2026
Merged

mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
eyoshioka:fix/2599-ignore-origin-in-type-equivalence

Conversation

@eyoshioka

Copy link
Copy Markdown
Contributor

Fixes #2599.

Since #2465, the loader sets IncludeOrigin, so every schema records where it was declared. TypeDefinitionsEquivalent used reflect.DeepEqual, so identical schemas declared on different lines no longer matched.

This implements option 2 from my comment on the issue. It compares the schemas the same way as reflect.DeepEqual, but treats any two *openapi3.Origin values as equal. The code follows the standard library's deepValueEqual, including cycle detection for recursive schemas, and its doc comment lists where the two differ. Fields are skipped by type, so this also covers Origin on SchemaRef, Discriminator and other kin-openapi types, and on any fields added later. It adds no new dependency. If you'd rather use go-cmp, I'm happy to switch, and the tests stay the same.

Tests:

  • TestTypeDefinitionsEquivalentIgnoresOrigin covers inline, nested and recursive schemas, which should be equal. It also covers two cases that must stay different: the same relative $ref resolving to different files, and a 3.1 $ref with a sibling keyword. Comparing JSON would fail both of these.
  • TestTypeDefinitionsEquivalentCycles compares two separately built self-referencing schemas. A loaded spec shares one *Schema per $ref target, so the loaded cases never reach the cycle detection. Without the detection, this test overflows the stack.
  • TestIdenticalXGoTypeNameSchemasWithOrigin runs the issue's spec through Generate.

make generate produces no diff with Go 1.27.1, which matches CI's stable. With Go 1.25 or 1.26, only the embedded spec blobs change, with or without this change. make test and make lint pass.

I used Claude to help write this change. I've reviewed and tested it myself.

Since oapi-codegen#2465 the loader sets IncludeOrigin, so every schema records the
line it was declared on. TypeDefinitionsEquivalent compared schemas with
reflect.DeepEqual, so two identical schemas with the same
x-go-type-name on different lines no longer matched, and generation
failed with "duplicate typename".

Compare the schemas like reflect.DeepEqual does, but treat any two
*openapi3.Origin values as equal. Comparing the JSON encodings instead
would drop resolved $ref targets and OpenAPI 3.1 $ref siblings, so
different schemas could be merged silently.

Fixes oapi-codegen#2599

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@eyoshioka
eyoshioka marked this pull request as ready for review October 8, 2026 10:40
@eyoshioka
eyoshioka requested a review from a team as a code owner October 8, 2026 10:40
@greptile-apps

greptile-apps Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Updates TypeDefinitionsEquivalent to ignore kin-openapi source locations without changing the schemas.

  • Matching named schemas can share one generated Go type despite different source locations.

Reviews (3) · Last reviewed commit: "Revert "fix: blank origins and reuse ref..." · Reviewed by Greptile

@mromaszewicz mromaszewicz added the bug Something isn't working label Oct 29, 2026
mromaszewicz and others added 2 commits October 31, 2026 16:38
…valent

Instead of carrying a copy of reflect's deepValueEqual that skips
*openapi3.Origin, walk both schemas once, nil every reachable Origin
field, run the real reflect.DeepEqual, and restore the origins afterwards.
Origins are still read later (for example to order paths by source line),
so a test checks that they come back.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mromaszewicz

Copy link
Copy Markdown
Member

Thanks for doing this. I'm going to push a change to it. Rather than reimplementing so much of DeepEquals, we're going to back up and zero out the Origin on the schemas, then restore before exiting, so that we can reuse the existing DeepEquals representation. It's not as clean, but it's a lot simpler and works for us.

Comment thread pkg/codegen/utils.go Resolved
@mromaszewicz

Copy link
Copy Markdown
Member

NVM, what I did is worse. Apologies. I'll put it back as it was, and I merged main into your branch so this applies cleanly.

…ionsEquivalent"

This reverts commit a4d1d606c1cc5bd5e1dbe42f2a2d7af1fa3c9bd4.

Clearing the origins before calling reflect.DeepEqual was a mistake.
Origins sit on every nested Schema, SchemaRef and Discriminator, so the
"clear and restore" walker ends up being its own recursive traversal with
cycle detection, while also mutating shared schema objects during the
comparison. The original read-only comparer that skips *openapi3.Origin
is the simpler and safer approach, so this restores it as submitted.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mromaszewicz
mromaszewicz enabled auto-merge (squash) October 31, 2026 21:48
@mromaszewicz
mromaszewicz merged commit b4fdfe1 into oapi-codegen:main Oct 31, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v2.8.0: identical schemas with the same x-go-type-name now fail with "duplicate typename"

2 participants