nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ApiQuery issue when using "content"

jjescof opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I was trying to use a Query parameter as a JSON object, so in order to describe it and support "nested objects or arrays" the official Open API documentation suggests using "content" instead of "schema" https://swagger.io/docs/specification/describing-parameters/#schema-vs-content

Now going to the NestJS Implementation, I use:

@ApiQuery({
      required: false,
      name: 'filter',
      description: 'Filter Query object',
      content: {
        'application/json': {
          schema: {
            format: 'json',
            type: 'object',
            items: {
              oneOf: [...],
            },
          },
          example: {
            name: { begins: 'gen' },
            createdAt: { gt: '2023-09-28T15:41:45.779Z' } },
        },
      },
    })

Everything goes well and it works as expected on Swagger Explorer serves by my NestJS application.

The problem arises when I take the JSON from https://localhost:3000/api-json to check the file in online tools like
https://editor.swagger.io/ or when I use the same file for openapi generation tool openapi-generator-cli.

Those tools report an error regarding the generated JSON file from NestJS, it is that content and schema should be mutually exclusive, but NestJS adds an empty schema Object in the definition:

image

As shown in the first snippet code, there is no schema in the ApiQuery Options, instead, content is added, but at generation time an empty schema is added.

I would like to help to solve this error but I'm not sure where to start checking for this, so any advice will be really appreciated.

Minimum reproduction code

Steps to reproduce

Add a ApiQuery decorator add Method level with content field instead of schema.

Expected behavior

When content is added to ApiQuery decorator it shouldn't add an empty schema object

Package version

7.1.12

NestJS version

9.1.1

Node.js version

18.15.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Would you like to create a PR for this issue?

I would like to create a PR but I don't understand where this tag is being written to the document, can you give me some insights, please?

@jjescof the point is in swagger-explorer.ts:

  private readonly metadataScanner = new MetadataScanner();
+ private readonly schemas?: Record<string, SchemaObject>;
- private readonly schemas: Record<string, SchemaObject> = {};
  private operationIdFactory: OperationIdFactory = (

but this fix create too many problems, why the schemas empty object gives you problems?

This is a duplicate of #1461.