OpenAPITools / OpenAPITools/openapi-generator

[JAVA] incorrect import mapping of DateTime

Open
#1,312 24 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Generation of java components fails when joda is not used due to incorrect mapping of imports

openapi-generator version

3.3.2-SNAPSHOT

OpenAPI declaration file content or url
---
components:
  schemas:
    Dummy:
      type: object
      properties:
        dt:
          type: date
        ts:
          type: date-time
Command line used for generation
Steps to reproduce

Generate with any Java generator with dateLibrary other then joda

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/pull/1313

Suggest a fix/enhancement
diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
index c119400db..15458ccb7 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java
@@ -486,7 +486,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
             typeMapping.put("date", "LocalDate");
             typeMapping.put("DateTime", "OffsetDateTime");
             importMapping.put("LocalDate", "org.threeten.bp.LocalDate");
-            importMapping.put("OffsetDateTime", "org.threeten.bp.OffsetDateTime");
+            importMapping.put("DateTime", "org.threeten.bp.OffsetDateTime");
         } else if ("joda".equals(dateLibrary)) {
             additionalProperties.put("joda", "true");
             typeMapping.put("date", "LocalDate");
@@ -500,10 +500,10 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
             importMapping.put("LocalDate", "java.time.LocalDate");
             if ("java8-localdatetime".equals(dateLibrary)) {
                 typeMapping.put("DateTime", "LocalDateTime");
-                importMapping.put("LocalDateTime", "java.time.LocalDateTime");
+                importMapping.put("DateTime", "java.time.LocalDateTime");
             } else {
                 typeMapping.put("DateTime", "OffsetDateTime");
-                importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
+                importMapping.put("DateTime", "java.time.OffsetDateTime");
             }
         } else if (dateLibrary.equals("legacy")) {
             additionalProperties.put("legacyDates", "true");

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 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java and review the dateLibrary type and import mappings. Reproduce generation with the provided schema using a non-joda Java date library, then verify that generated components use valid imports for date and date-time types across the affected libraries.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.