When `asString` receives `undefined` an exception is thrown

Ouverte
#680 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 2 jours

@cpruijsen y travaille déjà.

Depuis le 10/10/2026.

  • #893 par @cpruijsen — ouverte

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
35/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
javascript, node.js
Domaine
api, backend

Piste de recherche

L’échec est signalé dans lib/serializer.js, dans Serializer.asString vers les lignes 103-113 ; commencez par là et examinez l’issue et la pull request associées pour déterminer la sémantique prévue de null et undefined. Reproduisez le cas de réponse Fastify, ajoutez une couverture pour le comportement choisi et vérifiez que la réponse sous forme de tableau ne lève plus d’erreur.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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

Plugin version

5.9.1

Node.js version

20.2.0

Operating system

Linux

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

Ubuntu 22.04.1

Description

Not entirely sure this is a bug, could be intended behavior.

I have an array of stings, however one of the elements is undefined.

['test1', 'test2', 'test3', undefined]

If I return that from a fastify endpoint, asString throws an exception:

TypeError: Cannot read properties of undefined (reading 'toString')
    at Serializer.asString (/repo/node_modules/fast-json-stringify/lib/serializer.js:113:19)
    at anonymous1 (eval at build (/repo/node_modules/fast-json-stringify/index.js:190:23), <anonymous>:17:28)
    at anonymous0 (eval at build (/repo/node_modules/fast-json-stringify/index.js:190:23), <anonymous>:52:19)
    at serialize (/repo/node_modules/fastify/lib/reply.js:896:12)
    at preSerializationHookEnd (/repo/node_modules/fastify/lib/reply.js:516:17)
    at preSerializationHook (/repo/node_modules/fastify/lib/reply.js:500:5)
    at Reply.send (/repo/node_modules/fastify/lib/reply.js:204:7)
    at /repo/node_modules/fastify/lib/wrapThenable.js:25:15

Possibly related:
https://github.com/fastify/fast-json-stringify/issues/218
https://github.com/fastify/fast-json-stringify/pull/84

Steps to Reproduce
export default async (app: FastifyInstance) => {
  app.route({
    method: 'GET',
    url: '/test',
    schema: {
      response: {
        200: {
          type: 'object',
          properties: {
            status: { type: 'string' },
            message: { type: 'string' },
            testArray: {
              type: 'array',
              items: { type: 'string' }
            }
          }
        }
      }
    },
    handler: async () => {
      return {
        status: 'ok',
        message: 'This is a test route',
        testArray: ['test1', 'test2', 'test3', undefined]
      }
    }
  })
}
Expected Behavior

It seems that asString will default the input to an empty string if its null.

https://github.com/fastify/fast-json-stringify/blob/74c35c0ee4c2dd12cc8edf8acad4b2f46c0542bb/lib/serializer.js#L103

I can confirm this behavior by sending ['test1', 'test2', 'test3', null] instead. No error is thrown, and my response looks like the following:

{
    "status": "ok",
    "message": "This is a test route",
    "testArray": [
        "test1",
        "test2",
        "test3",
        ""
    ]
}

I'm thinking that asString should check for undefined as well. However I understand that this may technically be incorrect because my response schema defines an array of strings, not Array<string | undefined>.

Langage dominant
JavaScript
Étoiles
3.7k
Forks
245
Merge moyen
3 j 5 h
PR mergées (30 j)
16

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de fastify/fast-json-stringify

Toutes les issues de fastify/fast-json-stringify

Issues similaires

Plus d'issues JavaScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.