oapi-codegen
oapi-codegen copied to clipboard
Mechanism for providing `x-go-` options at generation time
The OpenAPITools/openapi-codegen tool accepts command-line flags (or YAML/JSON parameters) named importMapping and typeMapping for specifying custom type and import mappings. This is beneficial because it allows us to avoid polluting our OpenAPI spec with complete details for generating all the various clients - our openapi spec is a "public" document, but we generate clients for internal use using internal types and libraries. The openapi-codegen approach allows us to maintain one YAML file describing the service, and then one additional YAML for each target client language, describing how to generate the appropriate client.
The openapi-codegen go generator is unfortunately severely lacking, (including that it does not support type mappings at all!) but incorporating oapi-codegen alongside openapi-codegen for other languages is a bit awkward, because of the fact that our golang type mappings will need to live in the main configuration, rather than in their own file like the type mappings for other languages. It would be nice to have the option of specifying these directly in our call to oapi-codegen, instead of having them gleaned from the OpenAPI spec properties. For instance:
package: petstoreclient
generate:
types: true
client: true
models: true
embedded-spec: true
output: petstoreclient/client.gen.go
type-mappings:
MySimpleOpenAPIType:
name: interface{}
MyComplexOpenAPIType:
type: mypkg.MyGoType
type-import: github.com/example/sample/v3/mypkg
It would also be helpful to have a similar facility for adding struct tags (the x-oapi-codegen-extra-tags
property), although it's less clear to me how that would look in the oapi-codegen configuration.
I'd also really like this feature. I think the primary issue with this is how loose the type category is for OAPI 3. I think the only logical way to do this would be to extend the "format" tag to accept type mappings. This functionality is already mostly present. I'll look into creating a prototype PR soon