swagger icon indicating copy to clipboard operation
swagger copied to clipboard

[Feature request] Array of oneOf (or isArray for oneOf)

Open PAStheLoD opened this issue 3 years ago • 3 comments

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

Our API accepts an array and the elements of said array can be of two types (described by the oneOf schema combinator).

Describe the solution you'd like

The ideal solution would be to allow specifying isArray for the oneOf combinator.

Teachability, documentation, adoption, migration strategy

Adding one example in the docs is probably sufficient, and it compiles with the teachability requirements. No migration needed.

What is the motivation / use case for changing the behavior?

No change to existing behavior, the proposed feature does not interfere with the existing ones.

PAStheLoD avatar Dec 15 '21 22:12 PAStheLoD

Hi, any progress with this?

Youmoo avatar Apr 27 '22 09:04 Youmoo

For the time being, there is a hack that we can temporarily use to enable array of oneOf until the feature is actually implemented.

@ApiProperty({
  type: 'array',
  allOf: [
     {
        type: 'array',
        items: {
          oneOf: [ ...$refs ]
        }
     }
  ],
})
someProperty: (DtoClass1 | DtoClass2 | DtoClass3)[];

tduyduc avatar May 13 '22 17:05 tduyduc

By reading the source code I think that it's supported with a slightly different syntax:

@ApiProperty({
    type: 'array',
    items: {
        oneOf: [ ...$refs ]
    }
})
someProperty: Dto[];

GuyGitzMagen avatar Aug 02 '22 11:08 GuyGitzMagen

Will this be implemented?

krzysztofMlczk avatar Mar 23 '23 15:03 krzysztofMlczk

https://github.com/nestjs/swagger/issues/1729#issuecomment-1202387103

kamilmysliwiec avatar Nov 08 '23 10:11 kamilmysliwiec