1

I'm trying to apply @ApiProperty({ format: "date-time" }) to a nested field in a class. If I do this this at the first level, this works fine. Same If I do it in a field nested within an object. But If I go a level deeper the formatting stops applying in the swagger documentation payload example:

 class testDto {
   @ApiProperty({ format: "date-time" })
   date: string //This works: "2022-09-29T15:28:15.931Z"

   @Type(() => Foo)
   @ValidateNested()
   foo: Foo;
}

 class Foo {
   @ApiProperty({ format: "date-time" })
   date: string //This works: "2022-09-29T15:28:15.931Z"

   @Type(() => Bar)
   @ValidateNested()
   bar: Bar;
}

 class Bar {
   @ApiProperty({ format: "date-time" })
   date: string //This does not work: "string"
}

This will result in a swagger example value that looks something like this:

"date": "2022-09-29T15:28:15.931Z",
"foo": {
    "date": "2022-09-29T15:28:15.931Z",
    "bar": {
      "date": "string"
    }
  }

Is there a way I can make this ApiProperty apply to my nested fields?

Progman
  • 16,827
  • 6
  • 33
  • 48

1 Answers1

-1

Solution: You have to specify the ApiProperty type for the parent property For[bar]

 class Foo {
   @ApiProperty({ format: "date-time" })
   date: string

   @Type(() => Bar)
   @ValidateNested()
   @ApiProperty({ type: Bar }) // Tell swagger the type for this property
   bar: Bar;
}

 class Bar {
   @ApiProperty({ format: "date-time" })
   date: string
}