Skip to content

Commit 308a837

Browse files
authored
clientgen: fix panic when generation openapi with named unions (#2198)
Reported in https://discord.com/channels/814482502336905216/1445923767015571599/1448428947607130183 ```ts type FooRequest = { foo: string; }; type ApiRequest = { blob: FooRequest | string; }; export const myApi = api<ApiRequest, void>( { expose: true, method: "POST" }, (req) => {}, ); ``` Now yields: ```json { "schema": { "properties": { "blob": { "anyOf": [ { "$ref": "#/components/schemas/hello.FooRequest" }, { "type": "string" } ] } }, "required": [ "blob" ], "type": "object" } } ``` Possibly also solves #1757 #1521 #1867
1 parent ca39363 commit 308a837

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

pkg/clientgen/openapi/schema.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,13 @@ func (g *Generator) schemaType(typ *schema.Type) *openapi3.SchemaRef {
186186
}
187187

188188
// Otherwise, we have to represent this as an anyOf schema.
189-
schemas := make([]*openapi3.Schema, 0, len(t.Union.Types))
189+
schemaRefs := make([]*openapi3.SchemaRef, 0, len(t.Union.Types))
190190
for _, tt := range t.Union.Types {
191-
schemas = append(schemas, g.schemaType(tt).Value)
191+
schemaRefs = append(schemaRefs, g.schemaType(tt))
192192
}
193193

194-
s := openapi3.NewAnyOfSchema(schemas...)
194+
s := openapi3.NewSchema()
195+
s.AnyOf = schemaRefs
195196
s.Nullable = haveLiteralNull
196197
return s.NewRef()
197198

0 commit comments

Comments
 (0)