Skip to content

[BUG][typescript-fetch] Datetime in classes can't be null #21132

Description

@tikki100

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Currently, when a class is being generated, any dateTime values can not be null. It will replace the value with undefined instead. This means that you can not pass null as a value to any property that is part of a nullable class.

openapi-generator version

7.12.0

OpenAPI declaration file content or url

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml

Generation Details

cli command:

openapi-generator generate -i openapi.yaml -g typescript-fetch -o src/generated-api --additional-properties=importFileExtension=.js

Or with docker:

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i https://github.com/ghraw/OpenAPITools/openapi-generator/refs/heads/master/modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml \
    -g typescript-fetch \
    -o /local/packages/test/src/generated-api

Possible offending file:

{{#isDateType}}
'{{baseName}}': {{^required}}value['{{name}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}(value['{{name}}']{{#isNullable}} as any{{/isNullable}}).toISOString().substring(0,10)),
{{/isDateType}}
{{#isDateTimeType}}
'{{baseName}}': {{^required}}value['{{name}}'] == null ? undefined : {{/required}}({{#required}}{{#isNullable}}value['{{name}}'] == null ? null : {{/isNullable}}{{/required}}(value['{{name}}']{{#isNullable}} as any{{/isNullable}}).toISOString()),
{{/isDateTimeType}}

Steps to reproduce

Generate a class with a dateTime property that is nullable:

        shipDate:
          type: string
          format: date-time
          nullable: true
Related issues/PRs

Might be related? #11307

Suggest a fix

Expected result (somewhat like seen here):

        'date_prop': value['dateProp'] === null ? null : ((value['dateProp'] as any)?.toISOString().substring(0,10)),
        'datetime_prop': value['datetimeProp'] === null ? null : ((value['datetimeProp'] as any)?.toISOString()),

Current result (as seen here)

        'date_prop': value['dateProp'] == null ? undefined : ((value['dateProp'] as any).toISOString().substring(0,10)),
        'datetime_prop': value['datetimeProp'] == null ? undefined : ((value['datetimeProp'] as any).toISOString()),

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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