-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Problem
Starting in v2.6.0, oapi-codegen generates simple string schemas as type aliases (type X = string) instead of named types (type X string).
This is a breaking change: you cannot define methods on a type alias in Go. Any project that attaches methods to a generated string type (e.g. validation, enum helpers) will fail to compile after regenerating with @latest.
Reproduction
OpenAPI schema:
components:
schemas:
EventType:
type: string
maxLength: 64
pattern: "^(\\$[a-z][a-z0-9_:]*|[a-z][a-z0-9_:]*)$"v2.5.x output:
type EventType stringv2.6.0 output:
type EventType = stringAdding a method in the same package:
func (e EventType) Valid() bool { ... }Fails with:
cannot define new methods on non-local type EventType
Workaround
Exclude the schema from generation and define the type manually:
output-options:
exclude-schemas:
- EventTypeExpected behavior
Simple string schemas with constraints (pattern, maxLength, enum) should generate named types, not aliases, since these are the schemas most likely to have user-defined methods. At minimum, this should be opt-in via a config flag rather than the default behavior.