Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to reference models in the definitions of the root swagger definition #744

Closed
MetalMichael opened this issue May 3, 2021 · 6 comments
Labels
feature request New feature to be added good first issue Good for newcomers

Comments

@MetalMichael
Copy link

Similar issues:

Also tried using external spec, but run into these issues:

💬 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
@MetalMichael MetalMichael added the help wanted Help the community by contributing to this issue label May 3, 2021
@mcollina
Copy link
Member

mcollina commented May 3, 2021

I agree it would be really awesome to have a fix for this issue.

@MetalMichael
Copy link
Author

MetalMichael commented May 3, 2021

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

@mcollina
Copy link
Member

mcollina commented May 3, 2021

It's not written anywhere that the definitions specified in the config are available as shared schemas.

@gchagan36
Copy link

Any update on this? Would love to see this feature

@mcollina
Copy link
Member

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@Eomm Eomm transferred this issue from fastify/help Aug 1, 2023
@Eomm Eomm added feature request New feature to be added and removed help wanted Help the community by contributing to this issue labels Aug 1, 2023
@Uzlopak Uzlopak added the good first issue Good for newcomers label Aug 9, 2023
@ghost
Copy link

ghost commented Aug 28, 2023

Using the buildLocalReference ref resolver per the docs worked for me - https://github.com/fastify/fastify-swagger/blob/ffde179acb12a1d53573fab31362025bd648a7fa/README.md#managing-your-refs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature to be added good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants