diff --git a/apps/content/docs/openapi/plugins/smart-coercion.md b/apps/content/docs/openapi/plugins/smart-coercion.md index 24a78d11e..16cee3b62 100644 --- a/apps/content/docs/openapi/plugins/smart-coercion.md +++ b/apps/content/docs/openapi/plugins/smart-coercion.md @@ -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']])` diff --git a/packages/json-schema/src/coercer.test.ts b/packages/json-schema/src/coercer.test.ts index 3e320cd20..f6a5b205a 100644 --- a/packages/json-schema/src/coercer.test.ts +++ b/packages/json-schema/src/coercer.test.ts @@ -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', () => { diff --git a/packages/json-schema/src/coercer.ts b/packages/json-schema/src/coercer.ts index f3c5f3b43..b21e58f7c 100644 --- a/packages/json-schema/src/coercer.ts +++ b/packages/json-schema/src/coercer.ts @@ -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 } }