nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.68k stars 465 forks source link

[@nestjs/swagger v7] oneOf doesn't work as expected #3045

Open karlismelderis-mckinsey opened 3 weeks ago

karlismelderis-mckinsey commented 3 weeks ago

Did you read the migration guide?

Is there an existing issue that is already proposing this?

Potential Commit/PR that introduced the regression

No response

Versions

6 -> 7.4.0

Describe the regression

@ApiProperty({
        type: undefined,
        oneOf: [
          { type: 'array', items: { type: 'string' } },
          { type: 'array', items: { type: 'number' } },
          { type: 'array', items: { type: 'boolean' } },
        ],
        description: API_DESCRIPTION,
      })
values: string[] | number[] | boolean[]

this configuration used to produce this schema in v6

"values": {
  "oneOf": [
    {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    {
      "type": "array",
      "items": {
        "type": "number"
      }
    },
    {
      "type": "array",
      "items": {
        "type": "boolean"
      }
    }
  ]
}

in v7 for some reason type: array is added and oneOf suddenly defines items not schema of "values"

Minimum reproduction code

@ApiProperty({
        type: undefined,
        oneOf: [
          { type: 'array', items: { type: 'string' } },
          { type: 'array', items: { type: 'number' } },
          { type: 'array', items: { type: 'boolean' } },
        ],
        description: API_DESCRIPTION,
      })
values: string[] | number[] | boolean[]

Expected behavior

I expect to see this schema to be generated:

"values": {
  "oneOf": [
    {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    {
      "type": "array",
      "items": {
        "type": "number"
      }
    },
    {
      "type": "array",
      "items": {
        "type": "boolean"
      }
    }
  ]
}

Other

No response

karlismelderis-mckinsey commented 3 weeks ago

I think I found a workaround

type Values = string[] | number[] | boolean[]

@ApiProperty({
        type: undefined,
        oneOf: [
          { type: 'array', items: { type: 'string' } },
          { type: 'array', items: { type: 'number' } },
          { type: 'array', items: { type: 'boolean' } },
        ],
        description: API_DESCRIPTION,
      })
values: Values

it will do for us but behaviour is unexpected nevertheless