oapi-codegen
oapi-codegen copied to clipboard
Fix: Use of AnyOf inline generates struct type names starting with a number
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:
- When status code is the first element of path, PathToTypeName function will generate type name starting with a number
- 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
I accidentally closed https://github.com/deepmap/oapi-codegen/pull/1136 and lost the changes. Recovered the commits and opening a new PR here.
@deepmap-marcinr I have addressed your comment on https://github.com/deepmap/oapi-codegen/pull/1136 and fixed the go generate
and linter errors
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 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.
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