Specify the return content type of a Controller

Eric Lindsey

I have the following as a metrics.controller.ts file:

import { Controller, Get } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiUseTags, ApiModelProperty } from '@nestjs/swagger';
import { PrometheusService } from './prometheus.service';

@Controller('metrics')
@ApiUseTags('Misc')
export class MetricsController {
  constructor(private readonly prometheusService: PrometheusService) {}

  @Get('/')
  @ApiOperation({
    title: 'Prometheus metrics',
    description: 'Exposes default prometheus node metrics'
  })
  @ApiResponse({ status: 200, description: 'Prometheus metrics' })
  public getMetrics(): string {
    return this.prometheusService.getMetrics();
  }
}

However Swagger incorrectly indicates a Response content type of application/json, when the actual output is text/plain:

Screen shot of output

I have tried looking at the Swagger docs regarding describing responses, and the item I am looking for is the 'content' portion. However, that doc does not cover the TypeScript annotations. The Swagger Core 2.X documentation talks about an @Operation annotation, but I don't have a reference to that and I don't know where to get one--only @ApiOperation works.

The relevant version information from npm ls in the project directory is as follows:

+-- @nestjs/[email protected]
| +-- @nestjs/[email protected] deduped
| +-- @nestjs/[email protected] deduped
| +-- [email protected]
| +-- [email protected] deduped
| `-- [email protected]

What is the proper annotation to indicate a Response content type of text/plain?

Eric Lindsey

I found out the solution is to import ApiProduces from '@nestjs/swagger' like so:

import { ApiOperation, ApiResponse, ApiUseTags, ApiProduces } from '@nestjs/swagger'

//...

    @ApiProduces('text/plain')
    public getMetrics(): string {
        //...
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring controller adding charset to content type header for image return method

From Java

How to specify "nullable" return type with type hints

From Dev

How to specify "own type" as return type in Kotlin

From Dev

How to specify "nullable" return type with type hints

From Java

Specify return type in TypeScript arrow function

From Dev

How can I specify a return type for operator[]?

From Java

How to specify a return type if it is behind a private module?

From Dev

Is it possible to specify an optional return type and deduce it if unspecified?

From Dev

Is it possible to use a variable and not specify a return type in postgreSQL?

From Dev

Specify return type based on other template argument

From Dev

How to specify the return type of the method of an abstract class

From Dev

How can I specify a return type for operator[]?

From Dev

How to specify the return type of the method of an abstract class

From Dev

specify the return type without an intermediary variable

From Dev

specify return type for inline functions with flow

From Dev

In Scala is there a way to specify that the return type should match the type of the method caller?

From Dev

How to specify that an abstract method's return type is the subclass's type

From Dev

In Scala is there a way to specify that the return type should match the type of the method caller?

From Dev

return different data type without explicitly specify data type

From Dev

Spring controller: Content-Type for StreamingResponseBody

From Dev

Controller returning HttpResponseMessage not setting content type correctly

From Dev

MVC Controller Return Content vs Return Json Ajax

From Dev

API Controller Post method return type/value

From Dev

How do i specify spray Content-Type response header?

From Dev

Specify an enum as the content and also have an attribute in a single type in xsd?

From Dev

How to specify Content-Type of all responses once in API Blueprint?

From Dev

Do I need to specify the content type for encrypted string?

From Dev

How do i specify spray Content-Type response header?

From Dev

HttpClient does not return Content-Type

Related Related

  1. 1

    Spring controller adding charset to content type header for image return method

  2. 2

    How to specify "nullable" return type with type hints

  3. 3

    How to specify "own type" as return type in Kotlin

  4. 4

    How to specify "nullable" return type with type hints

  5. 5

    Specify return type in TypeScript arrow function

  6. 6

    How can I specify a return type for operator[]?

  7. 7

    How to specify a return type if it is behind a private module?

  8. 8

    Is it possible to specify an optional return type and deduce it if unspecified?

  9. 9

    Is it possible to use a variable and not specify a return type in postgreSQL?

  10. 10

    Specify return type based on other template argument

  11. 11

    How to specify the return type of the method of an abstract class

  12. 12

    How can I specify a return type for operator[]?

  13. 13

    How to specify the return type of the method of an abstract class

  14. 14

    specify the return type without an intermediary variable

  15. 15

    specify return type for inline functions with flow

  16. 16

    In Scala is there a way to specify that the return type should match the type of the method caller?

  17. 17

    How to specify that an abstract method's return type is the subclass's type

  18. 18

    In Scala is there a way to specify that the return type should match the type of the method caller?

  19. 19

    return different data type without explicitly specify data type

  20. 20

    Spring controller: Content-Type for StreamingResponseBody

  21. 21

    Controller returning HttpResponseMessage not setting content type correctly

  22. 22

    MVC Controller Return Content vs Return Json Ajax

  23. 23

    API Controller Post method return type/value

  24. 24

    How do i specify spray Content-Type response header?

  25. 25

    Specify an enum as the content and also have an attribute in a single type in xsd?

  26. 26

    How to specify Content-Type of all responses once in API Blueprint?

  27. 27

    Do I need to specify the content type for encrypted string?

  28. 28

    How do i specify spray Content-Type response header?

  29. 29

    HttpClient does not return Content-Type

HotTag

Archive