Skip to content

[Question] [typescript-fetch] The date format (RFC 3339: full-date) has a time zone gap when parsing from JSON and when converting to JSON #5932

Open
@mdoi2

Description

@mdoi2

target: typescript-fetch
openapi-generator version: 4.3.0

For example, if we generate from the definition of requestBody like this:

requestBody:
  required: true
  content:
    application/json:
      schema:
        title: RequestBody
        type: object
        properties:
          date:
            type: string
            format: date
            nullable: true

Such a code will be generated in the models directory:

export interface RequestBody {
  date?: Date | null;
}

export function RequestBodyFromJSON(json: any): RequestBody {
  return RequestBodyFromJSONTyped(json, false);
}

export function RequestBodyFromJSONTyped(json: any, ignoreDiscriminator: boolean): RequestBody {
  if ((json === undefined) || (json === null)) {
    return json;
  }
  return {
    'date': !exists(json, 'date') ? undefined : (json['date'] === null ? null : new Date(json['date'])),
  };
}

export function RequestBodyToJSON(value?: RequestBody | null): any {
  if (value === undefined) {
    return undefined;
  }
  if (value === null) {
    return null;
  }
  return {
    'date': value.date === undefined ? undefined : (value.date === null ? null : value.date.toISOString().substr(0,10)),
  };
}

Since the date format of OpenAPI conforms to RFC 3339 full-date, the time zone is not included in the original data string. ex: '2020-04-15'

When converting this original date string with the RequestBodyFromJSONTyped method, specify this string directly in the parameter of new Date, so it will be optimized in the local time zone.

I don't think this behavior is a problem.

But, when converting to JSON with the RequestBodyToJSON method, since it is converted to a string by toISOString, the UTC date is output.
With this, the value changes between conversion and restoration.

I think it should stringify with the date in the timezone of the Date instance.
For example:

`${value.date.getFullYear()}-${(value.date.getMonth() + 1).toString().padStart(2, '0')}-${value.date.getDate().toString().padStart(2, '0')}`

Is this my perception correct? Or am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions