From 1d9202e20c3631e66c1500397c95b59ea354d2c6 Mon Sep 17 00:00:00 2001 From: yuyongyan29-dev <286965556+yuyongyan29-dev@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:04:36 +0800 Subject: [PATCH] fix: keep array index keys in default order with objectMechanism 'native' The default codegen writes required properties first. With 'native' the values go through an object, and an object always lists array index keys (such as "2") first, so a required "a" next to an optional "2" came out as {"2":...,"a":...} instead of {"a":...,"2":...}. Leave such schemas to the default codegen. --- lib/projection.js | 7 ++++++- test/object-mechanism-native.test.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/projection.js b/lib/projection.js index 3f0e999b..1eb56c7c 100644 --- a/lib/projection.js +++ b/lib/projection.js @@ -12,7 +12,7 @@ // Projectable schemas (every node must be one of these): // - object with `properties` only: no patternProperties, combinators, // dependencies, and additionalProperties absent or false; at least one -// property; +// property; no array index key placed after a required property; // - string / number / integer / boolean without const or combinators; // a string may have any format but 'unsafe', which is written unescaped; // - the two-element forms ['string'|'number'|'boolean', 'null'] (any order), @@ -103,6 +103,11 @@ function resolveNode (location, refs, state, depth) { } const keys = Object.keys(schema.properties) if (keys.length === 0) return null + // an object lists array index keys first, whatever the order they are + // added in, so it cannot keep them after the required properties + const ordered = orderedKeys(schema) + const objectOrder = Object.keys(Object.fromEntries(ordered.map((key) => [key, null]))) + if (ordered.some((key, i) => key !== objectOrder[i])) return null const propertiesLocation = location.getPropertyLocation('properties') node.properties = Object.create(null) for (const key of keys) { diff --git a/test/object-mechanism-native.test.js b/test/object-mechanism-native.test.js index 55b7af05..583590c2 100644 --- a/test/object-mechanism-native.test.js +++ b/test/object-mechanism-native.test.js @@ -770,3 +770,22 @@ test('native mechanism: a serializer can be called again while it is serializing // after an error the serializer is not left in the "serializing" state t.assert.equal(nat({ a: '1', b: '2', c: '3', list: [{ x: '4' }] }), '{"a":"1","b":"2","c":"3","list":[{"x":"4"}]}') }) + +test('native mechanism: array index property names keep the order of the default mechanism', t => { + // An object lists array index keys first, so a projected object cannot put + // them after the required properties as the default mechanism does + const schemas = [ + { type: 'object', required: ['a'], properties: { a: { type: 'string' }, 2: { type: 'integer' } } }, + { type: 'array', items: { type: 'object', required: ['a'], properties: { 0: { type: 'string' }, a: { type: 'string' } } } }, + { type: 'object', required: ['1'], properties: { 1: { type: 'string' }, a: { type: 'string' } } } + ] + const inputs = [{ a: 'x', 0: 'y', 1: 'z', 2: 3 }, [{ a: 'x', 0: 'y' }, { a: 'z' }]] + t.plan(schemas.length * inputs.length + 1) + for (const schema of schemas) { + const nat = build(schema, { objectMechanism: 'native' }) + const def = build(schema) + for (const input of inputs) t.assert.deepStrictEqual(run(nat, input), run(def, input), JSON.stringify(schema)) + } + // the order is the same: the schema is still projected + t.assert.ok(build(schemas[2], { objectMechanism: 'native', mode: 'debug' }).code.includes('asStringValue')) +})