Unable to reference models in the definitions of the root swagger definition
Similar issues:
- https://github.com/fastify/help/issues/385 - Openapi. No answers
Also tried using external spec, but run into these issues:
- https://github.com/fastify/fastify-swagger/issues/286
- https://github.com/fastify/fastify-swagger/issues/337
💬 Question
I am trying to reference a definition from the root swagger config in my response and body schemas. I have modified a simple example based on the README and fastify-swagger examples, but trying to reference gives an error and wont validate.
module.exports = async function (fastify) {
fastify.register(require('fastify-swagger'), {
swagger: {
definitions: {
User: {
type: 'object',
required: ['id', 'email'],
properties: {
id: { type: 'string', format: 'uuid' },
firstName: { type: 'string' },
lastName: { type: 'string' },
email: { type: 'string', format: 'email' }
}
}
},
},
exposeRoute: true
});
fastify.get('/test', {
schema: {
response: {
200: {$ref: '#/definitions/User'},
}
}
}, (_req, _reply) => {
return { id: 'xx1234', firstName: 'test', lastName: 'test', email: 'test' };
});
}
FastifyError [Error]: Failed building the serialization schema for GET: /test, due to error Cannot find reference "definitions"
at Boot.<anonymous> (/private/tmp/fastify-demo/node_modules/fastify/lib/route.js:317:19)
at Object.onceWrapper (events.js:421:28)
at Boot.emit (events.js:327:22)
at /private/tmp/fastify-demo/node_modules/avvio/boot.js:153:12
at /private/tmp/fastify-demo/node_modules/avvio/plugin.js:269:7
at done (/private/tmp/fastify-demo/node_modules/avvio/plugin.js:201:5)
at check (/private/tmp/fastify-demo/node_modules/avvio/plugin.js:225:9)
at internal/process/task_queues.js:153:7
at AsyncResource.runInAsyncScope (async_hooks.js:189:9)
at AsyncResource.runMicrotask (internal/process/task_queues.js:150:8) {
code: 'FST_ERR_SCH_SERIALIZATION_BUILD',
statusCode: 500
}
In regular swagger spec this works fine, e.g. Java. But not sure if things need to be different through fastify.
I have tried adding the definitions as external schemas, and can get the schema to build using that, however then I run into problems because fastify-swagger nests definitions and causes swagger-ui errors (see 2 tickets in preamble).
This should be quite straight forward, and no need to external schemas?
Also tried various other methods of referencing the definitions, and trying to step through the code.
Your Environment
- node version: 14.4.0
- fastify version: ^3.0.0, fastify-swagger ^4.7.0
- os: Mac
I agree it would be really awesome to have a fix for this issue.
I see. I thought that it would be supported out of the box 😦
Is there an issue for this particular problem already open? I couldn't find to see anyone with the same problem
It's not written anywhere that the definitions specified in the config are available as shared schemas.
Any update on this? Would love to see this feature
Would you like to send a Pull Request to address this issue? Remember to add unit tests.
Using the buildLocalReference ref resolver per the docs worked for me - https://github.com/fastify/fastify-swagger/blob/ffde179acb12a1d53573fab31362025bd648a7fa/README.md#managing-your-refs