fastify/fast-json-stringify

Get working when AdditionalProperties is empty object

SzHeJason opened this issue · 1 comments

Bug Report

sinclairzx81/typebox#55

To Reproduce

const Controller: FastifyPluginCallback = (server, options, next) => {
  server.post(
    '/:moduleName/:action',
    {
      schema: {
        response: {
          '2xx': {
            type: 'object',
            properties: {
              code: { type: 'number' },
              subcode: { type: 'number' },
              message: { type: 'string' },
              data: { type: 'object', additionalProperties: {} },
            },
            required: ['code', 'subcode', 'message', 'data'],
          },
        },
      },
    },
    async (request, reply) => {
      reply.send({
        code: 0,
        subcode: 0,
        message: 'ok',
        data: {
          a: '123',
        },
      })
    }
  )
  next()
}

Expected behavior

response data should can compile safely by docs

If additionalProperties is an object, that object is a schema that will be used to validate 
any additional properties not listed in properties.

For example, one can allow additional properties, but only if they are each a string:

{
  "type": "object",
  "properties": {
    "number":      { "type": "number" },
    "street_name": { "type": "string" },
    "street_type": { "type": "string",
                     "enum": ["Street", "Avenue", "Boulevard"]
                   }
  },
  "additionalProperties": { "type": "string" }
}

Your Environment

  • node version: 12
  • fastify version:3.13.0
  • os: Mac

Fixed by #294.