swagger-api / swagger-api/swagger-codegen

[TypeScript] Configurable Date type mapping

Open
#5,141 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Feature request to be able to configure the Date type mapping to use string instead of Javascript Date.

As of v2.3.0, codegen is doing the correct default behavior of translating a swagger date-time to a Javascript Date object. However, we all know that JS Date is deeply flawed, only really working for UTC. To do real date and time work, a 3rd party library such as js-joda must be used. If the APIs take the ISO 8601 format on the wire and put them into a Date object though, information is already lost.

Swagger-codegen version

2.3.0

Related issues

#4812

Suggest a Fix

I'm not sure how to do configuration options. Currently, I'm forcing this with a modified source code:

@@ -42,7 +42,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
         modelTemplateFiles.put("model.mustache", ".ts");
         apiTemplateFiles.put("api.service.mustache", ".ts");
         languageSpecificPrimitives.add("Blob");
-        typeMapping.put("Date","Date");
+        typeMapping.put("Date","string");
         typeMapping.put("file","Blob");
         apiPackage = "api";
         modelPackage = "model";

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 in TypeScriptAngular2ClientCodegen at the typeMapping entry for Date, then inspect how this generator exposes configuration options. Define a configurable mapping while preserving Date as the default, and verify that selecting string changes generated model types without changing the default behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
api, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.