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.

Define multiple Output Formats (NEXT VERSION)

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

!!! Note that i am using the @next branch (4.0.0-next.3) !!!

Dear @kamilmysliwiec ,

i am trying to implement the new Nest Swagger Package with my current application.

In this context, the Open API v3 specs allows to define multiple media types (accept headers) the user can request. For example, i am building an API that allows for application/json and application/vnd.api+json ( the json:api specs) requests. However, the data format sent to and returned from my backend differs - depending on the requested type (i.e., the structure differs).

This behaviour is described in the official Open API documentation as well: https://swagger.io/docs/specification/describing-responses/

Current behavior

I found, that the @ApiResponse() decorator allows for defining this content attribute. However, I am not able to “link” this specific response format to a particular model (by class-name itself) but only via full-qualified reference string name.

Take, for example, the following fully-decorated method

  @Get('account')
  @ApiOperation({
    summary: 'My Account',
    description:
      'Returns the Account (based on JWT) for the currently active User',
  })
  @ApiResponse({
    content: {
      'application/json': {
        schema: { $ref: '#/components/schemas/HttpAccountEntity' },
      },
      'application/vnd.api+json': {
        schema: { $ref: '#/components/schemas/JsonAccountEntity' },
      },
    },
    description: 'Default Response',
    status: 200,
  })
  async myAccount() {
    // do something
  }

With this, i need to use the fully qualified schema reference name as string, because the Response.content.*.schema.ref is defined as string.

For the sake of consistency (and minimizing errors) i think, it would be better to define it like this (only relevant part):

  @ApiResponse({
    content: {
      'application/json': {
-        schema: { $ref: '#/components/schemas/HttpAccountEntity' },
+       schema: { $ref: HttpAccountEntity },
      },
      'application/vnd.api+json': {
-        schema: { $ref: '#/components/schemas/JsonAccountEntity' },
+       schema: { $ref: JsonAccountEntity },
      },
    },
    description: 'Default Response',
    status: 200,
  })

Please note that I am actually not aware, if the schema.$ref property is the correct way to specify my desired behavior. I noticed, that there is also a schema.type: string, however, i was not able to get this to work properly!

  @ApiResponse({
    content: {
      'application/json': {
        schema: { type: HttpAccountEntity },
      },
      // ...
    },
    description: 'Default Response',
    status: 200,
  })

Environment


Nest Core: 6.8.5
Nest Swagger: 4.0.0-next.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Dec 3, 2019

Take a look at this section in the documentation: https://docs.nestjs.com/recipes/swagger#extra-models

1reaction
nartccommented, Nov 4, 2019

I’m interested in working on a PR for this. Sounds reasonable and better DX.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Output to multiple formats - Help+Manual
You can generate output to multiple formats with the command line, either by calling publishing tasks defined in the Task Manager or by...
Read more >
Chapter 9 Multiple Output Formats | R Markdown Cookbook
One main advantage of R Markdown is that it can create multiple output formats from a single source, which could be one or...
Read more >
Multiple Output Formats — gcovr 5.2 documentation
You can write multiple report formats with one gcovr invocation by passing the output filename directly to the report format flag. If no...
Read more >
Specifying multiple simultaneous output formats in knitr
The solution is to change pdf_document to pdf_document: default . I can't unfortunately find a reference for this syntax in the official documentation....
Read more >
Mapping multiple output types for the same class - GraphQLite
Version: Next ... To do so, you need to create 2 output types for the same PHP class. ... First of all, we...
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