Skip to content

v2.8.0 regression: infinite recursion / stack overflow on self-referential allOf+anyOf schemas (side effect of #1904/#1905) #2542

Closed

Description

@tobio

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: string

cfg.yaml:

package: repro
generate:
  models: true
output: out.gen.go
go 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-codegen v2.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

  1. tobio commented on Aug 26, 2026

    @tobio
    ContributorAuthor

    Working on a fix like the good little robots they can be.

  2. added a commit that references this issue on Aug 26, 2026
    f9de283
  3. added a commit that references this issue on Sep 17, 2026
    a6d9d25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions