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

feature: define api response description globally #884

Closed
razvanz opened this issue Aug 3, 2020 · 4 comments · May be fixed by #1612
Closed

feature: define api response description globally #884

razvanz opened this issue Aug 3, 2020 · 4 comments · May be fixed by #1612

Comments

@razvanz
Copy link

razvanz commented Aug 3, 2020

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Currently, there's no way of configuring exception responses globaly.

Expected behavior

It would be great to be able to define response documentation globally. Maybe in the same way auth is added:

const app = await NestFactory.create(AppModule)

const document = SwaggerModule.createDocument(
  app,
  new DocumentBuilder()
    .setTitle('Custom API')
    .addBearerAuth()
    .addResponse({
      status: 500,
      description: 'Internal server error'
    }) // Same type as @ApiResponse decorator: ApiResponseOptions
    .build()
)
SwaggerModule.setup('docs', app, document)

await app.listen(3000)

Minimal reproduction of the problem with instructions

N/A

What is the motivation / use case for changing the behavior?

Having the ability to define exception responses globally would remove a good amount of redundant code that now needs to be defined on every controller.

Environment


Nest version: 7.4.2
 
For Tooling issues:
- Node version: 14.4.0
- Platform: Linux

Others:
- @nestjs/swagger: 4.5.12
@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@razvanz
Copy link
Author

razvanz commented Aug 3, 2020

I can give it a try. Do you have any pointers in regard to how to work it in the current implementation?

@nartc
Copy link
Contributor

nartc commented Aug 24, 2020

@razvanz just off top of my head. What you want to do, at the end of the day, is to generate a correct swagger.json file.

  • mirror the current implementation for some of the existing calls like apiBearerAuth which modifies a specific part in the OpenAPI Specification.
  • you want to add the global responses to components portion of OpenAPI Specification
  • you want to check for the available global responses and add the responses to each path's response with $ref. Take a look at some of the explorers and scanners in the source code.

razvanz added a commit to razvanz/swagger that referenced this issue Aug 26, 2020
Enables defining global responses which are inherited by all server
routes.

fixes nestjs#884
@kamilmysliwiec
Copy link
Member

Let's track this here #933

razvanz added a commit to razvanz/swagger that referenced this issue Nov 24, 2020
Enables defining global responses which are inherited by all server
routes.

fixes nestjs#884
cooperate pushed a commit to cooperate/swagger that referenced this issue Apr 5, 2021
Enables defining global responses which are inherited by all server
routes.

fixes nestjs#884
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants