Repository navigation
Closed
Description
Hello, I've looked for duplicate, but could not find exactly the same bug (as far as I searched), here are related ones:
- Generation of anonymous object schemas doesn't seem to work #626
- Anonymous object schema with oneof cannot be generated properly #1496
- strict fiber server returns nothing when using schema with oneOf #1872
- oneOf in nested RequestBody structure results in empty json #1882
- bug: MarshalJSON (and UnmarshalJSON) are not generated for the ResponseObject when the response is a oneOf schema #1665
And a related PR:
Steps to reproduce:
I've created minimal reproduction code here
1. OpenAPI Spec
openapi: 3.0.2
info:
version: 1.0.0
title: Issue 1900
paths:
/ping:
get:
responses:
'200':
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Pong1'
- $ref: '#/components/schemas/Pong2'
- $ref: '#/components/schemas/Pong3'2. Oapi-codegen config
package: issue1900
output: ping.gen.go
generate:
models: true
client: true3. Generated code
type GetPingResponse struct {
Body []byte
HTTPResponse *http.Response
JSON200 *struct {
union json.RawMessage
}
JSON404 *Error
}Here the JSON200 type is unusable
Workaround
As a workaround, I've manually modified the OpenAPI spec of the API we are using before generating code:
# Changed:
'/Ping':
get:
# ...
responses:
'200':
content:
application/json:
schema:
$ref: "#/components/schemas/PongResponse"
# Added:
schemas:
# ...
PongResponse:
oneOf:
- $ref: "#/components/schemas/Pong1"
- $ref: "#/components/schemas/Pong2"
- $ref: "#/components/schemas/Pong3"
# ...And it's now generating a usable type:
type GetPingResponse struct {
Body []byte
HTTPResponse *http.Response
JSON200 *PongResponse // generated type here
JSON404 *Error
}
func (t PongResponse) AsPong1() (Pong1, error) { ... }
func (t PongResponse) AsPong2() (Pong2, error) { ... }
// ...Reacted by Denis Fetinin, Jarangutan, Robin Huang, Egor Turchinovich, I Putu Ariyasa, Toby Brain, Martin Novák, matias-siracusa and Harry Cho
Metadata
Metadata
Assignees
Labels
No labels
Activity
PaluMacil commentedon May 17, 2025
I can confirm spending a couple hours on the same issue before looking here just now. My reproduction would have looked identical though.
feat(openapi): name UserConfigField + UserConfigFieldDefault schemas (#…
Overhaul anonymous schema hoisting
Overhaul anonymous schema hoisting
Overhaul anonymous schema hoisting (#2348)