Home > front end >  How to change the type for only one field using the openapi-generator-maven-plugin?
How to change the type for only one field using the openapi-generator-maven-plugin?

Time:11-10

I have a schema that describes the data I get from the REST service. I can't change this scheme. There are two date-time type fields in the schema that have a different format:

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

By default, the open api-generator-maven-plugin creates the OffsetDateTime type for date-time type fields:

    @JsonProperty("date1")
    private OffsetDateTime date1;

    @JsonProperty("date2")
    private OffsetDateTime date2;

With typeMappings and importMappings I can replace OffsetDateTime to LocalDateTime:

<typeMappings>
    <typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
    <importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
</importMappings>

But this replacement will happen for all fields:

    @JsonProperty("date1")
    private LocalDateTime date1;

    @JsonProperty("date2")
    private LocalDateTime date2;

Is there a way to replace OffsetDateTime with LocalDateTime for date1 only?

That's what I want to see in the generated class:

    @JsonProperty("date1")
    private LocalDateTime date1;

    @JsonProperty("date2")
    private OffsetDateTime date2;

I understand that I can fix the generated class and replace OffsetDateTime with LocalDateTime, but I don't want to change the generated class every time after generation.

Thanks in advance.

CodePudding user response:

Here is the solution I eventually came to. I am replacing OffsetDateTime with LocalDateTime using maven-replacer-plugin:

<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>

Not very elegant, but it works )

  • Related