Repository navigation
Description
Description
oapi-codegen v2.8.0 introduced an infinite recursion / stack overflow regression when generating types for a self-referential schema where the recursive $ref sits inside an allOf, and the referenced schema itself has a top-level anyOf/oneOf.
This appears to be a side effect of the fix for #1905 (PR #1904). That fix makes mergeOpenapiSchemas propagate a merged schema's oneOf/anyOf members up to the caller instead of silently dropping them (which is correct — dropping them was a real bug). However, unlike the sibling allOf merge path, which tracks visited $refs via seenSchemaRef to guard against cycles, the new oneOf/anyOf propagation added by #1904 has no cycle detection at all. When the propagated anyOf/oneOf transitively re-enters the same allOf merge, generation recurses forever instead of terminating or erroring.
Minimal reproduction
spec.yaml:
openapi: 3.0.0
info:
title: repro
version: "1.0.0"
paths: {}
components:
schemas:
Node:
anyOf:
- type: object
properties:
leaf:
type: string
- type: object
properties:
children:
type: array
items:
allOf:
- $ref: '#/components/schemas/Node'
- type: object
properties:
extra:
type: stringcfg.yaml:
package: repro
generate:
models: true
output: out.gen.gogo run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml spec.yaml
Result on v2.8.0
Immediate stack overflow:
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
goroutine 1 [running]:
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.mergeSchemas(...)
pkg/codegen/merge_schemas.go:24
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.MergeSchemas(...)
pkg/codegen/merge_schemas.go:21
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.GenerateGoSchema(...)
pkg/codegen/schema.go:852
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.oapiSchemaToGoType(...)
pkg/codegen/schema.go:1233
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.GenerateGoSchema(...)
pkg/codegen/schema.go:1210
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.GenerateGoSchema(...)
pkg/codegen/schema.go:1005
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.generateUnion(...)
pkg/codegen/schema.go:1629
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.GenerateGoSchema(...)
pkg/codegen/schema.go:1051
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.mergeSchemas(...)
pkg/codegen/merge_schemas.go:112
github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen.MergeSchemas(...)
pkg/codegen/merge_schemas.go:21
... (repeats identically until the goroutine stack limit is hit)
Result on v2.7.2 (before #1904)
Generation succeeds, but silently drops the recursive $ref from the allOf — the exact data-loss bug #1905/#1904 set out to fix:
// Node1 defines model for .
type Node1 struct {
Children *[]struct {
Extra *string `json:"extra,omitempty"`
} `json:"children,omitempty"`
}Note Node1.Children items have no reference back to Node at all — the recursive member of the allOf was dropped entirely.
Where we hit this
We hit this generating a Go client from Kibana's OpenAPI spec, on a real recursive schema shaped exactly like the repro (Kibana_HTTP_APIs_ContentPackIncludedObjects, a content-pack "included objects" tree with a routing array whose items are allOf: [$ref: self, {destination}]). Upgrading oapi-codegen from v2.7.2 to v2.8.0 turned a silent correctness bug (dropped recursive ref) into a hard crash.
Suggested fix
Extend the same seenSchemaRef cycle-guard already used for allOf members in mergeAllOf/mergeSchemas to the oneOf/anyOf propagation added in mergeOpenapiSchemas (PR #1904), so a schema that re-enters an allOf merge it's already inside of is skipped/short-circuited instead of re-expanded indefinitely.
Environment
oapi-codegenv2.8.0 (regression vs v2.7.2)- Go 1.26.1, darwin/arm64 (also reproduces regardless of platform — this is pure recursion, not a goroutine-stack-size issue)
Activity
tobio commentedon Aug 26, 2026
Working on a fix like the good little robots they can be.
Preserve recursive schema shape via back-ref substitution
fix: generate concrete recursive types for self-composing allOf
Prevent stack overflow on recursive schemas (#2543)
feat(v3): merge a property that several allOf members declare
feat(v3): merge allOf members by what allOf means (#2572)
fix: thread the recursion guard through schema-merging-behavior v1 (#…
refactor: remove duplication in the allOf and union code (#2584)