Skip to content

fix: correctly generate structs and properties for anyOfs #1178

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

Merged
merged 15 commits into from
Sep 20, 2024

Conversation

daleef-rahman
Copy link
Contributor

@daleef-rahman daleef-rahman commented Aug 4, 2023

#1121 reports that use of AnyOf inline generates invalid go code for client (struct type names starting with a number).
This PR fixes 2 issues:

  1. When status code is the first element of path, PathToTypeName function will generate type name starting with a number
  2. When a property have UnionElements and we use PathToTypeName to generate a reference type, we are not creating the corresponding struct for it

Closes #1121

daleeff and others added 5 commits July 5, 2023 00:32
…number

An issue oapi-codegen#1121 was reported that use of AnyOf inline generates invalid go code for client.
This PR fixes 2 issues:
1. When status code is the starting element of path PathToTypeName will generated names starting with a number
2. When we have UnionElements and we use PathToTypeName to generate a reference type, we are not creating the struct for it

Closes oapi-codegen#1121
@daleef-rahman
Copy link
Contributor Author

I accidentally closed #1136 and lost the changes. Recovered the commits and opening a new PR here.

@daleef-rahman
Copy link
Contributor Author

@deepmap-marcinr I have addressed your comment on #1136 and fixed the go generate and linter errors

Copy link
Member

@jamietanna jamietanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @daleef-rahman looks like we're missing the config for oapi-codegen to generate the code for internal/test/any_of/codegen - mind adding?

@jamietanna jamietanna modified the milestones: v1.14.0, v1.15.0 Aug 29, 2023
@daleef-rahman
Copy link
Contributor Author

Hey @daleef-rahman looks like we're missing the config for oapi-codegen to generate the code for internal/test/any_of/codegen - mind adding?

@jamietanna Added the missing config. Please check now.

@lzurbriggen
Copy link

lzurbriggen commented Oct 20, 2023

@daleef-rahman FYI, i tried this fork on a schema i had issues with. i have a scenario where the fix outputs the same struct twice:

      operationId: myOperation
      responses:
        "200":
          content:
            application/something.v1+json:
              schema:
                type: array
                items:
                  type: object
                  oneOf:
                  - $ref: '#/components/schemas/Ref1'
                  - $ref: '#/components/schemas/Ref2'
                  - $ref: '#/components/schemas/Ref3'
            application/json:
              schema:
                type: array
                items:
                  type: object
                  oneOf:
                  - $ref: '#/components/schemas/Ref1'
                  - $ref: '#/components/schemas/Ref2'
                  - $ref: '#/components/schemas/Ref3'

output:

type MyOperation_200_Item struct {
	union json.RawMessage
}
type MyOperation_200_Item struct {
	union json.RawMessage
}

i don't know why the schema looks the way it does and i have no control over it, but it seems to be valid.

Note

Maintainer edit: this issue is related to #1367

@jamietanna jamietanna modified the milestones: v1.16.0, v2.1.0 Oct 23, 2023
@jamietanna
Copy link
Member

I'll come back to this one - in particular re #1178 (comment) which seems interesting, and could be an existing issue

@jamietanna jamietanna requested a review from a team as a code owner September 20, 2024 13:41
@jamietanna jamietanna changed the title Fix: Use of AnyOf inline generates struct type names starting with a number fix: fix: correctly generate structs and properties for anyOfs Sep 20, 2024
@jamietanna jamietanna added bug Something isn't working area:*of `oneOf`/`anyOf`/`allOf` labels Sep 20, 2024
@jamietanna jamietanna changed the title fix: fix: correctly generate structs and properties for anyOfs fix: correctly generate structs and properties for anyOfs Sep 20, 2024
@jamietanna jamietanna enabled auto-merge (squash) September 20, 2024 14:26
Copy link
Member

@jamietanna jamietanna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@jamietanna jamietanna merged commit dad645e into oapi-codegen:main Sep 20, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:*of `oneOf`/`anyOf`/`allOf` bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use of AnyOf generates struct type names starting with a number
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy