Skip to content

v2.6+: simple string schemas generate type aliases, breaking method receivers #2303

@JasonLovesDoggo

Description

@JasonLovesDoggo

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 string

v2.6.0 output:

type EventType = string

Adding 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:
    - EventType

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions