Skip to content

fix(@effect/platform): flatten anyOf when accumulating same-content-type union payloads#6207

Open
Zelys-DFKH wants to merge 1 commit intoEffect-TS:mainfrom
Zelys-DFKH:fix/platform-flat-anyof-union-payload
Open

fix(@effect/platform): flatten anyOf when accumulating same-content-type union payloads#6207
Zelys-DFKH wants to merge 1 commit intoEffect-TS:mainfrom
Zelys-DFKH:fix/platform-flat-anyof-union-payload

Conversation

@Zelys-DFKH
Copy link
Copy Markdown

@Zelys-DFKH Zelys-DFKH commented May 2, 2026

Type

  • Bug Fix

Description

OpenApi.fromApi emits nested anyOf when setPayload receives a union where all members share the same content type. For example, Schema.Union(A, B, C) where all three are JSON structs produces anyOf: [ anyOf: [A, B], C ] instead of the expected flat anyOf: [A, B, C].

Root cause

extractPayloads accumulates same-content-type members with AST.Union.make([current.ast, ast]) on each pass. That builds a left-nested union tree — Union([Union([A, B]), C]) — which the JSON Schema encoder faithfully reflects as nested anyOf.

Fix

One line: replace AST.Union.make([current.ast, ast]) with HttpApiSchema.UnionUnifyAST(current.ast, ast).

UnionUnifyAST is already in the package and is already used for the same reason by the response-map accumulator (extractResponseMap, line 382). It flattens both sides through extractUnionTypes before creating the Union node, so the result stays flat no matter how many members land on the same content type. The asymmetry between the two accumulators is what introduced the bug.

Tests

Adds one test to packages/platform/test/OpenApi.test.ts: Schema.Union(A, B, C) (three JSON structs) on a POST endpoint produces a flat anyOf: [A, B, C] in the generated spec, not nested.

Related

…ype union payloads

OpenApi.fromApi was emitting nested anyOf arrays for flat unions passed
to setPayload when all members share the same content type. Each new member
was wrapped via AST.Union.make([current, next]), creating left-nested
Union nodes that the JSON Schema encoder preserved as nested anyOf.

The fix replaces that call with HttpApiSchema.UnionUnifyAST, which already
existed and is already used by the response-map accumulator for the same
purpose. It flattens both sides via extractUnionTypes before creating the
final Union node.

Fixes Effect-TS#6052
@Zelys-DFKH Zelys-DFKH requested a review from tim-smart as a code owner May 2, 2026 19:54
@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog May 2, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 2, 2026

🦋 Changeset detected

Latest commit: 15ea876

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 31 packages
Name Type
@effect/platform Patch
@effect/cli Patch
@effect/cluster Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/rpc Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-drizzle Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-node Patch
@effect/sql Patch
@effect/workflow Patch
@effect/ai Patch
@effect/ai-amazon-bedrock Patch
@effect/ai-anthropic Patch
@effect/ai-google Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/sql-kysely Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Discussion Ongoing

Development

Successfully merging this pull request may close these issues.

@effect/platform: Nested anyOf emitted for flat union payloads in OpenAPI output

1 participant