importMappings not working I can not replace import
<version>2.3.1</version>
<executions>
<execution>
<id>generate-server-jar</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${swagger-gen.yml}</inputSpec>
<language>spring</language>
<configOptions>
<sourceFolder>src/gen/java</sourceFolder>
<apiPackage>${swagger-gen.api.package}</apiPackage>
<modelPackage>${swagger-gen.model.package}</modelPackage>
<serializableModel>true</serializableModel>
<useJaxbAnnotations>false</useJaxbAnnotations>
<dateLibrary>java8-localdatetime</dateLibrary>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
</configOptions>
<typeMappings>
<typeMapping>OffsetDateTime=LocalDateTime</typeMapping>
</typeMappings>
<importMappings>
<importMapping>java.time.OffsetDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
</configuration>
@FuchengLi this should be fixed with the current master, as the spring generator manually overrides the date time library used, as of version 2.3.1
Hi Macjohnny, thanks you much for you reply do you mean
@FuchengLi I didn't test it with 3.0.0-rc0, but if you use the current master, i.e. 2.4.0-SNAPSHOT, then the following works:
<configuration>
...
<language>spring</language>
<configOptions>
...
<dateLibrary>java8-localdatetime</dateLibrary>
</configOptions>
</configuration>
Thank you so much @macjohnny,I was try 2.4.0 snapshot,but I got can not found jar file erre when I build,could you please paste you pom content?
@FuchengLi you need to build the .jar file yourself, i.e. in the cloned swagger-codegen repository you run
mvn package
and you finally get the jar file in
.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar
@FuchengLi you could also download a current snapshot here: https://oss.sonatype.org/content/repositories/snapshots/io/swagger/swagger-codegen-cli/2.4.0-SNAPSHOT/
I can confirm that this configuration with the maven plugin
<configOptions>
<dateLibrary>joda</dateLibrary>
<library>resttemplate</library>
</configOptions>
<typeMappings>
<typeMapping>DateTime=org.joda.time.LocalDateTime</typeMapping>
</typeMappings>
Results in a generated code that uses DateTime instead of LocalDateTime because the AbstractJavaCodegen#processOpts() method is called after the configuration given in the <typeMappings> element is processed, and thus, overwrites it...
A workaround is to use this configuration, but it is quite dirty (i tested id with resttemplate only)
<configOptions>
<serializableModel>true</serializableModel>
<dateLibrary>legacy</dateLibrary>
<library>resttemplate</library>
</configOptions>
<typeMappings>
<typeMapping>DateTime=org.joda.time.LocalDateTime</typeMapping>
<typeMapping>date=org.joda.time.LocalDate</typeMapping>
</typeMappings>
@aaitmouloud what is the difference between the type and import mappings in this case ? Is the typemapping functional just because it's not enforcing only changing of openAPITypes?
I have tested this on 2.4.5 and it appears still broken
@aaitmouloud your workaround also does not appear to work for the ApiResponse annotation in the response param. The original model remains
@aaitmouloud what is the difference between the type and import mappings in this case ?
typeMapping is the type used in the body of the generated class, importMapping only adds an import a.b.c.Type; at the top of your class file.
Is the typemapping functional just because it's not enforcing only changing of openAPITypes?
The key here is to use <dateLibrary>legacy</dateLibrary> instead of joda-time because using the latter will overwrite your typeMappings in AbstractJavaCodegen#processOpts()
I have tested this on 2.4.5 and it appears still broken
I'm using the same version, this works for me
@aaitmouloud your workaround also does not appear to work for the ApiResponse annotation in the response param. The original model remains
I did not have this issue, you can try <generateApiDocumentation>false</generateApiDocumentation> in the plugin configuration
Might be more specific to the use of the time classes. I am not using the time classes. But just trying to use custom classes rather than the generated definitions. But the generated API interface still points to the swagger definitions. The import is brought in, but the typing on the return types for the operations are still the swagger definitions.
Might be more specific to the use of the time classes. I am not using the time classes. But just trying to use custom classes rather than the generated definitions. But the generated API interface still points to the swagger definitions. The import is brought in, but the typing on the return types for the operations are still the swagger definitions.
Please post your plugin configuration here
@here , There is a field in my DTO of type Map<String, Object>[ ] event; While creating client code using codegen in spring boot, the plugin creates a Map class inside model package. And it give a compile error everytime as there is a conflict with java.util.Map; I tried using <importMapping> config but still its the same issue. Any help would be appreciated ? ?
A tip to fix the imports, I just used typeMappings :
<typeMappings>
<typeMapping>DateTime=java.time.LocalDateTime</typeMapping>
<typeMapping>Date=java.time.LocalDate</typeMapping>
</typeMappings>
and completely dropped off (removed this block I used jointly with typeMappings) this settings block:
<importMappings>
<importMapping>LocalDate=java.time.LocalDate</importMapping>
<importMapping>LocalDateTime=java.time.LocalDateTime</importMapping>
</importMappings>
this way, the date fields in the generated POJOs were declared with the full package name `java.time.LocalDate´ and no imports were added.