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.

Nest Swagger generates routes with undefined as response code.

See original GitHub issue

Bug Report

Current behavior

Adding the following decorator, where status is optional according to the TS typings, produces an open API spec with an undefined response code. This causes issues with the generated docs, as well as code generators that consume open API.

  @ApiResponse({
    description:
      'Updates a Cat',
  })

The open API spec generated looks something like this:

{
  "openapi": "3.0.0",
  "paths": {
    "/": {
      "get": {
        "responses": { "undefined": { "description": "Updates a Cat" } }
      }
    }
  }
}

Adding the correct status to the decorator seems the fix the issue.

Input Code

See this for a repro: https://codesandbox.io/s/trusting-wood-4gk83

See this for a live example, where the response “code” is undefined. https://4gk83.sse.codesandbox.io/api/#/default/AppController_getHello

Here is the link to the open API JSON: https://4gk83.sse.codesandbox.io/api-json

Expected behavior

Looking at the typings for ApiResponseOptions, it basically has the following signature (simplified a bit)

export interface ApiResponseMetadata extends Omit<ResponseObject, 'description'> {
    status?: number | 'default';
    type?: Type<unknown> | Function | [Function] | string;
    isArray?: boolean;
    description?: string;
}

Status is marked optional, which I would expect means that the status will be automatically inferred based on the type of decorator used at the controller level, like @Get(). It seems that in the response, the correct status code is being returned. It’s just the open API spec.

Environment


Nest version: 6.8.0, tested in a project at 7.0.0 as well.  


Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcdo29commented, Oct 20, 2020

@HMilbradt next time, would you mind creating a git repo? Easier to clone and play with 😃

The Swagger Module’s @ApiResponse() decorator doesn’t set the response code by default. The usage of @ApiResponse() is if you want to end up setting all of the values yourself. Otherwise, there’s things like the @ApiOkResponse() or @ApiCreatedResponse() decorators that allow you to set up parts of the response’s possible return values. The docs show the usages here. While thestatus is optional, there is no default value for it. @kamilmysliwiec should we possibly make the status a required value?

0reactions
kamilmysliwieccommented, Mar 18, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nest + Swagger not recognizing endpoints or parameters
My understanding is that methods will only fail to get unique operationId 's when they are not sufficiently unique: 2 methods with identical ......
Read more >
Describing Responses - Swagger
A response is defined by its HTTP status code and the data returned in the response body and/or headers. Here is a minimal...
Read more >
Exception filters | NestJS - A progressive Node.js framework
Exception filters. Nest comes with a built-in exceptions layer which is responsible for processing all unhandled exceptions across an application.
Read more >
Swagger PUT/POST body value issue
Without that wrapper (see Request JSON Schema examples below), the call fails. API Request C# Service Stack Class: [Route("/assets/{AdID}/ ...
Read more >
Nest.js Step-by-Step: Part 3 (Users and Authentication)
Use Nest.js in a User's Module create a user and locate them in the ... Depending on the status of registration, this route...
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