fastify / fastify/fast-json-stringify

BigInt serialization error on Integer and Null union type

Open
#501 12 comments 4 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
3.7k
Forks
226
Avg merge
1d 59m
Merged PRs (30d)
3

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported

### Fastify version

4.3.0

### Plugin version

_No response_

### Node.js version

16.16.0

### Operating system

Linux

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

elementary OS 6.1 Jólnir (Ubuntu 20.04.3 LTS)

### Description

Using typebox as the schema generator, whenever I made a type on the schema where it is a union of Integer and Null, Fastify always return as such.

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

With the stacktrace of,

```
TypeError: Do not know how to serialize a BigInt
at JSON.stringify ()
at anonymous12 (eval at build (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fast-json-stringify/index.js:177:23), :76:50)
at main (eval at build (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fast-json-stringify/index.js:177:23), :6:15)
at serialize (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fastify/lib/reply.js:797:12)
at preserializeHookEnd (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fastify/lib/reply.js:478:17)
at preserializeHook (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fastify/lib/reply.js:462:5)
at Reply.send (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fastify/lib/reply.js:182:7)
at Object. (/home/xxxxxx/xxxxxx/xxxxxx/index.ts:74:30)
at preHandlerCallback (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fastify/lib/handleRequest.js:126:28)
at preValidationCallback (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fastify/lib/handleRequest.js:110:5)
```

### Steps to Reproduce

1. Create a endpoint (using Typebox v0.24.20) as such
```typescript
app.get(
"/",
{
schema: {
response: {
"200": Type.Object({
test2: Type.Union([Type.Integer(), Type.Null()]),
}),
},
},
},
async (req, res) => {
return res.status(200).send({
// @ts-expect-error No BigInt Type in Typebox
test2: 12n,
});
}
);
```

3. Call the endpoint

### Expected Behavior

The serializer should serialize as an integer if it's an BigInt, and null if it's not

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.