使用 java.time.Instant 来表示 DateTime 而不是 OffsetDateTime

sro*_*ane 17 java swagger openapi openapi-generator

我正在使用openApi maven 插件为 REST api 生成 java 请求/响应。

该请求有一个 DateTime 属性,当我运行生成器时,我得到了表示为 java.time.OffsetDateTime 的属性的 DateTime 属性。问题是我需要将属性表示为 java.time.Instant。

这是请求的 openApi 规范:

"DocumentDto" : {
      "type" : "object",
      "properties" : {
        "uuid" : {
          "type" : "string",
          "format" : "uuid"
        },
        "creationDate" : {
          "type" : "string",
          "format" : "date-time"
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

生成的java请求:

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2019-05-21T13:07:21.639+02:00[Europe/Zurich]")
public class DocumentDto {
  public static final String SERIALIZED_NAME_UUID = "uuid";
  @SerializedName(SERIALIZED_NAME_UUID)
  private UUID uuid;


  public static final String SERIALIZED_NAME_TEST = "creationDate";
  @SerializedName(SERIALIZED_NAME_TEST)
  private OffsetDateTime creationDate;
}
Run Code Online (Sandbox Code Playgroud)

Maven 插件设置:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>3.3.4</version>
    <executions>
        <execution>
            <id>test-service</id>
            <phase>validate</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>
                    ${project.build.directory}/open-api/swagger.json
                </inputSpec>
                <generatorName>java</generatorName>
                <validateSpec>false</validateSpec>
                <generateApis>false</generateApis>
                <groupId>com.test</groupId>
                <artifactId>test-service</artifactId>
                <modelPackage>test.model</modelPackage>
                <apiPackage>test.api</apiPackage>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                    <dateLibrary>java8</dateLibrary>
                    <java8>true</java8>
                </configOptions>
            </configuration>
        </execution>              
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我已经尝试过typeMappingsimportMappings如下,但它对生成的代码没有影响:

<typeMappings>DateTime=Instant</typeMappings>
<importMappings>Instant=java.time.Instant</importMappings>
Run Code Online (Sandbox Code Playgroud)

小智 19

只需添加到 openapi-generator-maven-plugin 的配置中

<typeMappings>
     <typeMapping>OffsetDateTime=Instant</typeMapping>
</typeMappings>
<importMappings>                                
     <importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping>
</importMappings>
Run Code Online (Sandbox Code Playgroud)


Dan*_*nio 11

我知道问题是关于openapi-generator-maven-plugin,但由于寻找org.openapi.generatorgradle 插件的相同配置将我带到这里,我想这对其他人可能有用:

gradle 插件的等效项org.openapi.generator是:

openApiGenerate {
    // ...
    typeMappings = [
        OffsetDateTime: "Instant"
    ]
    importMappings = [
        "java.time.OffsetDateTime": "java.time.Instant"
    ]
}
Run Code Online (Sandbox Code Playgroud)


小智 8

有同样的问题,但想使用LocalDateTime而不是Instant. 添加以下工作,至少对于实体

<configuration>
  <typeMappings>
    <typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
  </typeMappings>
  <importMappings>                
    <importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
  </importMappings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

LocalDateTime添加了一个导入,并且 yaml 中具有类型的任何实体字段format: date-time都映射到LocalDateTime.

但是,对于 api parameters,没有使用这些设置添加导入。过了一会儿,我放弃了,而是使用完全限定的类型,这样就不需要导入了。只需将上面的更改typeMapping为:

<typeMapping>OffsetDateTime=java.time.LocalDateTime</typeMapping>
Run Code Online (Sandbox Code Playgroud)

之后生成的方法如下所示:

    default ResponseEntity<List<SomeDto>> someMethodGet(
        @ApiParam(value = "") @Valid @RequestParam(value = "changeDateFrom",
            required = false) @org.springframework.format.annotation.DateTimeFormat(
                iso = org.springframework.format.annotation.DateTimeFormat.ISO.DATE_TIME) java.time.LocalDateTime changeDateFrom,
    {
        return getDelegate().someMethodGet(..., changeDateFrom, ...);
    }
Run Code Online (Sandbox Code Playgroud)

PS1 确保您使用的是最新版本的插件。

PS2 我使用了委托模式。

PS2我用的是弹簧模型。

  • 只需 `&lt;dateLibrary&gt;java8-localdatetime&lt;/dateLibrary&gt;` 就足以使其成为 LocalDateTime (https://openapi-generator.tech/docs/generators/java/)。我仍然无法让它与 Instant 一起使用。 (3认同)

tsa*_*hev 6

您的更改会被dateLibrary生成器的配置覆盖。要覆盖datedate-time格式化类型,您最好dateLibrary通过指定 java 生成器无法识别的值来禁用。

将此添加到您的插件<configuration>以实现此目的:

  <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>
Run Code Online (Sandbox Code Playgroud)

您可能还想添加<jsr310>true</jsr310>配置选项,因为它在 dateLibrary 时自动启用java8(但据我所知,这主要用于生成客户端)。