Skip to content

fix: keep array index keys in default order with objectMechanism 'native' - #941

Open
yuyongyan29-dev wants to merge 2 commits into
fastify:mainfrom
yuyongyan29-dev:fix/native-index-key-order
Open

yuyongyan29-dev wants to merge 2 commits into
fastify:mainfrom
yuyongyan29-dev:fix/native-index-key-order

Conversation

@yuyongyan29-dev

Copy link
Copy Markdown

With objectMechanism: 'native', a required property next to an array index key comes out in a different order than the default codegen:

const schema = { type: 'object', required: ['a'], properties: { a: { type: 'string' }, 2: { type: 'integer' } } }
// default: {"a":"x","2":3}
// native:  {"2":3,"a":"x"}   (Node 26)

The scratch object always lists index keys first. Such schemas now fall back to the default codegen.

  • npm run lint and npm test pass (100% coverage, Node 24 and 26)
  • test added

yuyongyan29-dev and others added 2 commits November 27, 2026 00:04
…ive'

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.
Signed-off-by: Antonio Tripodi <Tony133@users.noreply.github.com>
@mcollina
mcollina requested review from Tony133 and gurgunday and removed request for Tony133 November 27, 2026 14:47

@Tony133 Tony133 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Tony133

Tony133 commented Nov 27, 2026

Copy link
Copy Markdown
Member

I resolved the conflict with main after #940 was merged: the check on MAX_PROPERTIES and the new check on the key order are both kept. No other change.

I reproduced the problem on main with Node.js v26.13.0 and confirmed that the output is the same as the default mechanism with this fix.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants