Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenApi Generator create models with type String instead of Date

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"
like image 526
FranaGrana Avatar asked Dec 31 '25 10:12

FranaGrana


2 Answers

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

like image 104
FranaGrana Avatar answered Jan 02 '26 05:01

FranaGrana


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:

  • https://github.com/OpenAPITools/openapi-generator/issues/926
  • https://github.com/OpenAPITools/openapi-generator/issues/1875
like image 32
kolypto Avatar answered Jan 02 '26 05:01

kolypto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!