diff --git a/pkg/codegen/configuration.go b/pkg/codegen/configuration.go index 5608e227cd..38afb9005b 100644 --- a/pkg/codegen/configuration.go +++ b/pkg/codegen/configuration.go @@ -269,9 +269,9 @@ type OutputOptions struct { // Whether to generate nullable type for nullable fields NullableType bool `yaml:"nullable-type,omitempty"` - // DisableTypeAliasesForType allows defining which OpenAPI `type`s will explicitly not use type aliases - // Currently supports: - // "array" + // DisableTypeAliasesForType allows defining which OpenAPI `type`s will explicitly not use type aliases. Use Go type names + // and not the OpenAPI type names. Use "[]type" to disable arrays, a shortcut for all arrays can be used by specifying + // `array` value. DisableTypeAliasesForType []string `yaml:"disable-type-aliases-for-type"` // NameNormalizer is the method used to normalize Go names and types, for instance converting the text `MyApi` to `MyAPI`. Corresponds with the constants defined for `codegen.NameNormalizerFunction` diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index d2e64f9805..0cf7abd21c 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -589,6 +589,7 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem outSchema.AdditionalTypes = arrayType.AdditionalTypes outSchema.Properties = arrayType.Properties outSchema.DefineViaAlias = true + // kept for backward compatibility, array can be disabled via either "array" or "[]type" if sliceContains(globalState.options.OutputOptions.DisableTypeAliasesForType, "array") { outSchema.DefineViaAlias = false } @@ -656,6 +657,10 @@ func oapiSchemaToGoType(schema *openapi3.Schema, path []string, outSchema *Schem } else { return fmt.Errorf("unhandled Schema type: %v", t) } + + if outSchema.GoType != "" && sliceContains(globalState.options.OutputOptions.DisableTypeAliasesForType, outSchema.GoType) { + outSchema.DefineViaAlias = false + } return nil }