【问题标题】:Nest.js swagger module - object in dto not visible in swaggerNest.js 招摇模块 - dto 中的对象在招摇中不可见
【发布时间】:2019-04-01 15:30:57
【问题描述】:

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

@ApiModelProperty()
@IsOptional()
  readonly foo: {
  boo: string[];
  boo2: string;
  boo3: string;
  ..etc
 };

在 swagger 中,我只有 foo 对空对象可见,是否可以使用 swagger nest js 模块使整个 json 对象可见?

提前谢谢 卡罗尔

【问题讨论】:

  • 这个问题与 NestJS 的哪个版本有关?还有哪个版本的 NestJS/Swagger?

标签: swagger nestjs


【解决方案1】:

使用显式类型

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

@ApiModelPropertyOptional({ type: Foo })
@IsOptional()
readonly foo: Foo;

【讨论】:

  • 什么?不应该用答案来回答问题吗?但是然后呢?另一个问题?
【解决方案2】:

不要创建/使用接口的创建 subDto(如果你想使用导出或不使用)例如:

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;
}

【讨论】:

  • 这对我来说是不必要的。 Swagger 正确使用/显示的东西,而没有提及类型。
【解决方案3】:

我相信您使用的是旧版本的 nestjs,因为 @ApiModelProperty 现在称为 @ApiProperty。 我建议您将 nestjs 和 swagger 升级到最新版本并按照以下步骤操作,对我来说效果很好:

https://docs.nestjs.com/recipes/swagger#openapi-swagger

希望有帮助。

【讨论】:

    【解决方案4】:

    改用类

    例子:

    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[];
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多