如何使用 openapi-generator-maven-plugin 仅更改一个字段的类型?

bra*_*vo1 5 java date generator maven openapi

我有一个描述从 REST 服务获取的数据的架构。我无法改变这个计划。date-time架构中有两个具有不同格式的类型字段:

"date1": {
    "type": "string",
    "description": "Date 1",
    "format": "date-time"
},
"date2": {
    "type": "string",
    "description": "Date 2",
    "format": "date-time"
}
Run Code Online (Sandbox Code Playgroud)
{
    "date1": "2021-07-29T03:00:00",
    "date2": "2021-04-22T08:25:30.264Z"
}
Run Code Online (Sandbox Code Playgroud)

date-time默认情况下,打开的 api-generator-maven-plugin 会为类型字段创建 OffsetDateTime 类型:

    @JsonProperty("date1")
    private OffsetDateTime date1;

    @JsonProperty("date2")
    private OffsetDateTime date2;
Run Code Online (Sandbox Code Playgroud)

typeMappings我可以将importMappingsOffsetDateTime 替换为 LocalDateTime:

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

但这种替换将发生在所有字段上:

    @JsonProperty("date1")
    private LocalDateTime date1;

    @JsonProperty("date2")
    private LocalDateTime date2;
Run Code Online (Sandbox Code Playgroud)

有没有办法仅用 LocalDateTime 替换 OffsetDateTime date1

这就是我想在生成的类中看到的内容:

    @JsonProperty("date1")
    private LocalDateTime date1;

    @JsonProperty("date2")
    private OffsetDateTime date2;
Run Code Online (Sandbox Code Playgroud)

我知道我可以修复生成的类并将 OffsetDateTime 替换为 LocalDateTime,但我不想在生成后每次都更改生成的类。

提前致谢。

bra*_*vo1 0

这是我最终得出的解决方案。我使用 maven-replacer-plugin 将 OffsetDateTime 替换为 LocalDateTime:

<replacements>
    <replacement>
        <token>import java.time.OffsetDateTime;</token>
        <value>import java.time.OffsetDateTime;\nimport java.time.LocalDateTime;</value>
    </replacement>
    <replacement>
        <token>OffsetDateTime date1</token>
        <value>LocalDateTime date1</value>
    </replacement>
</replacements>
Run Code Online (Sandbox Code Playgroud)

不是很优雅,但它有效)