Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorator APIProduces doesn't work #626

Closed
VaultDeveloper opened this issue Mar 24, 2020 · 7 comments
Closed

Decorator APIProduces doesn't work #626

VaultDeveloper opened this issue Mar 24, 2020 · 7 comments

Comments

@VaultDeveloper
Copy link

VaultDeveloper commented Mar 24, 2020

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
@kamilmysliwiec
Copy link
Member

Please, provide a minimal repository which reproduces your issue.

@VaultDeveloper
Copy link
Author

I created a repo: https://github.com/VaultDeveloper/SwaggerAPIProduces/tree/master
Install process in the readme :

git clone https://github.com/VaultDeveloper/SwaggerAPIProduces.git
cd repro
npm i
npm install --save reflect-metadata rxjs
npm run start

Impossible to check produced type.

image

@kamilmysliwiec
Copy link
Member

@VaultDeveloper https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md
The latest version of OpenAPI specification doesn't include produces key anymore.
Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@zachgoll
Copy link

@kamilmysliwiec I am running into this issue as well. The ApiProduces() and ApiConsumes() decorators work at the method level, but not the controller level as stated in the documentation.

I understand that the new OpenApi Spec does not include the key, but what should I be using to define the media type in the NestJS API?

Specifically, I am using the application/vnd.api+json media type from the JSON API spec.

Any guidance here would be appreciated.

@VaultDeveloper
Copy link
Author

VaultDeveloper commented Mar 30, 2020

@kamilmysliwiec thanks for your reply. Is there a way to downgrade the version of OpenAPI we are using ?

@VaultDeveloper
Copy link
Author

VaultDeveloper commented 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

@zuluana
Copy link

zuluana commented 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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants