fix: serialize array input when object precedes array in multi-type schema#851
Open
spokodev wants to merge 1 commit into
Open
fix: serialize array input when object precedes array in multi-type schema#851spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
…i-type
A schema with type: ['object', 'array'] serialized array input as {},
dropping every element, because the object branch's typeof x === 'object'
guard matches arrays first and makes the trailing Array.isArray branch
unreachable. Exclude arrays from the object branch, mirroring the object
guard already used in buildArrayTypeCondition, so a sibling array type in
the same type list can match.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a schema lists
objectbeforearrayin itstypearray, array input serializes to{}and every element is lost.Steps to reproduce
Swapping the order to
type: ['array', 'object']produces the correct[1,2,3], so the bug is order dependent.Root cause
buildMultiTypeSerializeremits one branch per listed type in list order. The object branch is generated by thedefaultcase asif (typeof input === "object" || input === null). Becausetypeof [] === 'object', that guard captures array input first, which makes the laterelse if (Array.isArray(input))branch unreachable. The array is then run through the object serializer, which emits only declaredpropertiesand returns{}.Fix
Add an explicit
objectcase that excludes arrays from the object branch, mirroring the object guard already used inbuildArrayTypeCondition:A sibling
arraytype in the sametypelist can now match array input. Plain objects,toStringobjects for['object', 'string'], and['object', 'null']null handling are unchanged, since none of those are arrays.Authority
Round-trip invariant:
JSON.parse(serialize(x))must equal a schema-validx.[1,2,3]is valid for thearraymember yet serialized to{}, breaking the invariant.Tests
Added to
test/typesArray.test.js: array input undertype: ['object', 'array']serializes to[1,2,3](object input still serializes as an object), and a JSON:API-styledatafield withtype: ['object', 'array']round-trips an array of objects. Both fail on the current base (array serialized as{}) and pass with the fix.Full suite: 475/475 passing.