OpenAPITools / OpenAPITools/openapi-generator

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

Open
#5,932 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated typescript-fetch model methods RequestBodyFromJSONTyped and RequestBodyToJSON, using the provided OpenAPI schema and date value to reproduce the round-trip behavior. Compare the original full-date string with the serialized result and review the generator logic that produces these methods; done requires an agreed, tested handling of dates without time zones.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.