Swagger: [next] JSON model property returns array instead of object

Created on 28 Nov 2019  路  4Comments  路  Source: nestjs/swagger

I'm submitting a...


[x] Regression 
[ ] 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

Consider the following code:

import { Entity, Column, PrimaryGeneratedColumn, ManyToMany, JoinColumn, JoinTable } from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
import { User } from './user.entity';

@Entity('events')
export class Event {

    @PrimaryGeneratedColumn()
    @ApiProperty()
    id: number;

    @ManyToMany(type => User, user => user.events)
    @JoinTable()
    @ApiProperty({ type: [User] })
    users: User[];
}

This generates the following OAS3 output:

"Event": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          },
          "users": {
            "type": "array",
            "items": {
              "type": "array"
            }
          },
        },
        "required": [
          "id",
          "users",
        ]
      }

Expected behavior

We expect the following output:

"Event": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          },
          "users": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/User"
            }
          },
        },
        "required": [
          "id",
          "users",
        ]
      }

Minimal reproduction of the problem with instructions

Minimal repo that shows the issue: https://github.com/dennisameling/nest-swagger-example

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

Worked in previous NestJS/Swagger version

Environment


Nest version: 4.0.0-next.16


For Tooling issues:
- Node version: 10.16.3
- Platform:  Windows

Others:

Happy to provide a PR if you can confirm that this is indeed a regression that needs to be fixed

All 4 comments

You have a circular dependency between your entities. For that, you should use lazy type resolvers:

@ApiProperty({ type: () => [User] })

(notice arrow function)

Otherwise, we won't be able to infer your type at runtime

Thank you! Is it okay if I provide a PR in the https://github.com/nestjs/docs.nestjs.com repo to add some documentation for this?

https://docs.nestjs.com/recipes/swagger#working-with-arrays

Currently the docs mention the following:

@ApiModelProperty({ type: [String] })
readonly names: string[];

I could either

  1. Replace @ApiModelProperty({ type: [String] }) by @ApiModelProperty({ type: () => [String] }) in the docs, as the lazy type resolver should work in all cases (non-circular and circular dependencies), so that it's consistent
  2. Add an extra section about circular dependencies to the docs with @ApiModelProperty({ type: () => [String] })

If you could leave your preference here, I'll close this issue and reference it in my PR in https://github.com/nestjs/docs.nestjs.com

It's already covered here https://github.com/nestjs/docs.nestjs.com/pull/825 馃槃

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.

Was this page helpful?
0 / 5 - 0 ratings