Skip to content

fix: write null instead of undefined for values JSON cannot represent - #912

Open
luantaraschi wants to merge 3 commits into
fastify:mainfrom
luantaraschi:fix/untyped-undefined-items
Open

luantaraschi wants to merge 3 commits into
fastify:mainfrom
luantaraschi:fix/untyped-undefined-items

Conversation

@luantaraschi

@luantaraschi luantaraschi commented Oct 30, 2026 •

Copy link
Copy Markdown

A schema without a type ({}, true, an array without items) and the extra items allowed by additionalItems: true are serialized with JSON.stringify(value). For undefined, functions and symbols that call returns undefined, and the generated code appends it to the output as the text undefined:

schema input main this PR
{ type: 'array', items: {} } [1, undefined, 2] [1,undefined,2] [1,null,2]
{ type: 'array', items: [{ type: 'string' }], additionalItems: true } ['a', undefined] ["a",undefined] ["a",null]
{ type: 'object', properties: { a: {} } } { a: () => {} } {"a":undefined} {"a":null}
{} undefined undefined null

None of the main outputs parse as JSON. This PR falls back to null in those places, which is what JSON.stringify writes for the same values inside an array. For an object property, JSON.stringify would drop the key instead, but here the key is already written when the value is serialized, so null is what keeps the output valid. Properties set to undefined are still skipped as before; that check runs earlier.

#869 mentions this gap in its "Regression surface" section and leaves it out of scope there. #893 is about the typed paths (=== null to == null) and doesn't touch these lines, so the two don't overlap.

The new tests in test/any.test.js fail on main (3 of them) and pass with the change. npm run lint and npm run test pass (664 tests, 100% coverage, tstyche).

On cost: a 100-item array with items: {} went from 4307 to 4362 ns/op (median of three runs, about 1%), and an object with three untyped properties stayed within run-to-run noise (124 to 128 ns/op on both branches).

Checklist

Schemas without a type (an empty schema, `true`, or an array without
`items`) and the extra items allowed by `additionalItems: true` are
serialized with `JSON.stringify(value)`. For `undefined`, functions and
symbols that call returns `undefined`, which was appended to the output
as the text `undefined`, so `[1, undefined]` with `{ type: 'array' }`
became `[1,undefined]`, which is not valid JSON.

Fall back to `null` in those places, which is what `JSON.stringify`
writes for such values inside arrays.

@mcollina mcollina 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

@mcollina mcollina added the semver-major Issue or PR that should land as semver major label Nov 18, 2026

@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.

Thanks for the fix, the change itself looks good. Two things before merging:

  1. One path is still uncovered. With additionalProperties: true, a value whose toJSON() returns undefined still produces invalid JSON:

    const stringify = build({ type: 'object', additionalProperties: true })
    stringify({ x: { toJSON () { return undefined } }, y: 1 })
    // => {"x":undefined,"y":1}

    That branch in buildExtraObjectPropertiesSerializer calls JSON.stringify(value) directly. The same fallback fixes it (I tried it locally: output becomes {"x":null,"y":1}, lint and tests still pass):

    json += asString(key) + JSON_STR_COLONS + (JSON.stringify(value) ?? JSON_STR_NULL)

    Could you add it here with a test?

  2. Docs. For a declared property with an untyped schema, { x: () => {} } now serializes as {"x":null}, while native JSON.stringify gives {} and the additionalProperties/patternProperties path skips functions and symbols. I'm fine with null, but it differs from both and this is semver-major, so please add a short note to the README.

With `additionalProperties: true` the value goes straight to
`JSON.stringify`, so an object whose `toJSON()` returns `undefined`
still came out as `{"x":undefined}`. Use the same `null` fallback there.

Also note in the README how values JSON cannot represent are written.
@luantaraschi

Copy link
Copy Markdown
Author

Thanks, both done in b466fb1. The additionalProperties: true branch uses the same ?? JSON_STR_NULL fallback now, with a test in test/additionalProperties.test.js that fails without it. The README note went under "Missing fields", since that section already explains what gets left out of an object.

@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

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

semver-major Issue or PR that should land as semver major

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants