swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

importMappings not working I can not replace import

Open FuchengLi opened this issue 7 years ago • 15 comments

      <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 avatar Apr 19 '18 14:04 FuchengLi

@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

macjohnny avatar Apr 19 '18 14:04 macjohnny

Hi Macjohnny, thanks you much for you reply do you mean 3.0.0-rc0 is working ? but looks this version can not run up https://github.com/swagger-api/swagger-codegen/issues/7518

FuchengLi avatar Apr 20 '18 00:04 FuchengLi

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

macjohnny avatar Apr 20 '18 05:04 macjohnny

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 avatar Apr 20 '18 07:04 FuchengLi

@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

macjohnny avatar Apr 20 '18 08:04 macjohnny

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

macjohnny avatar Apr 24 '18 10:04 macjohnny

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 avatar Jun 05 '19 14:06 aaitmouloud

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

StephenOTT avatar Jun 26 '19 22:06 StephenOTT

I have tested this on 2.4.5 and it appears still broken

StephenOTT avatar Jun 26 '19 23:06 StephenOTT

@aaitmouloud your workaround also does not appear to work for the ApiResponse annotation in the response param. The original model remains

StephenOTT avatar Jun 26 '19 23:06 StephenOTT

@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

aaitmouloud avatar Jun 27 '19 14:06 aaitmouloud

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.

StephenOTT avatar Jun 27 '19 15:06 StephenOTT

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

aaitmouloud avatar Jul 02 '19 09:07 aaitmouloud

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

PiyushP17 avatar Mar 17 '20 17:03 PiyushP17

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.

belgoros avatar Jun 13 '24 10:06 belgoros