繁体   English   中英

在顶层使用 oneOf / anyOf 时,Fastify 无法序列化响应模式

[英]Fastify fail to serialize response schema when using oneOf / anyOf at top level

这是我的路线:

fastify.route({
    method: "GET",
    url: "/foo/:foo_id",
    schema: {
        params: {
            foo_id: { type: "string" },
        },
        response: {
            200: {
                oneOf: [
                    { type: "string" },
                    { type: "number" },
                ],
            },
        },
    },
    handler: fooHandler,
})

当我尝试启动服务器时,我收到此错误消息:

{
    "code":"FST_ERR_SCH_SERIALIZATION_BUILD",
    "message":"Failed building the serialization schema for GET: /foo/:foo_id, due to error undefined unsupported",
    "statusCode":500,
    "stack":"FastifyError: Failed building the serialization schema for GET: /foo/:foo_id, due to error undefined unsupported
        at Boot.<anonymous> (/home/fooUser/repo/node_modules/fastify/lib/route.js:280:19)
        at Object.onceWrapper (events.js:421:28)
        at Boot.emit (events.js:327:22)
        at /home/fooUser/repo/node_modules/avvio/boot.js:153:12
        at /home/fooUser/repo/node_modules/avvio/plugin.js:269:7
        at done (/home/fooUser/repo/node_modules/avvio/plugin.js:201:5)
        at check (/home/fooUser/repo/node_modules/avvio/plugin.js:225:9)
        at internal/process/task_queues.js:153:7
        at AsyncResource.runInAsyncScope (async_hooks.js:186:9)
        at AsyncResource.runMicrotask (internal/process/task_queues.js:150:8)
        at processTicksAndRejections (internal/process/task_queues.js:97:5)"
}

我的架构定义中没有发现任何错误。 似乎它不仅适用于响应模式。 我为bodyfoo_id尝试了相同的模式,它工作正常:

params: {
    foo_id: {
        oneOf: [
            { type: "string" },
            { type: "number" },
        ],
    }
},
body: {
    oneOf: [
        { type: "string" },
        { type: "number" },
    ]
}

当我在响应模式中使用oneOf但在嵌套级别时,它也可以工作,如下所示:

response: {
    200: {
        type: "object",
        properties: {
            foo: {
                oneOf: [
                    { type: "string" },
                    { type: "number" },
                ],
            }
        }
    },
}

我不明白为什么我不能为 http 响应定义多个模式,这是没有意义的。

问题是fast-json-stringify不支持oneOf作为根对象:

const fastJson = require('fast-json-stringify')

const serial = fastJson({
  type: 'object',
  properties: {
    response: {
      oneOf: [
        { type: "string" },
        { type: "number" },
      ],
    }
  }
})

console.log(serial(5));
console.log(serial("5"));

所以你有几个解决方案:

  1. 向模块发送 PR 以添加此功能😀

  2. 将您的纯字符串/数字道具包装到一个对象中:

fastify.get('/', {
  schema: {
    response: {
      200: {
        type: 'object',
        properties: {
          response: {
            oneOf: [
              { type: "string" },
              { type: "number" },
            ],
          }
        }
      },
    },
  }
},...
  1. 在处理程序中为这些特殊情况编写自定义序列化程序:
const fastify = require('fastify')({ logger: true })
fastify.get('/', {
  serializerCompiler({ schema, method, url, httpStatus }) {
    return (responsePayload) => {
      if (typeof responsePayload === 'string') {
        return `"${responsePayload}"`
      } else {
        return `${responsePayload}`
      }
    }
  },
  schema: {
    response: {
      200: {
        oneOf: [
          { type: "string" },
          { type: "number" },
        ],
      }
    },
  },
  handler: async () => { return Math.random() >= 0.5 ? 5 : '5' }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM