fix: do not let a string type swallow array input in multi-type schemas - #876
Open
chuanghiduoc wants to merge 1 commit into
Open
fix: do not let a string type swallow array input in multi-type schemas#876chuanghiduoc wants to merge 1 commit into
chuanghiduoc wants to merge 1 commit into
Conversation
A schema with type: ['string', 'array'] serialized array input as a comma-joined string, dropping the JSON structure, because the string branch's duck-typing check (typeof x === 'object' && x.toString !== Object.prototype.toString) also matches arrays and made the sibling array branch unreachable. Exclude arrays from that check, mirroring the object-branch guard added for the same class of bug in fastify#851.
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
stringbeforearrayin itstypearray, array input serializes to a comma-joined string and the JSON structure is lost.Steps to reproduce
Nested objects are destroyed the same way:
Swapping the order to
type: ['array', 'string']produces the correct output, so the bug is order dependent.Root cause
buildMultiTypeSerializeremits one branch per listed type, in list order. Thestringbranch accepts anything that quacks like a stringifiable object:An array satisfies all three (
Array.prototype.toString !== Object.prototype.toString), so array input is captured by the string branch and the laterelse if (Array.isArray(input))branch is unreachable.This is the same class of bug as #851 (object branch capturing arrays because
typeof [] === 'object'), which was fixed by excluding arrays from that branch.Fix
Exclude arrays from the string branch's duck-typing check, mirroring the object-branch guard from #851.
Date/RegExpand genuine custom-toStringobjects keep working, since they are matched by the earlierinstanceofchecks or still pass the (now array-excluded) object check.Tests
Added
multi-type [string, array] round-trips an arraytotest/typesArray.test.js, covering both the array input and a plain string input for the same schema. It fails onmain(fail 1) and passes with the fix.Full unit suite: 501 passing, 0 failing, coverage stays at 100%.