question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Swagger docs for "Adding routes"

See original GitHub issue

I have this model

class User {
  name: string;
  friends: User[];
}

And I have created a crud controller to manage and get user without friends property. To get friends of a user, I have added an “adding route” :

  @Get(':id/friends')
  @UseInterceptors(CrudRequestInterceptor)
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

All works fine, but there is not swagger documentation for this route. To generate it, I have to add :

import { SerializeHelper } from '@nestjsx/crud/lib/crud/serialize.helper';
[...]
  @Get(':id/friends')
  @UseInterceptors(CrudRequestInterceptor)
  @ApiOperation({ summary: 'Get friends of a user' })
  @ApiParam({
    name: 'id',
    description: 'The id of the user',
    type: 'string',
  })
  @ApiOkResponse({
    schema: {
      oneOf: [
        { $ref: getSchemaPath(SerializeHelper.createGetManyDto(User, User.name)) },
        {
          type: 'array',
          items: { $ref: getSchemaPath(User) },
        },
      ],
    },
  })
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

Do you think we can simplify this declaration by exporting a helper to generate the GetManyDto reponse ?

Or, by adding a new @GetManyResponse decorator that ony generates the response swagger documentation :

  @Get(':id/friends')
  @UseInterceptors(CrudRequestInterceptor)
  @ApiOperation({ summary: 'Get friends of a user' })
  @ApiParam({
    name: 'id',
    description: 'The id of the user',
    type: 'string',
  })
  @GetManyResponse({type: User})
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

Or, by adding a new @GetMany decorator that generates the swagger documentation and the CrudRequestInterceptor interceptor :

  @Get(':id/friends')
  @GetMany({type: User})
  async getFriends(@ParsedRequest() req: CrudRequest) {
    return this.userService.getFriends(req);
  }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
mkolbuszcommented, Aug 26, 2020

+1 It would be grateful to create a custom GET route and have already generated swagger documentation with the filters, sorting, selecting, etc. Waiting for the update!

4reactions
yab94commented, Jul 28, 2020

+1 This would be great to have the swagger doc for custom routes when we used CrudRequest in it !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Paths and Operations - Swagger
API paths and operations are defined in the global paths section of the API specification. ... All paths are relative to the API...
Read more >
Adding custom baucis routes to its generated swagger api
I would like to add my "/myroute/:id' to the generated swagger api. PUT /Users/myroute/{id} description. Does anyone know how to do about this?...
Read more >
swagger:route · GitBook - Goswagger.Io
A swagger:route annotation links a path to a method. This operation gets a unique id, which is used in various places as method...
Read more >
swagger-routes-express - npm
Connect Express route controllers to restful paths using a Swagger 2 or OpenAPI 3 definition file. Latest version: 3.3.2, last published: 5 ...
Read more >
Documenting Express.js API with Swagger - LogRocket Blog
You can customize Swagger UI by implementing a custom CSS into Swagger integration. To do that, add customCssUrl option into swaggerUi.setup :
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found