Repository navigation
fix: thread the recursion guard through schema-merging-behavior v1 - #2583
Merged
mromaszewicz merged 1 commit intoSep 26, 2026
Merged
Conversation
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]>
Contributor
|
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.
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
genContextthrough generation, with the guard living in that context.schema-merging-behavior: v1(and its aliasold-merge-schemas: true) generated every allOf member through the exportedGenerateGoSchema, 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
$refmember as a Go struct rather than merging it, andgenerateGoSchemareturns a$refto 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$refto a whole document ($ref: ./tree.yaml, no#): such a$refnames no Go type, so its value is generated inline, and atree.yamlwhose array items composetree.yamlitself overflowed the stack under v1 while v2 terminated on it.What changed.
mergeSchemasV1and its struct generation take the context of the allOf being merged, exactly asmergeSchemasV2does.GenStructFromAllOfstays 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
$refthat 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.
pkg/codegen/merge_schemas_v1_test.gopins what v1 generates for the v2.8.0 regression: infinite recursion / stack overflow on self-referential allOf+anyOf schemas (side effect of #1904/#1905) #2542 shapes (embedded$ref, inlined sibling, no named item type) and covers the whole-document shape under both v1 and v2. The whole-document case overflows the stack without the fix.internal/test/schemas/recursive_v1generates the v2.8.0 regression: infinite recursion / stack overflow on self-referential allOf+anyOf schemas (side effect of #1904/#1905) #2542 shapes and the whole-document shape under v1 and round-trips them;mainoverflows on this fixture.mainagainst one from this branch over every//go:generate ... --configtarget underinternal/testandexamples: as-is and withskip-fmtforced (363 targets each), and withschema-merging-behavior: v1forced where the config does not already pin a version, with and withoutskip-fmt(217 targets each). Every output is identical apart from the build-version header line (and, underskip-fmt, the import order that varied between runs, since fixed by fix: make the order of external imports deterministic #2582). The only exit-code difference is the new fixture.make generatechanges no committed file;make test,make lintandinternal/testunder Go 1.25 pass.🤖 Generated with Claude Code