Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
illiafox opened this issue Jul 8, 2023 · 4 comments
Open
Labels
enhancement New feature or request

Comments

@illiafox
Copy link

illiafox commented Jul 8, 2023

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.

@luci-sd
Copy link

luci-sd commented 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
Copy link
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!

@jamietanna jamietanna added the enhancement New feature or request label Jul 31, 2023
@gurleensethi
Copy link

Any update on this ability?

@jamietanna
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants