Repository navigation
fix: write null instead of undefined for values JSON cannot represent - #912
luantaraschi wants to merge 3 commits into
Conversation
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.
Tony133
left a comment
There was a problem hiding this comment.
Thanks for the fix, the change itself looks good. Two things before merging:
-
One path is still uncovered. With
additionalProperties: true, a value whosetoJSON()returnsundefinedstill produces invalid JSON:const stringify = build({ type: 'object', additionalProperties: true }) stringify({ x: { toJSON () { return undefined } }, y: 1 }) // => {"x":undefined,"y":1}
That branch in
buildExtraObjectPropertiesSerializercallsJSON.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?
-
Docs. For a declared property with an untyped schema,
{ x: () => {} }now serializes as{"x":null}, while nativeJSON.stringifygives{}and theadditionalProperties/patternPropertiespath skips functions and symbols. I'm fine withnull, 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.
|
Thanks, both done in b466fb1. The |
A schema without a
type({},true, an array withoutitems) and the extra items allowed byadditionalItems: trueare serialized withJSON.stringify(value). Forundefined, functions and symbols that call returnsundefined, and the generated code appends it to the output as the textundefined:{ 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}{}undefinedundefinednullNone of the main outputs parse as JSON. This PR falls back to
nullin those places, which is whatJSON.stringifywrites for the same values inside an array. For an object property,JSON.stringifywould drop the key instead, but here the key is already written when the value is serialized, sonullis what keeps the output valid. Properties set toundefinedare 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 (
=== nullto== null) and doesn't touch these lines, so the two don't overlap.The new tests in
test/any.test.jsfail on main (3 of them) and pass with the change.npm run lintandnpm run testpass (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
npm run test && npm run benchmark --if-presentand the Code of conduct