Skip to content

fix: thread the recursion guard through schema-merging-behavior v1 - #2583

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/v1-allof-recursion
Sep 26, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:fix/v1-allof-recursion

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

What was wrong. #2543 stopped the generator overflowing the stack on an allOf that refers back into a body still being generated (issue #2542) by threading a genContext through generation, with the guard living in that context. schema-merging-behavior: v1 (and its alias old-merge-schemas: true) generated every allOf member through the exported GenerateGoSchema, which starts a fresh context, so under v1 the guard never saw the enclosing frame.

In practice v1 was mostly safe anyway: it embeds a $ref member as a Go struct rather than merging it, and generateGoSchema returns a $ref to a Go type before descending, so the #2542 shapes themselves never recursed under v1 (the test #2543 added for it says as much). What did recurse without end was a member that is a $ref to a whole document ($ref: ./tree.yaml, no #): such a $ref names no Go type, so its value is generated inline, and a tree.yaml whose array items compose tree.yaml itself overflowed the stack under v1 while v2 terminated on it.

What changed. mergeSchemasV1 and its struct generation take the context of the allOf being merged, exactly as mergeSchemasV2 does. GenStructFromAllOf stays exported, as a wrapper that starts a fresh context. Under v1 the whole-document composition now becomes a named item type that refers to itself through the slice (Tree_Children_Item), as it does under v2, with the fields v1 inlines for a whole-document member. The context only influences output on the path that used to recurse forever, so nothing that generated before generates differently.

Not covered: a whole-document $ref that refers to itself without an allOf still overflows the stack, under v1 and v2 alike. That predates this change and has a different cause.

How it's tested.

🤖 Generated with Claude Code

schema-merging-behavior v1 (also old-merge-schemas: true) generated each
allOf member through the exported GenerateGoSchema, which starts a fresh
genContext. The recursion guard oapi-codegen#2543 added for issue oapi-codegen#2542 lives in that
context, so under v1 it never saw the enclosing frame. v1 embeds a $ref
member instead of merging it, so the shapes of oapi-codegen#2542 never recursed under
it; what did was a member that is a $ref to a whole document
(`$ref: ./tree.yaml`), which names no Go type and is generated inline: a
tree.yaml whose array items compose tree.yaml itself overflowed the stack
under v1, while v2 terminated on it.

mergeSchemasV1 and its struct generation now take the context of the
allOf being merged, as mergeSchemasV2 does. GenStructFromAllOf stays
exported as a wrapper that starts a fresh context. Under v1 such a
composition now becomes a named item type that refers to itself through
the slice, as under v2, with the fields v1 inlines for a whole-document
member.

The context only changes output on the path that used to recurse without
end, so nothing that generated before generates differently. Verified by
running a binary built from main and one from this branch over every
//go:generate config target under internal/test and examples: as-is and
with skip-fmt forced (363 targets each), and with schema-merging-behavior
forced to v1 where the config does not already pin a version, with and
without skip-fmt (217 targets each). Every output is identical apart from
the build-version header line (and, under skip-fmt, the import order the
separate import-order fix addresses). The only exit-code difference is
the new fixture, which main overflows on.

Tests: pkg/codegen/merge_schemas_v1_test.go pins what v1 generates for
the oapi-codegen#2542 shapes and covers the whole-document shape under v1 and v2;
internal/test/schemas/recursive_v1 generates the same shapes under v1 and
round-trips them.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 26, 2026 05:12
@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adds recursion guard to schema-merging code generation.

The PR appears safe to merge; no actionable regression was identified.

Summary

The PR threads the active recursion context through v1 allOf merging, allowing a recursive whole-document reference to generate a named item type instead of recursing indefinitely.

  • Adds unit coverage for v1’s existing recursive shapes and the whole-document case under v1 and v2.
  • Adds a v1 generated-code fixture with JSON round-trip tests.

Reviews (1) · Last reviewed commit: "fix: thread the recursion guard through ..."

@mromaszewicz mromaszewicz added the bug Something isn't working label Sep 26, 2026
@mromaszewicz
mromaszewicz merged commit 3a7db93 into oapi-codegen:main Sep 26, 2026
16 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.

1 participant