Skip to content

@nestjs/swagger v11.2.2 breaks with arrays of objects #3630

@HristoKolev

Description

@HristoKolev

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

With the following DTOs:

export class App {
  /**
   * Name of the app.
   * @example "example-app"
   */
  public name!: string;
}

export class AppDataResult {
  /**
   * Returned rows. Might be less than `total` if the data is being paged.
   */
  public items!: App[];

  /**
   * Total number of rows that match the filters.
   * Used when paging.
   * @example 100
   */
  public total!: number; //in case of paging this will show the total items count
}

and the following controller:

import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { AppService } from './app.service';
import { AppDataResult } from './dto/response.dto';

@ApiTags('App')
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  /**
   * Get data with items array and count
   * @returns Returns an object with items array and total count
   */
  @Get()
  getData(): AppDataResult {
    return this.appService.getData();
  }
}

@nestjs/swagger should output the following openapi definition:

{
  "openapi": "3.0.0",
  "paths": {
    "/": {
      "get": {
        "operationId": "AppController_getData",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AppDataResult"
                }
              }
            }
          }
        },
        "summary": "Get data with items array and count",
        "tags": [
          "App"
        ]
      }
    }
  },
  "info": {
    "title": "NestJS API",
    "description": "NestJS API with Swagger documentation",
    "version": "1.0",
    "contact": {

    }
  },
  "tags": [],
  "servers": [],
  "components": {
    "schemas": {
      "App": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "description": "Name of the app.",
            "example": "example-app"
          }
        },
        "required": [
          "name"
        ]
      },
      "AppDataResult": {
        "type": "object",
        "properties": {
          "items": {
            "description": "Returned rows. Might be less than `total` if the data is being paged.",
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/App"
            }
          },
          "total": {
            "type": "number",
            "description": "Total number of rows that match the filters.\nUsed when paging.",
            "example": 100
          }
        },
        "required": [
          "items",
          "total"
        ]
      }
    }
  }
}

and it did until 11.2.2 when it started generating the following definition:

{
  "openapi": "3.0.0",
  "paths": {
    "/": {
      "get": {
        "operationId": "AppController_getData",
        "parameters": [],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/AppDataResult"
                }
              }
            }
          }
        },
        "summary": "Get data with items array and count",
        "tags": [
          "App"
        ]
      }
    }
  },
  "info": {
    "title": "NestJS API",
    "description": "NestJS API with Swagger documentation",
    "version": "1.0",
    "contact": {

    }
  },
  "tags": [],
  "servers": [],
  "components": {
    "schemas": {
      "AppDataResult": {
        "type": "object",
        "properties": {
          "items": {
            "description": "Returned rows. Might be less than `total` if the data is being paged.",
            "allOf": [
              {
                "$ref": "#/components/schemas/"
              }
            ]
          },
          "total": {
            "type": "number",
            "description": "Total number of rows that match the filters.\nUsed when paging.",
            "example": 100
          }
        },
        "required": [
          "items",
          "total"
        ]
      }
    }
  }
}

notice the items property of AppDataResult.

Reproduction repository: https://github.com/HristoKolev/nestjs-swagger-array-issue/tree/main

Minimum reproduction code

https://github.com/HristoKolev/nestjs-swagger-array-issue/tree/main

Steps to reproduce

No response

Expected behavior

.

Package version

11.2.2

NestJS version

11.1.9

Node.js version

v20.19.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions