question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array of multiple types in response not working

See original GitHub issue

Version : “@nestjs/swagger”: “^2.5.1”

I’m trying to print out an API response with a list of various object types. I’m also trying to use class-transformer with discriminators but swagger prints out [{}]

export class GetPageDto extends WeakCreatePageDto {
  @ApiModelPropertyOptional({
    isArray: true,
  })
  @IsOptional()
  @IsArray()
  @Type(() => BaseBlockDto, {
    discriminator: {
        property: 'type',
        subTypes: [
            { value: GetBannerDto, name: 'banner' },
            { value: GetCampaignDto, name: 'campaign' },
        ],
    },
  })
  readonly blockList: GetBannerDto | GetCampaignDto;
}

Is there a way to define an array of multiple types ?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
arekbalcommented, May 14, 2019

Same observation today. @ApiModelProperty({ isArray: true, type: [FooDto] }) readonly items: FooDto[] leads to type being empty in .json.

EDIT: Got this working… Apparently out of 5 known (to me) possibilities:

  1. @ApiModelProperty({ type: [FooDto] }) readonly items: FooDto[]
  2. @ApiModelProperty({ isArray: true, type: FooDto }) readonly items: FooDto[]
  3. @ApiModelProperty({ isArray: true }) readonly items: FooDto[]
  4. @ApiModelProperty({ isArray: true, type: [FooDto] }) readonly items: FooDto[]
  5. @ApiModelProperty() readonly items: FooDto[] only first 2 work.

It is closest to what is described in the ‘quickstart’ as well. So I would not dare to say ‘docs’ are wrong… but still… it could be a little confusing. 😉

0reactions
lock[bot]commented, Jun 24, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Returning decoded data of multiple types not working
I am downloading data from Firebase and storing into UserDefaults. I am encoding and decoding it properly, and I can retrieve the data...
Read more >
Solved: Can You Define a Response Consisting of an Array W...
Solved: Hi! It is easy to define a response that consists of an array made up of several objects. Is it possible to...
Read more >
Arrays | Elasticsearch Guide [8.5] | Elastic
Field data types ... Arrays of objects do not work as you would expect: you cannot query each object independently of the other...
Read more >
Returning values - Manual - PHP
A function can not return multiple values, but similar results can be obtained by returning an array. Example #2 Returning an array to...
Read more >
How to extract data from array within Response Body
The answers above will work fine if you are sure that you will always get only one object in that array. So be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found