Skip to content

Generate a type for nested struct in respond instead of anonymous struct #1139

Closed

Description

@illiafox

1. What version are you using?

❯ oapi-codegen --version
github.com/deepmap/oapi-codegen/cmd/oapi-codegen
v1.13.0

2. 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.yaml

4. oapi-codegen config

package: api
generate:
  models: true
  fiber-server: true
  strict-server: true
  embedded-spec: true
output: api.gen.go

5. 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 = role
resp = 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.

Activity

  1. luci-sd commented on Jul 13, 2023

    @luci-sd

    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.

  2. jamietanna commented on Jul 31, 2023

    @jamietanna
    Member

    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!

  3. added
    enhancement
    on Jul 31, 2023
  4. gurleensethi commented on Dec 17, 2023

    @gurleensethi

    Any update on this ability?

  5. jamietanna commented on Dec 17, 2023

    @jamietanna
    Member

    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

  6. stnokott commented on Jan 4, 2026

    @stnokott

    @gurleensethi perhaps a little late, but encountered the same problem, managed to solve it by adding a x-go-type-name as outlined in #1512 (comment).
    oapi-codegen then created a dedicated struct for the previously anonymous inline struct.

  7. mromaszewicz commented on May 3, 2026

    @mromaszewicz
    Member

    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-name to 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 your allOf becomes a $ref to both Successful-Response and Data.

    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.

  8. added 3 commits that reference this issue on Jun 19, 2026
    4e9b14d
    121ad2f
    c1d48f7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions