Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-platform-flat-anyof-union-payload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

`OpenApi.fromApi` now emits a flat `anyOf` array when multiple union members in `setPayload` share the same content type. Previously, each additional member was wrapped in a new `Union` node, producing deeply nested `anyOf: [ anyOf: [A, B], C ]` structures. The fix uses `HttpApiSchema.UnionUnifyAST` — already used by the response-map accumulator — to flatten unions as they are collected.
2 changes: 1 addition & 1 deletion packages/platform/src/HttpApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ const extractPayloads = (topAst: AST.AST): ReadonlyMap<string, {
ast
})
} else {
current.ast = AST.Union.make([current.ast, ast])
current.ast = HttpApiSchema.UnionUnifyAST(current.ast, ast)
}
}
if (topAst._tag === "Union") {
Expand Down
72 changes: 72 additions & 0 deletions packages/platform/test/OpenApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,78 @@ describe("OpenApi", () => {
expectSpecPaths(api, expected)
})
})

it("setPayload with a flat JSON union produces flat anyOf (not nested)", () => {
const A = Schema.Struct({ _tag: Schema.Literal("A"), a: Schema.String })
const B = Schema.Struct({ _tag: Schema.Literal("B"), b: Schema.Number })
const C = Schema.Struct({ _tag: Schema.Literal("C"), c: Schema.Boolean })
const api = HttpApi.make("api").add(
HttpApiGroup.make("group").add(
HttpApiEndpoint.post("post", "/")
.addSuccess(Schema.String)
.setPayload(Schema.Union(A, B, C))
)
)
expectSpecPaths(api, {
"/": {
"post": {
"tags": ["group"],
"operationId": "group.post",
"parameters": [],
"security": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"anyOf": [
{
"type": "object",
"required": ["_tag", "a"],
"properties": {
"_tag": { "enum": ["A"], "type": "string" },
"a": { "type": "string" }
},
"additionalProperties": false
},
{
"type": "object",
"required": ["_tag", "b"],
"properties": {
"_tag": { "enum": ["B"], "type": "string" },
"b": { "type": "number" }
},
"additionalProperties": false
},
{
"type": "object",
"required": ["_tag", "c"],
"properties": {
"_tag": { "enum": ["C"], "type": "string" },
"c": { "type": "boolean" }
},
"additionalProperties": false
}
]
}
}
},
"required": true
},
"responses": {
"200": {
"description": "a string",
"content": {
"application/json": {
"schema": { "type": "string" }
}
}
},
"400": HttpApiDecodeError
}
}
}
})
})
})

describe("HttpApiEndpoint.del", () => {
Expand Down
Loading