x-enum-names¶
Override generated variable names for enum constants.
Overview¶
When consuming an enum value from an external system, the name may not produce a nice variable name. Using the x-enum-names extension allows overriding the name of the generated variable names.
Example¶
openapi: "3.0.0"
info:
version: 1.0.0
title: x-enum-names
components:
schemas:
ClientType:
type: string
enum:
- ACT
- EXP
ClientTypeWithNamesExtension:
type: string
enum:
- ACT
- EXP
x-enum-names:
- Active
- Expired
Generated Code¶
From here, we now get two different forms of the same enum definition.
type ClientType string
const (
ACT ClientType = "ACT"
EXP ClientType = "EXP"
)
type ClientTypeWithNamesExtension string
const (
Active ClientTypeWithNamesExtension = "ACT"
Expired ClientTypeWithNamesExtension = "EXP"
)
Use Cases¶
This extension is particularly useful when:
- External API uses abbreviated or cryptic enum values
- You want more descriptive constant names in your Go code
- Enum values contain special characters or don't follow Go naming conventions
- You need to maintain backwards compatibility while improving code readability
Full Example¶
You can see this in more detail in the example code.
Related Extensions¶
x-go-name- Override the generated name of a field or typex-go-type-name- Override the generated name of a type