nestia icon indicating copy to clipboard operation
nestia copied to clipboard

Handling Dates

Open AtlasRW opened this issue 3 years ago • 1 comments

Feature Request

Hi everyone !

While trying to generate OpenAPI/Swagger docs using classes with native JS Date attributes, Nestia converts it to empty objects in the Swagger output. I don't understand well how Nestia understands types yet, but there must be a way to convert Date attributes to Swagger's standard date or date-time.

Currently, Date attributes in classes/interfaces give this result in the Swagger output

"Date": {
    "type": "object",
    "properties": {},
    "nullable": false,
    "description": "Enables basic storage and retrieval of dates and times."
},

In the OpenAPI specs, the standard way to handle dates is the following

"Date": {
    "type": "string",
    "format": "date-time", // date | date-time
    "nullable": false,
    "description": "Enables basic storage and retrieval of dates and times."
},

You can find more details in Swagger's documentation https://swagger.io/docs/specification/data-models/data-types/#string

Nestia Version used : 3.0.16

AtlasRW avatar Oct 17 '22 16:10 AtlasRW

Define DTO like below:

interface IMember {
    /**
     * @format date
     */
    birthdate: string;

    /**
     * @format date-time
     */
    joined_at: string;
}

samchon avatar Oct 19 '22 01:10 samchon

Hello everyone!

Commenting here because I have a related issue with Date handling when generating the swagger.json file.

We use NestJS payload object transformation (https://docs.nestjs.com/techniques/validation#transform-payload-objects). For example, a DTO would look like this:

import { IsNotEmpty } from 'class-validator';

export class MyObjectCreateDTO {
   /**
   * Start date
   * @example 21/12/2022
   */
  @IsNotEmpty() dateStart!: Date;
}

The Date is handled automatically by NestJS which is transforming the string received in the payload to a Date object. I tried to adding @format date-time but then I get the following error:

Error on typia.MetadataTagFactory.generate(): format requires string type, but no "MyObjectCreateDTO.dateStart"

Is there any way to override this validation and force the swagger generation to ignore the Date object type?

Thank you very much!

antyale avatar Dec 23 '22 13:12 antyale

Now no problem through update of typia.

samchon avatar Mar 16 '23 17:03 samchon