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

Swagger multiple document not working when use Nestjs Fastify #234

Closed
JackoPham opened this issue Apr 19, 2019 · 4 comments · Fixed by #427
Closed

Swagger multiple document not working when use Nestjs Fastify #234

JackoPham opened this issue Apr 19, 2019 · 4 comments · Fixed by #427

Comments

@JackoPham
Copy link

When I try to working Swagger multiple document with the nestjs + fastify , I had got error as below. Please kindly help me resolve it.

In the case use one document, it's work. It's not working when I add more than 1 document.

I use with versions.

    "@nestjs/common": "^6.1.1",
    "@nestjs/core": "^6.1.1",
    "@nestjs/graphql": "^6.0.5",
    "@nestjs/platform-fastify": "^6.1.1",
    "@nestjs/platform-socket.io": "^6.1.1",
    "@nestjs/swagger": "^3.0.2",
    "@nestjs/typeorm": "^6.0.0",
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';

const serverApp = new FastifyAdapter();
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    serverApp
  );
  const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();

  const catDocument = SwaggerModule.createDocument(app, options, {
    include: [SystemModule],
  });
  SwaggerModule.setup('docs/cat', app, catDocument);

  const secondOptions = new DocumentBuilder()
    .setTitle('Dogs example')
    .setDescription('The dogs API description')
    .setVersion('1.0')
    .addTag('dogs')
    .build();

  const dogDocument = SwaggerModule.createDocument(app, secondOptions, {
    include: [UserModule],
  });
  SwaggerModule.setup('api/dogs', app, dogDocument);

  await app.listen(port);
======> With error.
/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/@nestjs/platform-fastify/node_modules/fastify/lib/decorate.js:17
    throw new FST_ERR_DEC_ALREADY_PRESENT(name)
    ^
FastifyError [FST_ERR_DEC_ALREADY_PRESENT]: FST_ERR_DEC_ALREADY_PRESENT: The decorator 'swagger' has already been added!
    at decorate (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/@nestjs/platform-fastify/node_modules/fastify/lib/decorate.js:17:11)
    at Object.decorateFastify [as decorate] (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/@nestjs/platform-fastify/node_modules/fastify/lib/decorate.js:35:3)
    at Object.module.exports [as static] (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/fastify-swagger/static.js:59:11)
    at fastifySwagger (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/fastify-swagger/index.js:15:19)
    at Plugin.exec (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/avvio/plugin.js:89:17)
    at Boot.loadPlugin (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/avvio/plugin.js:175:10)
    at Task.release (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/fastq/queue.js:127:16)
    at worked (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/fastq/queue.js:169:10)
    at toLoad.finish (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/avvio/plugin.js:178:7)
    at done (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/avvio/plugin.js:136:5)
    at check (/mnt/d/PROJECTS/PRIVATE/nestjs-fastify/node_modules/avvio/plugin.js:147:7)
    at process._tickCallback (internal/process/next_tick.js:61:11)
@nartc
Copy link
Contributor

nartc commented Apr 19, 2019

httpServer.register(loadPackage('fastify-swagger', 'SwaggerModule'), {

https://github.com/fastify/fastify-swagger/blob/c41d37cd4a2ceb6b1a667f3dd03f8ccf5f53e2bf/static.js#L59

I don't think there's anything we can do right now. Under the hood, fastify-swagger calls fastify.decorate('swagger',...) to register this swagger decorator so you can only have one document with Fastify as of the moment :)

Try raising this issue to fastify-swagger repo?

@JackoPham
Copy link
Author

swagger/lib/swagger-module.ts

Line 71 in 9bdad6c

httpServer.register(loadPackage('fastify-swagger', 'SwaggerModule'), {
https://github.com/fastify/fastify-swagger/blob/c41d37cd4a2ceb6b1a667f3dd03f8ccf5f53e2bf/static.js#L59

I don't think there's anything we can do right now. Under the hood, fastify-swagger calls fastify.decorate('swagger',...) to register this swagger decorator so you can only have one document with Fastify as of the moment :)

Try raising this issue to fastify-swagger repo?

Thank you for your quick answer. When I working with nest + express, It's ok. But when I change to Fastify I encountered this error and I think that was the cause of Nestjs.

@nartc
Copy link
Contributor

nartc commented Apr 20, 2019

No it’s not the cause of Nest. We use two different wrappers for Express (swagger-ui-express) and Fastify (fastify-swagger). Fastify Swagger sets up the decorator with a hardcoded “swagger” string under the hood, that prevents multiple swaggers to be registered with Fastify.

@lock
Copy link

lock bot commented Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants