Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/content/docs/openapi/plugins/smart-coercion.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ This is particularly useful for [Bracket Notation](/docs/openapi/bracket-notatio

### Array → Map

Support arrays of key-value pairs:
Support arrays of key-value pairs with **unique keys**:

- `[['key1', 'value1'], ['key2', 'value2']]` → `new Map([['key1', 'value1'], ['key2', 'value2']])`
5 changes: 5 additions & 0 deletions packages/json-schema/src/coercer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ describe('jsonSchemaCoercer', () => {
{ 'type': 'array', 'items': { type: 'array', prefixItems: [{ type: 'number' }, { type: 'boolean' }] }, 'x-native-type': 'map' } as any,
['1'],
)).toEqual(['1'])

expect(coercer.coerce(
{ 'type': 'array', 'items': { type: 'array', prefixItems: [{ type: 'number' }, { type: 'boolean' }] }, 'x-native-type': 'map' } as any,
[['1', 'true'], ['2', 'false'], ['1', 'false']],
)).toEqual([[1, true], [2, false], [1, false]])
})

it('can coerce enum/const values', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/json-schema/src/coercer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ export class experimental_JsonSchemaCoercer {
return value
}

return new Map(value as [unknown, unknown][])
const result = new Map(value as [unknown, unknown][])

if (result.size !== value.length) {
return value
}

return result
}
}