Nest.js swagger模块-dto中的对象在swagger中不可见

Nest.js swagger模块-dto中的对象在swagger中不可见,swagger,nestjs,Swagger,Nestjs,我用swagger在nest js中实现了一个小应用程序,我有一列(postgresql)作为json对象(typeorm中的简单json类型),嵌套对象在swagger中不可见。我的代码是: @ApiModelProperty() @IsOptional() readonly foo: { boo: string[]; boo2: string; boo3: string; ..etc }; 在swagger中,我只有空对象的foo可见,是否可以使用swagger ne

我用swagger在nest js中实现了一个小应用程序,我有一列(postgresql)作为json对象(typeorm中的简单json类型),嵌套对象在swagger中不可见。我的代码是:

@ApiModelProperty()
@IsOptional()
  readonly foo: {
  boo: string[];
  boo2: string;
  boo3: string;
  ..etc
 };
在swagger中,我只有空对象的foo可见,是否可以使用swagger nest js模块使整个json对象可见

提前thx Karol

使用显式类型

export interface Foo {
  boo: string[];
  boo2: string;
  boo3: string;
  ..etc
}


不要创建/使用接口的创建子数据(如果需要,请使用导出或不导出),例如:


大摇大摆地申请上课

例如:

import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString } from 'class-validator';
import { Type } from 'class-transformer';

export class StickerRequest {

    @IsNotEmpty()
    @IsString()
    @ApiProperty({ example: 'sticker 01' })
    readonly name: string;

    @ApiPropertyOptional({ example: 'This is sticker description' })
    readonly description?: string;

    @ApiPropertyOptional({ example: 'ami-01, ami-02' })
    readonly tags?: string;

}

export class CollectionRequest {

  @ApiProperty({ example: 'Ami' })
  @IsNotEmpty()
  @IsString()
  readonly collectionName: string;

  @ApiPropertyOptional({ example: 'This is collection description' })
  readonly description?: string;

  @ApiProperty({ example: 'fffa73e4efca9245489e2bac' })
  @IsNotEmpty()
  @IsString()
  readonly author: string;

  @ApiProperty({ type: StickerRequest })
  @IsNotEmpty()
  @IsArray()
  @Type(() => StickerRequest)
  stickers: [StickerRequest];

}

我相信您使用的是旧版本的nestjs,因为
@ApiModelProperty
现在被称为
@ApiProperty
。 我建议您将nestjs和swagger升级到其最新版本,并遵循以下步骤,这对我来说非常有效:


希望能有所帮助。

这对我来说是不必要的。斯威格正确地使用/显示了东西,但没有提及类型。什么?不应该用答案回答问题吗?但是用什么呢?另一个问题?这个问题与NestJS的哪个版本有关?还有哪个版本的NestJS/Swagger?
export class SubDto {
    @ApiModelProperty({ type: String })
    @IsString()
    readonly subStringOne: string;

    @ApiModelProperty({ type: String })
    @IsString()
    readonly subStrinTwo: string;
}

export class MainDto {

    @ApiModelProperty({ type: String })
    @IsString()
    readonly mainStringOne: string;

    @ApiModelProperty({ type: [SubDto] })
    @IsArray()
    readonly mainArray: SubDto[];

    // or do the same thing for objects
    @ApiModelProperty({ type: SubDto })
    @IsJSON() // @IsOject doesn't exist in Nestjs so I use @IsJSON().
    readonly mainObject: SubDto;
}
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString } from 'class-validator';
import { Type } from 'class-transformer';

export class StickerRequest {

    @IsNotEmpty()
    @IsString()
    @ApiProperty({ example: 'sticker 01' })
    readonly name: string;

    @ApiPropertyOptional({ example: 'This is sticker description' })
    readonly description?: string;

    @ApiPropertyOptional({ example: 'ami-01, ami-02' })
    readonly tags?: string;

}

export class CollectionRequest {

  @ApiProperty({ example: 'Ami' })
  @IsNotEmpty()
  @IsString()
  readonly collectionName: string;

  @ApiPropertyOptional({ example: 'This is collection description' })
  readonly description?: string;

  @ApiProperty({ example: 'fffa73e4efca9245489e2bac' })
  @IsNotEmpty()
  @IsString()
  readonly author: string;

  @ApiProperty({ type: StickerRequest })
  @IsNotEmpty()
  @IsArray()
  @Type(() => StickerRequest)
  stickers: [StickerRequest];

}