Skip to content

@effect/openapi-generator emits eager forward references for recursive schemas #6357

Description

@jpenilla

@effect/[email protected] can emit generated schema code that references a later declaration during module initialization.

With pnpm, generating from the Incus OpenAPI spec and importing the result fails:

ReferenceError: Cannot access 'ResourcesNetworkCardSRIOV' before initialization

Repro:

mkdir effect-openapi-repro
cd effect-openapi-repro
pnpm init
pnpm add [email protected] @effect/[email protected]
pnpm add -D @effect/[email protected] swagger2openapi
curl -fsSL https://raw.githubusercontent.com/lxc/incus/main/doc/rest-api.yaml -o rest-api.yaml
pnpm exec openapigen --spec rest-api.yaml --name Incus --format httpclient > client.gen.ts
node --experimental-strip-types -e 'import("./client.gen.ts")'

The generated code contains this pattern:

const __recursive_ResourcesNetworkCard = Schema.Struct({
  sriov: Schema.optionalKey(ResourcesNetworkCardSRIOV),
  // ...
})

export const ResourcesNetworkCardSRIOV = Schema.Struct({
  vfs: Schema.optionalKey(Schema.Array(ResourcesNetworkCard)),
})

Since ResourcesNetworkCardSRIOV is declared later, the module throws while evaluating __recursive_ResourcesNetworkCard.

Wrapping the forward reference fixes the import-time error:

sriov: Schema.optionalKey(Schema.suspend(() => ResourcesNetworkCardSRIOV))

Expected behavior: generated modules should be importable without consumers patching recursive or mutually recursive references.

Note: swagger2openapi is included in the repro only to work around Effect-TS/effect-smol#2155.

Activity

  1. added
    bug
    on Jul 14, 2026
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

    4.0bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions