fastify/fast-json-stringify

BigInt Problem

conioX opened this issue · 2 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.0

Plugin version

4.2.0

Node.js version

14.00

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

Windows 11

Description

Hi Guys

I found a problem when use fastify with prisma.

when my prisma schema return BigInt fastify return this error

{"statusCode":500,"error":"Internal Server Error","message":"Do not know how to serialize a BigInt"}

when i search inside prisma issue i found this issue

prisma/studio#614

this issue not come from prisma but the problem come from JSON.stringify, i think fast-json-stringify need to handle this

Steps to Reproduce

  1. install fastify-cli
  2. install prisma
  3. create db with bigint schema

Expected Behavior

No response

BigInt does not support serialization to JSON.

We should handle BigInt according to the README.md.

| Instance | Serialized as |
| -------- | ---------------------------- |
| `Date` | `string` via `toISOString()` |
| `RegExp` | `string` |
| `BigInt` | `integer` via `toString` |

We need a reproducible code to demonstrate it is happen inside fast-json-stringify. BigInt actually tested with

'use strict'
const t = require('tap')
const test = t.test
const build = require('..')
test('render a bigint as JSON', (t) => {
t.plan(1)
const schema = {
title: 'bigint',
type: 'integer'
}
const stringify = build(schema)
const output = stringify(1615n)
t.equal(output, '1615')
})
test('render an object with a bigint as JSON', (t) => {
t.plan(1)
const schema = {
title: 'object with bigint',
type: 'object',
properties: {
id: {
type: 'integer'
}
}
}
const stringify = build(schema)
const output = stringify({
id: 1615n
})
t.equal(output, '{"id":1615}')
})
test('render an array with a bigint as JSON', (t) => {
t.plan(1)
const schema = {
title: 'array with bigint',
type: 'array',
items: {
type: 'integer'
}
}
const stringify = build(schema)
const output = stringify([1615n])
t.equal(output, '[1615]')
})
test('render an object with an additionalProperty of type bigint as JSON', (t) => {
t.plan(1)
const schema = {
title: 'object with bigint',
type: 'object',
additionalProperties: {
type: 'integer'
}
}
const stringify = build(schema)
const output = stringify({
num: 1615n
})
t.equal(output, '{"num":1615}')
})