Skip to content

feat(gen): support non-primitive enums with sum types#1597

Merged
ernado merged 2 commits intoogen-go:mainfrom
lanej:feature/non-primitive-enum-support
Nov 30, 2025
Merged

feat(gen): support non-primitive enums with sum types#1597
ernado merged 2 commits intoogen-go:mainfrom
lanej:feature/non-primitive-enum-support

Conversation

@lanej
Copy link
Contributor

@lanej lanej commented Nov 30, 2025

Summary

Generate proper sum types for object enums where each enum value becomes a concrete struct variant.

Features:

  • Automatic discriminator field detection for value-based discrimination
  • Concrete struct types for each enum value (e.g., ObjectEnumFoo, ObjectEnumBar)
  • Sum type with Type field for variant switching
  • Full JSON encoding/decoding with value-based discrimination
  • Getter/setter methods for type-safe variant access

For array enums, falls back to jx.Raw since array items typically don't have discriminator fields.

Example

Given this schema:

ObjectEnum:
  type: object
  enum:
    - type: foo
      value: 1
    - type: bar
      value: 2
    - type: baz
      value: 3

Generates:

type ObjectEnum struct {
    Type          ObjectEnumType
    ObjectEnumFoo ObjectEnumFoo
    ObjectEnumBar ObjectEnumBar
    ObjectEnumBaz ObjectEnumBaz
}

type ObjectEnumFoo struct {
    Type  string `json:"type"`
    Value int64  `json:"value"`
}

Test plan

  • Integration tests for encoding/decoding
  • Tests for getter methods
  • Verify discriminator-based variant selection

Fixes #1596

Generate proper sum types for object enums where each enum value
becomes a concrete struct variant. Features:

- Automatic discriminator field detection for value-based discrimination
- Concrete struct types for each enum value (e.g., ObjectEnumFoo, ObjectEnumBar)
- Sum type with Type field for variant switching
- Full JSON encoding/decoding with value-based discrimination
- Getter/setter methods for type-safe variant access

For array enums, falls back to jx.Raw since array items typically
don't have discriminator fields.

Fixes ogen-go#1596
@lanej lanej force-pushed the feature/non-primitive-enum-support branch from ad6cfa5 to 4ca1e72 Compare November 30, 2025 01:48
The gocritic linter flagged the unnamed return values in the
findEnumDiscriminator function. Named the return values as
fieldName and values for better documentation.
@ernado ernado merged commit cb69a49 into ogen-go:main Nov 30, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: support non-primitive enums (object/array enum values)

2 participants