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

Problem genrated swagger for type anyof isArray

benjaballah opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I am in linux I use this versions

"@nestjs/common": "^10.0.2",
"@nestjs/core": "^10.0.2",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/swagger": "^7.1.1",

When I Add ApiProperty in my DTO

  @ApiProperty({
    description: 'children hierarchy',
    required: false,
    isArray: true,
    anyOf: [
      {
        $ref: getSchemaPath(CustomSectionDto),
      },
      {
        $ref: getSchemaPath(TextCustomFieldDto),
      },
      {
        $ref: getSchemaPath(TextAreaCustomFieldDto),
      },
    ],
  })

  children: (TextCustomFieldDto | TextAreaCustomFieldDto | CustomSectionDto)[];

I get This YAML

children:
  description: children hierarchy
  anyOf:
    - $ref: '#/components/schemas/CustomSectionDto'
    - $ref: '#/components/schemas/TextCustomFieldDto'
    - $ref: '#/components/schemas/TextAreaCustomFieldDto'
  items:
    type: array

But in reality should generated this YAML

children:
  description: children hierarchy
  type: array
  items:
    anyOf:
    - $ref: '#/components/schemas/CustomSectionDto'
    - $ref: '#/components/schemas/TextCustomFieldDto'
    - $ref: '#/components/schemas/TextAreaCustomFieldDto'

So I try with this code

@ApiProperty({
  description: 'children hierarchy',
  required: false,
  isArray: true,
  items: {
    anyOf: [
      {
        $ref: getSchemaPath(CustomSectionDto),
      },
      {
        $ref: getSchemaPath(TextCustomFieldDto),
      },
      {
        $ref: getSchemaPath(TextAreaCustomFieldDto),
      },
    ],
  },
})

I get this YAML

children:
  description: children hierarchy
  items:
    type: array
  type: array

Minimum reproduction code

#2564

Steps to reproduce

No response

Expected behavior

But in reality should generated this YAML

children:
  description: children hierarchy
  type: array
  items:
    anyOf:
    - $ref: '#/components/schemas/CustomSectionDto'
    - $ref: '#/components/schemas/TextCustomFieldDto'
    - $ref: '#/components/schemas/TextAreaCustomFieldDto'

Package version

^7.1.1

NestJS version

^10.0.2

Node.js version

18.16.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Would you like to create a PR for this issue?

I also had this issue and applying #2564 via patch-package fixed it

Let's track this here #2564