|
| 1 | +--- |
| 2 | +title: Additional properties |
| 3 | +description: "How Speakeasy-generated SDKs handle additionalProperties." |
| 4 | +--- |
| 5 | + |
| 6 | +# Additional properties |
| 7 | + |
| 8 | +When OpenAPI schemas use the [`additionalProperties`](https://json-schema.org/understanding-json-schema/reference/object#additional-properties) keyword, Speakeasy generates SDKs that handle these flexible object types automatically. |
| 9 | + |
| 10 | +## Using `additionalProperties` in SDKs |
| 11 | + |
| 12 | +**Important**: `additionalProperties` is an internal implementation detail. Pass objects with all properties directly - the SDK handles serialization and validation automatically. |
| 13 | + |
| 14 | +## The Speakeasy approach |
| 15 | + |
| 16 | +Speakeasy sets `additionalProperties` to `false` by default unless explicitly defined otherwise. This encourages fully-typed objects with clear documentation. Most developers prefer closed objects by default, but setting `additionalProperties: false` in every schema would be tedious. Most backend frameworks that generate OpenAPI schemas don't add `additionalProperties: false`, even when that's the intended behavior. |
| 17 | + |
| 18 | +## Using `additionalProperties: true` |
| 19 | + |
| 20 | +Set `additionalProperties: true` to accept arbitrary fields beyond the defined properties. This is useful for accepting unpredictable fields or allowing future extensions without schema updates. |
| 21 | + |
| 22 | +```yaml openapi.yaml |
| 23 | +components: |
| 24 | + schemas: |
| 25 | + FlexibleObject: |
| 26 | + type: object |
| 27 | + properties: |
| 28 | + title: |
| 29 | + type: string |
| 30 | + description: |
| 31 | + type: string |
| 32 | + additionalProperties: true |
| 33 | +``` |
| 34 | +
|
| 35 | +### Generated type structure |
| 36 | +
|
| 37 | +```typescript |
| 38 | +export type FlexibleObject = { |
| 39 | + title: string; |
| 40 | + description: string; |
| 41 | + additionalProperties: Record<string, any>; // Internal field - do not interact directly |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### Usage |
| 46 | + |
| 47 | +```typescript |
| 48 | +await sdk.items.create({ |
| 49 | + title: "My Item", |
| 50 | + description: "A flexible item", |
| 51 | + customField: "custom value", |
| 52 | + anotherField: 123, |
| 53 | + metadata: { key: "value" } |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +## Using typed `additionalProperties` |
| 58 | + |
| 59 | +Constrain additional properties to a specific type: |
| 60 | + |
| 61 | +```yaml openapi.yaml |
| 62 | +components: |
| 63 | + schemas: |
| 64 | + StringOnlyObject: |
| 65 | + type: object |
| 66 | + properties: |
| 67 | + title: |
| 68 | + type: string |
| 69 | + description: |
| 70 | + type: string |
| 71 | + additionalProperties: |
| 72 | + type: string |
| 73 | +``` |
| 74 | +
|
| 75 | +### Generated type structure |
| 76 | +
|
| 77 | +```typescript |
| 78 | +export type StringOnlyObject = { |
| 79 | + title: string; |
| 80 | + description: string; |
| 81 | + additionalProperties: Record<string, string>; // Internal field - do not interact directly |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +### Usage |
| 86 | + |
| 87 | +```typescript |
| 88 | +await sdk.items.create({ |
| 89 | + title: "My Item", |
| 90 | + description: "A typed item", |
| 91 | + category: "electronics", |
| 92 | + brand: "Acme", |
| 93 | + sku: "ABC-123" |
| 94 | +}); |
| 95 | +``` |
0 commit comments