4

I am trying to generate an API class from a yaml file using ZonedDateTime as the class for a date. When i do this i can successfully change the generated class OffsetDateTime to ZonedDateTime in the configuration, but the correct import statement is not generated, which causes an error. It only imports import java.time.OffsetDateTime;. Does anybody have an idea to what I can try so it generates the correct import?

This is my latest attempt to configure it in the pom, and I am using the swagger-codegen-maven-plugin, version 3.0.20, from io.swagger.codegen.v3.

<configuration>
    <additionalProperties>
        <additionalProperty>ignoreImportMappings=false</additionalProperty>
    </additionalProperties>
    <generateSupportingFiles>false</generateSupportingFiles>                 
    <inputSpec>${project.basedir}/src/main/resources/api/api.yaml</inputSpec>
    <modelPackage>api.domene</modelPackage>
    <language>spring</language>
    <generateModels>true</generateModels>
    <generateApis>true</generateApis>
    <generateApiTests>false</generateApiTests>
    <configOptions>
        <library>spring-boot</library>
        <interfaceOnly>true</interfaceOnly>
        <hideGenerationTimestamp>true</hideGenerationTimestamp>
        <useTags>true</useTags>
        <java8>true</java8>
        <serializableModel>true</serializableModel>
        <dateLibrary>java8</dateLibrary>
    </configOptions>
    <typeMappings>OffsetDateTime=ZonedDateTime</typeMappings>
    <importMappings>                             
         <importMapping>java.time.OffsetDateTime=java.time.ZonedDateTime</importMapping>
    </importMappings>
</configuration>
Jeroen Kransen
  • 1,379
  • 3
  • 19
  • 45
Sara HH
  • 41
  • 4
  • How about adding another mapping in the reverse order i.e. ` java.time.OffsetDateTime=java.time.ZonedDateTime java.time.ZonedDateTime=java.time.OffsetDateTime `? – Arvind Kumar Avinash Jul 22 '21 at 10:46

1 Answers1

0

The importMappings and importMapping tags do not work, you are right. But there is a workaround;

...
    </configOptions>
    <typeMappings>
        <typeMapping>OffsetDateTime=java.time.ZonedDateTime</typeMapping>
    </typeMappings>
</configuration>

That way, in the generated sources, even though the import section contain "import java.time.OffsetDateTime;", the generated method parameters and variables will be defined as "java.time.ZonedDateTime" so you can use correct date functions in your code.

az3
  • 3,571
  • 31
  • 31