I am using openapi-generator-maven-plugin with next config:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.0.1</version>
<executions>
<execution>
<id>spring-code-generation</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<typeMappings>
<typeMapping>OffsetDateTime=ZonedDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.ZonedDateTime</importMapping>
</importMappings>
<inputSpec>
${project.basedir}/openapi.yml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>api-swagger.api</apiPackage>
<modelPackage>api-swagger.model</modelPackage>
<supportingFilesToGenerate>
ApiUtil.java
</supportingFilesToGenerate>
<configOptions>
<sourceFolder>src/main/java/</sourceFolder>
<delegatePattern>true</delegatePattern>
<interfaceOnly>true</interfaceOnly>
<library>spring-boot</library>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
I need to have ability to use several datetime java classes for my models - some needs to be ZonedDateTime, some other LocalDateTime and another needs to be Date class.
Something like this:
SomeDto:
required:
- dateTime
type: object
properties:
dateTime:
type: string
format: ZonedDateTime
SomeOtherDto:
required:
- dateTime
type: object
properties:
dateTime:
type: string
format: LocalDateTime
Can I have such a logic by using openapi-generator-maven-plugin ?
Yes, you are so close. Try this:
<typeMappings>
<typeMapping>OffsetDateTime=OffsetDateTime</typeMapping>
<typeMapping>ZonedDateTime=ZonedDateTime</typeMapping>
<typeMapping>LocalDateTime=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>OffsetDateTime=java.time.OffsetDateTime</importMapping>
<importMapping>ZonedDateTime=java.time.ZonedDateTime</importMapping>
<importMapping>LocalDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
What this is doing is telling the generator that any type of OffsetDateTime is equal to the class OffsetDateTime
. Then, it tells the generator that any time the class OffsetDateTime
is used, the import for that class is java.time.OffsetDateTime
.
So, you can also have custom type mappings that will still use the java class. You don't have to name them after the class name. For example, the following will still work:
<typeMappings>
<typeMapping>offset=OffsetDateTime</typeMapping>
<typeMapping>zoned=ZonedDateTime</typeMapping>
<typeMapping>local=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>OffsetDateTime=java.time.OffsetDateTime</importMapping>
<importMapping>ZonedDateTime=java.time.ZonedDateTime</importMapping>
<importMapping>LocalDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
when used with this schema:
SomeDto:
required:
- dateTime
type: object
properties:
dateTime:
type: string
format: zoned
SomeOtherDto:
required:
- dateTime
type: object
properties:
dateTime:
type: string
format: local
EvenAnotherDto:
required:
- dateTime
type: object
properties:
dateTime:
type: string
format: offset
Furthermore, you can declare the imports inline if you would like, and not worry about the importMappings
at all.
<typeMappings>
<typeMapping>OffsetDateTime=java.time.OffsetDateTime</typeMapping>
<typeMapping>ZonedDateTime=java.time.ZonedDateTime</typeMapping>
<typeMapping>LocalDateTime=java.time.LocalDateTime</typeMapping>
</typeMappings>
It's really up to you.