#@ApiBody() support for multiple @ApiConsumes()

7 messages · Page 1 of 1 (latest)

halcyon karma
#

Hi everyone!

I have a POST endpoint that is able to receive `application/json' and 'application/gzip' in the body (and it works), but the OpenAPI request body does not know how to match them to the correct type.

This is an example:

class Payload { content: string; }

@Controller({
  path: 'test',
  version: '1',
})
export class TestController {
  @Post('/maybe-zip')
  @ApiOperation({ summary: 'Test controller', })
  @ApiBody({ type: Payload || Buffer })
  @ApiConsumes('application/json', 'application/gzip')
  async test(@Body() body: Payload | Buffer): Promise<void> {
    // content
  }
}

The output OpenAPI json:

{
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "$ref": "#/components/schemas/Payload"
        }
      },
      "application/gzip": {
        "schema": {
          "$ref": "#/components/schemas/Payload"
        }
      }
    }
  }
}

Is there a way to match application/gzip to Buffer?

fair scroll
#
@ApiBody({ type: Payload || Buffer })

This evaluates to only Payload btw

#

I think the only ways to distinguish the payloads and assign the correct one to the correct mime type is to use @ApiOperaton

#

and put everything in there

#

Or actually @ApiResponse is the correct one

#

you might need to put @ApiExtraModels(SomeDto) on top of the controller as well