Skip to content

Commit 1e38645

Browse files
committed
Sync open source content 🐝 (from 5a9255a4a3172a6abd31216835ab1dab25fcde4c)
1 parent 99e505f commit 1e38645

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

docs/customize/data-model/enums.mdx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ Resulting enum values will be `FOO_LOWER`, `FOO_MIXED`, and `FOO_UPPER`.
3737

3838
If unique names cannot be resolved, a validation error will prompt you to resolve conflicts, potentially using the `x-speakeasy-enums` extension.
3939

40+
The recommended approach is to use the map format, which prevents length mismatch errors:
41+
42+
```yaml
43+
schema:
44+
type: integer
45+
enum:
46+
- 1
47+
- 2
48+
- 3
49+
x-speakeasy-enums:
50+
1: NOT_STARTED
51+
2: IN_PROGRESS
52+
3: COMPLETE
53+
```
54+
55+
Alternatively, you can use the array format, but ensure the order in the enum array corresponds exactly to the custom names in the `x-speakeasy-enums` array:
56+
4057
```yaml
4158
schema:
4259
type: integer
@@ -50,8 +67,6 @@ schema:
5067
- COMPLETE
5168
```
5269

53-
Ensure the order in the enum array corresponds to the custom names in the `x-speakeasy-enums` array.
54-
5570
## Enum class naming
5671

5772
Use the `x-speakeasy-name-override` attribute to customize enum class names:
@@ -279,7 +294,7 @@ Native enums need to be
279294
imported from within the SDK but benefit from having members with clear names
280295
and documentation on each. This is particularly useful when you define enums
281296
that do not map well to spoken language, such as `enum: ["+", "_", "*", "!"]`.
282-
Using the `x-speakeasy-enums` extension will allow you to customise the name of
297+
Using the `x-speakeasy-enums` extension will allow you to customize the name of
283298
each native enum member.
284299

285300
In TypeScript and Python, native enums are nominally typed, which means that
@@ -305,9 +320,9 @@ components:
305320
- 2
306321
- 3
307322
x-speakeasy-enums:
308-
- Red
309-
- Green
310-
- Blue
323+
1: Red
324+
2: Green
325+
3: Blue
311326
```
312327
313328
In this case, the `BackgroundColor` enum will be generated as a native enum in

docs/speakeasy-reference/extensions.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Table } from "@/mdx/components";
1414
{ extension: "x-speakeasy-group", description: "Defines custom namespaces when adding this property to any operation in the OpenAPI spec. If added, the tags for that method will be ignored" },
1515
{ extension: "x-speakeasy-ignore", description: "Exclude certain methods from your SDK with this extension." },
1616
{ extension: "x-speakeasy-include", description: "Can be used to force the generation of an unused or orphaned schema from the `components` section inside the main document." },
17-
{ extension: "x-speakeasy-enums", description: "Use this extension to control generated enum members by providing alternative names for each value in the enum field." },
17+
{ extension: "x-speakeasy-enums", description: "Use this extension to control generated enum members by providing alternative names for each value in the enum field. The recommended map format prevents length mismatch errors that can occur with the array format." },
1818
{ extension: "x-speakeasy-enum-format", description: "Customize how the enum type is generated for the schema, either `enum` for a native enum type or `union` for a union of values." },
1919
{ extension: "x-speakeasy-retries", description: "Enable retries globally or on a per-request basis. A backoff strategy is applied to specified status codes." },
2020
{ extension: "x-speakeasy-pagination", description: "Use to customize offset- or cursor-based pagination rules for each API operation." },

openapi/schemas/enums.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,29 +166,9 @@ In this `oneOf` solution, the client has to supply the label and the value, defe
166166

167167
If you are using OpenAPI 3.0 or want to use `enum`, you can use an extension attribute to give labels to enum values. Different code generators and OpenAPI tools use different extension names. Below are examples using the Speakeasy extension field:
168168

169-
### Array Format (Full Coverage)
169+
### Map Format (Recommended)
170170

171-
The traditional array format provides names for all enum values in order:
172-
173-
```yaml
174-
components:
175-
schemas:
176-
CupSize:
177-
description: Size of the cup to order, represented by a numeric code with a corresponding label.
178-
type: integer
179-
enum:
180-
- 10
181-
- 20
182-
- 50
183-
x-speakeasy-enums:
184-
- Small
185-
- Medium
186-
- Large
187-
```
188-
189-
### Map Format (Full or Partial Coverage)
190-
191-
The map format allows you to specify custom names for specific enum values. This is useful when you want to provide names for only some values or when the enum values don't follow a predictable pattern:
171+
The map format is the recommended approach for `x-speakeasy-enums` as it prevents length mismatch errors and allows flexible naming of enum values. You can specify custom names for specific enum values without worrying about array ordering:
192172

193173
```yaml
194174
components:
@@ -231,6 +211,28 @@ components:
231211

232212
Both string and integer enums support the map format. When using partial coverage, enum values without explicit mappings will use their original values as names in the generated code.
233213

214+
### Array Format (Alternative)
215+
216+
The array format provides names for all enum values in order, but requires that the array length matches the enum values exactly to prevent errors:
217+
218+
```yaml
219+
components:
220+
schemas:
221+
CupSize:
222+
description: Size of the cup to order, represented by a numeric code with a corresponding label.
223+
type: integer
224+
enum:
225+
- 10
226+
- 20
227+
- 50
228+
x-speakeasy-enums:
229+
- Small
230+
- Medium
231+
- Large
232+
```
233+
234+
**Note**: The array format requires the order in the enum array to correspond exactly to the custom names in the `x-speakeasy-enums` array. If the lengths don't match, this will cause errors. For this reason, the map format is recommended as the safer approach.
235+
234236
Or you can use a simple text description to prioritize human understanding over tooling support:
235237

236238
```yaml

0 commit comments

Comments
 (0)