Skip to content

BigInt serialization error on Integer and Null union type #501

Open
@fauh45

Description

@fauh45

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.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.

{
  "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 (<anonymous>)
              at anonymous12 (eval at build (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fast-json-stringify/index.js:177:23), <anonymous>:76:50)
              at main (eval at build (/home/xxxxxx/xxxxxx/xxxxxx/node_modules/fast-json-stringify/index.js:177:23), <anonymous>: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.<anonymous> (/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

    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,
        });
      }
    );
  2. Call the endpoint

Expected Behavior

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions