16

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"

2 Answers 2

17

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

Sign up to request clarification or add additional context in comments.

Comments

6

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.