fix: serialize BigInt values in integer anyOf/oneOf and multi-type schemas - #875
Open
marko1olo wants to merge 1 commit into
Open
fix: serialize BigInt values in integer anyOf/oneOf and multi-type schemas#875marko1olo wants to merge 1 commit into
marko1olo wants to merge 1 commit into
Conversation
AJV does not recognise BigInt as type 'integer', so values like 12n were always falling through to the TypeError branch in anyOf/oneOf union code, and in multi-type (type array) serializers the Number.isInteger() guard also rejects BigInt. Two targeted changes: - buildMultiTypeSerializer: add ypeof input === 'bigint' alongside Number.isInteger() in the integer branch condition. - buildOneOf: when an option schema declares type 'integer', prefix the AJV validator.validate() call with a BigInt type-guard so BigInt values are correctly routed to asInteger() (which already handles BigInt). asInteger() in lib/serializer.js already converts BigInt to string via .toString(), so no change is needed there. Fixes fastify#501
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.
Problem
When a schema field is typed as
anyOf: [{type: 'integer'}, {type: 'null'}]ortype: ['integer', 'null'], passing aBigIntvalue (e.g.12n) throws an error instead of serializing it as an integer.This was reported in #501 where Typebox's
Type.Union([Type.Integer(), Type.Null()])produces exactly this schema shape.Root cause
Two code paths were affected:
buildMultiTypeSerializer(fortype: ['integer', 'null']): the branch condition wasNumber.isInteger(x) || x === null.Number.isInteger(12n)returnsfalse, so BigInt fell through.buildOneOf(foranyOf/oneOf): branch selection usesvalidator.validate()(AJV). AJV does not recognise BigInt astype: 'integer'by default, so BigInt never matched any branch.Note:
lib/serializer.jsasInteger()already handles BigInt correctly via.toString().Fix
buildMultiTypeSerializer: addtypeof input === 'bigint'to the integer guard.buildOneOf: when an option schema hastype: 'integer', prefix the AJV check withtypeof input === 'bigint' ||so BigInt values are routed toasInteger().Tests
Added 2 new test cases in
test/typesArray.test.jscovering both code paths. Full 501-test suite passes.