openapi-generator: [JAVA] incorrect import mapping of DateTime

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");

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 2
  • Comments: 23 (12 by maintainers)

Most upvoted comments

As a workaround try this:

                            <configOptions>
                                <java8>true</java8>
                                <dateLibrary>custom</dateLibrary>
                            </configOptions>
                            <typeMappings>
                                <typeMapping>DateTime=Instant</typeMapping>
                                <typeMapping>Date=LocalDate</typeMapping>
                            </typeMappings>
                            <importMappings>
                                <importMapping>Instant=java.time.Instant</importMapping>
                                <importMapping>LocalDate=java.time.LocalDate</importMapping>
                            </importMappings>

so that’s not us. We should open an issue in swagger-parser

I get it : your OAS is wrong. The type of dt and ts should be string with format: date and format: date-time respectively. Now there remains 2 issues:

  • You have validateSpec=true so it should have failed the generation
  • When I generate with the maven-plugin after fixing the OAS, I get threetenbp imports instead of java.time ones. This doesn’t happen when generating from the CLI.

cc @jimschubert for the maven plugin