From 538dadbdc75e4d104822d9b600439e2d5f6822f9 Mon Sep 17 00:00:00 2001 From: unnoq Date: Thu, 31 Jul 2025 10:19:06 +0700 Subject: [PATCH 1/2] fix(json-schema): enhance array to map coercion to handle unique keys --- apps/content/docs/openapi/plugins/smart-coercion.md | 2 +- packages/json-schema/src/coercer.test.ts | 5 +++++ packages/json-schema/src/coercer.ts | 8 +++++++- 3 files changed, 13 insertions(+), 2 deletions(-) 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..afb8dd5d7 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'], ['duplicate', 'false'], ['duplicate', 'true']], + )).toEqual([[1, true], ['duplicate', false], ['duplicate', true]]) }) 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 } } From e70f80e8ddc160c1c68c627262ddc456f1475334 Mon Sep 17 00:00:00 2001 From: unnoq Date: Thu, 31 Jul 2025 11:22:16 +0700 Subject: [PATCH 2/2] Update packages/json-schema/src/coercer.test.ts Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- packages/json-schema/src/coercer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/json-schema/src/coercer.test.ts b/packages/json-schema/src/coercer.test.ts index afb8dd5d7..f6a5b205a 100644 --- a/packages/json-schema/src/coercer.test.ts +++ b/packages/json-schema/src/coercer.test.ts @@ -78,8 +78,8 @@ describe('jsonSchemaCoercer', () => { expect(coercer.coerce( { 'type': 'array', 'items': { type: 'array', prefixItems: [{ type: 'number' }, { type: 'boolean' }] }, 'x-native-type': 'map' } as any, - [['1', 'true'], ['duplicate', 'false'], ['duplicate', 'true']], - )).toEqual([[1, true], ['duplicate', false], ['duplicate', true]]) + [['1', 'true'], ['2', 'false'], ['1', 'false']], + )).toEqual([[1, true], [2, false], [1, false]]) }) it('can coerce enum/const values', () => {