jstrother
jstrother

Reputation: 358

NestJS / Swagger - Not Getting Complete Descriptions

I'm having issues getting Swagger to work correctly. I can get an example of the request body, but not the response or the swagger-json. The response shows as {} and the swagger-json is:

{
  statusCode: 404,
  message: "Cannot GET /swagger-json",
  error: "Not Found",
}

My nest-cli.json file is:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "plugins": ["@nestjs/swagger/plugin"]
  }
}

And in my main.ts I have:

import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
...
const options = new DocumentBuilder()
    .setTitle('ILuvCoffee')
    .setDescription('Coffee Application')
    .setVersion('1.0')
    .build();

  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

I'm doing the Nest.js course, and I have followed along pretty faithfully so far. When I hit this snag, I double-checked my code was correct, even copy/pasting. I also double-checked that my DTO files follow *.dto.ts and my Entity files *.entity.ts. But I still can't get Swagger to show anything more than the request. Thoughts? Who's seeing what I'm not?

Here is my repo for it if you'd like to take a deeper peek: https://github.com/jstrother/iluvcoffee

Thanks!

Upvotes: 2

Views: 7121

Answers (1)

eol
eol

Reputation: 24565

Looks like you did not specifify the actual response types in your controller, instead you're using <any>, e.g:

@Get(':id')
findOne(@Param('id') id: string): Promise<any> {
  console.log(id);
  return this.coffeesService.findOne(id);
}

Try changing those anys to the actual types

@Get(':id')
findOne(@Param('id') id: string): Promise<Coffee> {
  console.log(id);
  return this.coffeesService.findOne(id);
}

Note that you can also use the ApiResponse decorator to explicitely define responses -> check the official example for more details.

Upvotes: 3

Related Questions