Customize Enums
Default Behavior
By default, we want to give users a compilable SDK. That means making sure that there are no naming conflicts in your spec. Enums provide a particular challenge because they are often defined in-line, i.e. not as a schema definition. If an enum is defined as a schema, then we use the schema name to represent the enum, however if the enum is defined in-line, we will construct the name from it's position in the document. See below for an example showing the default behavior for both in-line, and schema-based enums.
In-line Enum:
paths:
/user/:
post:
operationId: createUser
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
format: email
description: The users email address
status:
type: string
description: The users status
enum:
- basic
- premium
country:
$ref: "#/components/schemas/Country"
responses:
"200":
description: OK
components:
schemas:
Country:
type: string
description: The users country
enum:
- US
- UK
user = {
email: "test@speakeasyapi.dev",
status: CreateUserRequestBodyStatusEnum.Basic
country: CountryEnum.UK
}
Rename In-line Enums
If you want to define enums in-line, we support the $anchor keyword to allow you to rename (i.e. shorten) enum names. See below for an example:
In-line Enum with Anchor:
paths:
/user/:
post:
operationId: createUser
requestBody:
content:
application/json:
schema:
type: object
properties:
email:
type: string
format: email
description: The users email address
status:
$anchor: Status
type: string
description: The users role
enum:
- basic
- premium
country:
$ref: "#/components/schemas/Country"
responses:
"200":
description: OK
components:
schemas:
Country:
type: string
description: The users country
enum:
- US
- UK
user = {
email: "test@speakeasyapi.dev",
status: StatusEnum.Basic
country: CountryEnum.UK
}