Karol Grabowski
Karol Grabowski

Reputation: 1471

Nest.js swagger module - object in dto not visible in swagger

I'm implementing small app in nest js with swagger, i have one column (postgresql) as json object (simple json type in typeorm) and the nested object is not visible in swagger. My code for it:

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

in swagger I have only foo visible with empty object, is it posible using swagger nest js module to make the whole json object visible?

thx in advance Karol

Upvotes: 1

Views: 10685

Answers (5)

Edward Casanova
Edward Casanova

Reputation: 954

If you are using docker with docker compose like I, to run a NestJs app avoid creating a bind mount on the ./dist folder.

Let's just stay it messes things up with dtos not showing up.

Unfortunately, you may have to rebuild the image everytime you want to see new changes on your dtos.

Upvotes: 0

Thanh Le
Thanh Le

Reputation: 31

Use a class instead

Example:

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 }) <------- Here
  @IsNotEmpty()
  @IsArray()
  @Type(() => StickerRequest)
  stickers: StickerRequest[];

}

Upvotes: 1

dtmiRRor
dtmiRRor

Reputation: 599

I believe you're using an older version of nestjs, since @ApiModelProperty is now called @ApiProperty. I suggest you to upgrade nestjs and swagger to their latest versions and follow these steps, worked for me quite well:

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

hope that helps.

Upvotes: 0

Siyanda Cele
Siyanda Cele

Reputation: 35

Do not create/use interface's create a subDto (use export or not if you want) e.g:

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

Upvotes: 0

Michael Trefzer
Michael Trefzer

Reputation: 21

Use an explicit type

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

and

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

Upvotes: 2

Related Questions