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.

Decorator APIProduces doesn't work

See original GitHub issue

I’m submitting a Potential bug


[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request

Current behavior

I use nestjs/swagger with swagger-ui-express. I set the decorator @ApiProduces("text/csv") but nothing is displayed in Swagger-ui. I check the produced specs using F12 on Swagger-UI and nothing, in the specs, involved “Produces”.

Expected behavior

The user can choose the Content-Type of the Response in Swagger-UI. As shown on the image below. This image is generated with YAML. image

The GET attribute has :

produces:
        - application/json
        - text/csv

Minimal reproduction of the problem with instructions

Insert this function in a controller.

@Get('thing')
@ApiProduces("text/csv")
// @ApiProduces("text/csv", "application/json")
public test(): any {
	return {
		id: "id",
		name: "name"
	};
}

Environment

Last version.


"@nestjs/common": "^6.7.2",
"@nestjs/config": "0.0.9",
"@nestjs/core": "^6.7.2",
"@nestjs/platform-express": "^6.7.2",
"@nestjs/swagger": "^4.4.0",
"swagger-ui-express": "^4.1.4"
 
For Tooling issues:
- Platform: Windows

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
VaultDevelopercommented, Mar 30, 2020

@zachgoll I found a way.

@Get(':id')
@ApiProduces("application/json", "application/vnd.api+json")
@ApiCreatedResponse({
	type: MyModel
})
public getModel(@Param('id') psModelId: string): MyModel {
	return {
		id: "123456789-12",
		body: ""
	};
}

MyModel is a classic Typescript class containing attribute id and body.

export class MyModel {
	@ApiProperty()
	id: string;

	@ApiProperty()
	body: string;
}

Result.

image

3reactions
CodalReefcommented, Jan 13, 2022

I made a tweak to make this work for Swagger 3 with the JS Codegen tool:

  @ApiProduces('image/png')
  @ApiResponse({
    status: 200,
    schema: {
      type: 'string',
      format: 'binary',
    },
  })

The Swagger JSON produced looks like this:

{
"responses":{"200":{"description":"","content":{"image/png":{"schema":{"type":"string","format":"binary"}}}}}
}

Which matches the Swagger 3 Docs for Binary Files: https://swagger.io/docs/specification/basic-structure/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Specify the return content type of a Controller - Stack Overflow
I found out the solution is to import ApiProduces from '@nestjs/swagger' like so: import { ApiOperation, ApiResponse, ApiUseTags, ...
Read more >
OpenAPI (Swagger) | NestJS - A progressive Node.js framework
While the application is running, open your browser and navigate to http://localhost:3000/api . You should see the Swagger UI.
Read more >
Downloading files with NestJS - notiz.dev
The decorators for the job are @ApiResponse and @ApiProduces . ... Websites and APIs and develop Open Source tools.
Read more >
Describing Responses - Swagger
Note that an API specification does not necessarily need to cover all ... To indicate the response body is empty, do not specify...
Read more >
OpenAPI (Swagger) - 《Nest.js v7.0 Document》 - 书栈网
Let's open the browser and verify the generated CreateCatDto model: OpenAPI (Swagger) - 图3. In addition, the @ApiProperty() decorator ...
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