Skip to content

Duplicate DTOs are implicitly overriden which causes an incorrect schema #3398

@Newbie012

Description

@Newbie012

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When using the same class name in different dto files, only one of them is correctly set inside components while the others are ignored. Even if it's non-exported class module.

For instance:

// foo.dto.ts
export class CustomResponse {
  list: ListedPayload;
}

class ListedPayload {
  foo: string;
}
// bar.dto.ts
export class AnotherResponse {
  list: ListedPayload;
}

class ListedPayload {
  bar: string;
}

This will generate the following schema:

 "components": {
    "schemas": {
      "ListedPayload": {
        "type": "object",
        "properties": {
          "foo": {
            "type": "string"
          }
        },
        "required": [
          "foo"
        ]
      },
      "CustomResponse": {
        "type": "object",
        "properties": {
          "list": {
            "$ref": "#/components/schemas/ListedPayload"
          }
        },
        "required": [
          "list"
        ]
      },
      "AnotherResponse": {
        "type": "object",
        "properties": {
          "list": {
            "$ref": "#/components/schemas/ListedPayload"
          }
        },
        "required": [
          "list"
        ]
      }
    }
  }

as you can see, ListedPayload of AnotherResponse does not exist.

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-kl8vww1d?file=src%2Fapp.controller.ts

Steps to reproduce

  1. npm i & npm start
  2. go to /api/json
  3. see reproduction

Expected behavior

Either one of the following:

  1. If there's more than one dto with the same name, we could prefix it with the parent class name, so for the previous example we would have CustomResponse_ListedPayload and AnotherResponse_ListedPayload, but this will only work for child dtos.

  2. Print some warnings/errors about the existence of duplicate DTOs

Package version

11.1.2

NestJS version

11.0.2

Node.js version

20

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