Repository navigation
Closed
Labels
enhancementNew feature or request
Description
1. What version are you using?
❯ oapi-codegen --version
github.com/deepmap/oapi-codegen/cmd/oapi-codegen
v1.13.02. OpenAPI 3 spec:
/roles/{id}:
summary: Get role by id
get:
tags: [ role ]
parameters:
- name: id
in: path
description: Role ID
required: true
deprecated: false
allowEmptyValue: false
schema:
type: integer
responses:
'200':
description: Role
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/Successful-Response'
- properties:
data:
type: object
properties:
role:
$ref: '#/components/schemas/Role'
required: [ role ]
components:
schemas:
Successful-Response:
type: object
properties:
ok:
type: boolean
default: true
description: Indicated whether the response is successful.
data:
type: object
description: If successful, response from api
required:
- ok
- data 3. Cmd arguments
oapi-codegen -config oapi.yaml -package api swagger.yaml4. oapi-codegen config
package: api
generate:
models: true
fiber-server: true
strict-server: true
embedded-spec: true
output: api.gen.go5. Expected behavior
type GetRolesId200JSONResponseData struct {
Role Role `json:"role"`
}
type GetRolesId200JSONResponse struct {
Data GetRolesId200JSONResponseData `json:"data"`
// Ok Indicated whether the response is successful.
Ok bool `json:"ok"`
}6. Actual behavior
type GetRolesId200JSONResponse struct {
Data struct {
Role Role `json:"role"`
} `json:"data"`
// Ok Indicated whether the response is successful.
Ok bool `json:"ok"`
}With actual behavior there are only two ways to set data:
var resp api.GetRolesId200JSONResponse
resp.Ok = true
resp.Data.Role = roleresp = api.GetRolesId200JSONResponse{
Ok: true,
Data: struct {
Role api.Role `json:"role"`
}{
Role: dto.Role(role),
},
}But I want to do like this
resp = api.GetRolesId200JSONResponse{
Ok: true,
Data: api.GetRolesId200JSONResponseData{
Role: dto.Role(role),
},
}I saw issues and some fixes, but they don't seem to solve my problem.
Reacted by Jim Ren, Kenji Noguchi, Nikita Voynov, fushimi-wano, marcel-idana, Geert-Johan Riemer and Steffen
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
luci-sd commentedon Jul 13, 2023
I'm having a similar problem. I'm consuming an API that has defined error objects inline, as opposed to references to separate schemas. This causes oapi to generate anonymous structs for the error objects, which makes it difficult to get at that data.
The workaround has so far been to change the input YAML file, but that makes it much harder to integrate changes from the producer of the API.
jamietanna commentedon Jul 31, 2023
See also #648 - this is unfortunately one of the issues that @deepmap-marcinr and I have seen as a problem that plagues a lot of folks. When it was originally built, there was an assumption that everyone would control their specs, but that's not always the case. May be fixed before long, but in the meantime if you're able to move the types into the
#/components/schemas/that'd resolve this for now - appreciate it's a workaround, not a fix!gurleensethi commentedon Dec 17, 2023
Any update on this ability?
jamietanna commentedon Dec 17, 2023
I can try and have a look in the new year, but I've unfortunately got a lot of triaging to do first that'd take priority.
If it's high priority for you, I have a GitHub Sponsors option to prioritise the fix - happy to chat more about that if it's of interest
stnokott commentedon Jan 4, 2026
@gurleensethi perhaps a little late, but encountered the same problem, managed to solve it by adding a
x-go-type-nameas outlined in #1512 (comment).oapi-codegenthen created a dedicated struct for the previously anonymous inline struct.fix(codegen): only inject parent siblings into allOf merge when struc…
Fix: Respect parent properties in case of merged schemas by allOf (#1415
mromaszewicz commentedon May 3, 2026
This is an interesting issue. I am uncertain how to resolve it.
We intentionally don't "hoist" anonymous inner types into top level objects, leaving that ability to the user. You can use
x-go-type-nameto force it to be emitted as a top level type, or another option, is that you put your data into#/components/schemas/Data..., and then yourallOfbecomes a$refto bothSuccessful-ResponseandData.To fix this by default would mean that all anonymous inner schemas are hoisted to top level schemas, with some default name (I generate names by walking the path in the OpenAPI spec and concatenating the path, so names are long and icky). This is a major behavior change, but I think it can be put behind a flag.
When I wrote the code originally, that was my intended behavior. Hoisting all types makes for quite a lot of types, so I gave control of that via putting things into
#/components/schemas.Add optional type generation for inline schemas
codegen: warn when generate-types-for-anonymous-schemas requires type…
Add optional type generation for inline schemas (#2366)
Fix: Respect parent properties in case of merged schemas by allOf (oa…
Add optional type generation for inline schemas (oapi-codegen#2366)