I'm using the openApi maven plugin to generate java request/responses for a REST api.
The request has a DateTime property, when I run the generator I get the DateTime property of the attribute represented as java.time.OffsetDateTime. The problem is I have a requirement to represent the property as java.time.Instant.
This is the openApi specification for the request:
"DocumentDto" : {       "type" : "object",       "properties" : {         "uuid" : {           "type" : "string",           "format" : "uuid"         },         "creationDate" : {           "type" : "string",           "format" : "date-time"         }       }     }   The generated java request:
@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; }   The maven plugin setup:
<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>   I've already tried the typeMappings and importMappings as follow but it had no affect on the generated code:
<typeMappings>DateTime=Instant</typeMappings> <importMappings>Instant=java.time.Instant</importMappings> 
                just add to the configuration of openapi-generator-maven-plugin
<typeMappings>      <typeMapping>OffsetDateTime=Instant</typeMapping> </typeMappings> <importMappings>                                      <importMapping>java.time.OffsetDateTime=java.time.Instant</importMapping> </importMappings> 
                        Had the same problem but wanted to use LocalDateTime instead of Instant. Adding the following works, at least for entities:
<configuration>   <typeMappings>     <typeMapping>OffsetDateTime=LocalDateTime</typeMapping>   </typeMappings>   <importMappings>                     <importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>   </importMappings> </configuration>   i.e. an import for LocalDateTime is added, and any entity fields in the yaml with type format: date-time are mapped to LocalDateTime.
However, for api parameters, no import was added with those settings. After a while I gave up and instead used fully qualified types so that no import is required. Just change the above typeMapping to:
<typeMapping>OffsetDateTime=java.time.LocalDateTime</typeMapping>   Your generated methods afterwards looked like:
    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, ...);     }   PS1 Make sure you use the most recent version of the plugin.
PS2 I used the delegate pattern.
PS2 I used the spring model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With