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

Created on 25 Oct 2018  路  21Comments  路  Source: OpenAPITools/openapi-generator

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

All 21 comments

The type DateTime is mapped to another type (eg. OffsetDateTime) so I don't see why we need an import for DateTime. Do you have a failing example ?

Hi @cbornet

with this pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.openapitools</groupId>
  <artifactId>failing-date-time</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>3.3.2-SNAPSHOT</version>
        <executions>
          <execution>
            <id>Generate CRM</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>src/main/resources/dummy.yaml</inputSpec>
              <validateSpec>true</validateSpec>
              <generatorName>java</generatorName>
              <configOptions>
                <interfaceOnly>true</interfaceOnly>
                <library>webclient</library>
                <dateLibrary>java8</dateLibrary>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

and this specification:

---
openapi: 3.0.0
info:
  title: Dummy test
  version: 0.0.1
paths:
  "/dummy":
    get:
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/VersionResponse"
components:
  schemas:
    Dummy:
      type: object
      properties:
        dt:
          type: date
        ts:
          type: date-time

the generated model has this import section

import java.util.Objects;
import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.time.LocalDate;
import org.joda.time.*;

as you can see, OffsetDateTime is not correctly imported

the same happens for instance with jaxrs-spec generator

strange indeed, but this is blocking

What I find surprising is that you get joda and not threetenbp which is the default. Also from what I see in the code, specifying java8 for webclient shouldn't be necessary. What does it give if you remove <dateLibrary>java8</dateLibrary> ?

with dateLibrary removed i get:

import java.util.Objects;
import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.joda.time.*;
import org.threeten.bp.LocalDate;

When the CLI generator does not show this behaviour, could the issue be in the wiring of CodeGenMojo?

Oh, actually it's because your DateTime is not remapped to OffsetDateTime

i was thinking along the same path, but have not found where the remapping should happen or why it fails, the codebase is rather new to me

BTW you can remove interfaceOnly as it's not a java client option. And also java8 as Webclient sets java8 by default.

I reproduce your issue.

ok , that's good.

I had already remove java8 before, the interfaceOnly was indeed a heritage of trying with another generator.

With interfaceOnly removed i still get

import java.util.Objects;
import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.joda.time.*;
import org.threeten.bp.LocalDate;

POM used:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.openapitools</groupId>
  <artifactId>failing-date-time</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>3.3.2-SNAPSHOT</version>
        <executions>
          <execution>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>src/main/resources/dummy.yaml</inputSpec>
              <validateSpec>true</validateSpec>
              <generatorName>java</generatorName>
              <configOptions>
                <library>webclient</library>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

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

@guidoschreuder you can workaround the second issue by forcing dateLibrary=java8

@cbornet thank you, i totally missed that issue in the specification, good catch

can't believe this had me stumped for so long

generation indeed now works with java8 with

        dt:
          type: string
          format: date
        ts:
          type: string
          format: date-time

i've closed the PR

About the spec validation, the issue is also present with the CLI.

that makes sense as the validation messages come from io.swagger.v3.parser.OpenAPIV3Parser

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wing328 picture wing328  路  25Comments

renskesterrenburg picture renskesterrenburg  路  27Comments

TiFu picture TiFu  路  70Comments

ondrakucera picture ondrakucera  路  20Comments

jonrimmer picture jonrimmer  路  80Comments