swagger icon indicating copy to clipboard operation
swagger copied to clipboard

@ApiProperty's example property being overridden

Open uPaymeiFixit opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Current behavior

I have a ParentDto which has a property of type ChildDto. Both DTOs have ApiProperty decorators with example properties on them. I would expect the example from ParentDto to override any of the examples in ChildDto, which is the case when the DTO is used with @Body, but when it's used with @Header, @Param, or @Query the child DTO examples are used instead.

@Papooch helped me find a workaround by adding type: 'string' to the ParentDto. But I'm still wondering, is this expected behavior? If so, why?

import { Body, Controller, Get, Headers, Param, Query } from '@nestjs/common';
import { AppService } from './app.service';
import { ApiProperty } from '@nestjs/swagger';

class ChildDto {
  @ApiProperty({
    example: 'child DTO example 1',
  })
  childKey1: string;

  @ApiProperty({
    example: 'child DTO example 2',
  })
  childKey2: string;
}

class ParentDto {
  @ApiProperty({
    example: 'parent DTO example',
    // type: 'string', // This fixes the issue
  })
  parentKey: ChildDto;
}

@Controller()
export class HelloController {
  constructor(private readonly appService: AppService) {}

  @Get('/hello/world')
  getHello(
    @Param() params: ParentDto,
    @Query() query: ParentDto,
    @Headers() headers: ParentDto,
    @Body() body: ParentDto,
  ) {
    return this.appService.getHello();
  }
}

Image

Minimum reproduction code

Steps to reproduce

Expected behavior

All examples (@Body, @Header, @Param, or @Query) use the ParentDto example.

Package version

11.0.3

NestJS version

11.0.10

Node.js version

22.5.1

In which operating systems have you tested?

  • [x] macOS
  • [ ] Windows
  • [ ] Linux

Other

No response

uPaymeiFixit avatar Mar 06 '25 17:03 uPaymeiFixit