oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Fix: Use of AnyOf inline generates struct type names starting with a number

Open daleef-rahman opened this issue 1 year ago • 5 comments

https://github.com/deepmap/oapi-codegen/issues/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 https://github.com/deepmap/oapi-codegen/issues/1121

daleef-rahman avatar Aug 04 '23 19:08 daleef-rahman

I accidentally closed https://github.com/deepmap/oapi-codegen/pull/1136 and lost the changes. Recovered the commits and opening a new PR here.

daleef-rahman avatar Aug 04 '23 19:08 daleef-rahman

@deepmap-marcinr I have addressed your comment on https://github.com/deepmap/oapi-codegen/pull/1136 and fixed the go generate and linter errors

daleef-rahman avatar Aug 05 '23 05:08 daleef-rahman

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.

daleef-rahman avatar Sep 04 '23 18:09 daleef-rahman

@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.

lzurbriggen avatar Oct 20 '23 07:10 lzurbriggen

I'll come back to this one - in particular re https://github.com/deepmap/oapi-codegen/pull/1178#issuecomment-1772263434 which seems interesting, and could be an existing issue

jamietanna avatar Jan 25 '24 14:01 jamietanna