4

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?

1 Answer 1

14

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 {
        //...
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.