OpenApi Generator create models with type String instead of Date
Asked Answered
J

2

14

Im using OpenApi Generator to create services and models for my angular project.

This is an example of my Json:

"uploadDate" : {
              "type" : "string",
              "description" : "Data caricamento",
              "format" : "date-time"
            },

When I generate the files uploadDate is converted to String instead of Date.

See:

     /**
     * Data caricamento
     */
    uploadDate?: string;

Is there a way to add a configuration and create models with Date as a type where format is date-time?

This is the script that I use to launch the generation:

"generate": "openapi-generator-cli generate -g typescript-angular -i openapi.json -o ./generated-sources/client"
Johm answered 21/10, 2020 at 8:32 Comment(0)
J
15

Fixed by adding: --type-mappings=DateTime=Date

Johm answered 21/10, 2020 at 9:0 Comment(0)
P
4

You need to specify the type mapping: it lets you use alternative Date libraries. You either provide this argument to your command:

$ openapi-generator-cli generate -g typescript \
  --type-mappings=DateTime=Date \
  --config openapi.config.json

Or add it to your config JSON:

{
  // ...
  "typeMappings": {
    "DateTime": "Date"
  }
}

There is, however, an issue here: your models would say field: Date, but they will contain a string. OpenAPI-generator won't construct Date objects :(

See issues:

Perfumer answered 28/8, 2023 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.