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.

Using API model property with an array of types does not display correctly

See original GitHub issue

I’m submitting a…


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Using API model property with an array of types does not display correctly

Expected behavior

An array of the object with the properties listed

Minimal reproduction of the problem with instructions

  @TreeChildren()
  @ApiModelProperty({
    description: 'Array of Child Conversations.',
    isArray: true,
    type: [Conversation],
  })
  @Field(/* istanbul ignore next */ _ => [Conversation])
  public children?: Conversation[];

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

Environment


Nest version: 7.1.5

 
For Tooling issues:
- Node version: 12.13.0
- Platform:  Mac

Others:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
iambpncommented, Jun 16, 2022

For anyone who is trying to generate schema of array property in dto, here is the solution.

@ApiProperty({
  isArray: true
  type: AnotherClass
})
arr: AnotherClass[];

This will automatically generate AnotherClass Schema.

1reaction
marcinjahncommented, Sep 2, 2022

I can’t get it to work, even with @iambpn’s suggested solution. Here’s my endpoint:

  @Get('cats')
  async cats(@Query() request: CatInput): Promise<CatDto> {
    return new Promise<CatDto>(() => {
        return {color: 'black', name: 'Abc'};
      }
    )
  }

I’d like CatInput to be properly rendered in Swagger It contains an array of objects called infos:

export class Info {
    public name: string;
    public color: string;
}

export class CatInput {
    public infos: Info[]
}

I’m using the nest swagger plugin. Here’re some of the options I tried:

  1. No decorators added
export class Info {
    public name: string;
    public color: string;
}

export class CatInput {
    public infos: Info[]
}

image

Swagger UI does not allows me to input multiple infos. Even the single one I provide is sent wrong, missing the name of the infos field.

  1. Added ApiProperty decorator as suggested by @iambpn
export class Info {
    public name: string;
    public color: string;
}

export class CatInput {
    @ApiProperty({isArray: true, type: Info})
    public infos: Info[]
}

image

I get exactly the same result as before. I cannot provide multiple infos.

  1. Different form of ApiProperty
export class Info {
    public name: string;
    public color: string;
}

export class CatInput {
    @ApiProperty({isArray: true, type: [Info]})
    public infos: Info[]
}

image

Starts to look good. However, when I try to add an item:

image

Something is clearly wrong there, the swagger JSON is:

"parameters": [
          {
            "name": "infos",
            "required": true,
            "in": "query",
            "schema": {
              "type": "array",
              "items": {
                "type": "array",
                "items": {
                  "type": "a"
                }
              }
            }
          }
        ],

The specification is definitely wrong. Where does it take the type: "a" from?


Am I missing something? @kamilmysliwiec, let me know if I should create a new issue for this, this one seemed very similar to mine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to describe a model in Swagger for an array with simple ...
The array is composed of 'stackoverflow' items. Each item is an object, that has name property. This is the correct answer. The key...
Read more >
How to model an array of different types of object
Hi,. I have a use case where I want to describe using Swagger 2.0 a callback API. This API will POST an array...
Read more >
Types and Parameters - OpenAPI - Documentation | NestJS
When the property is an array, we must manually indicate the array type ... SwaggerModule may not be able to properly generate model...
Read more >
Data Types - Swagger
OAS 3 This guide is for OpenAPI 3.0. Data Types. The data type of a schema is defined by the type keyword, for...
Read more >
How to Use the Fetch API (Correctly) - CODE Magazine
If you have everything installed correctly, you should get an array of JSON product objects displayed in the browser. Leave the Web API...
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