Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA] import mappings in model generation are ignored by default #10419

Closed
oliverschmidthypoport opened this issue Aug 3, 2020 · 4 comments · Fixed by #12234 or swagger-api/swagger-codegen-generators#1189

Comments

@oliverschmidthypoport
Copy link

Description

Since #9981 I have to specify an additional Property ignoreImportMappings=false to use importMappings. The JavaCodegen ist the only Codegen where the default is to ignore defined importMappings. Imho the default should be inverted - it was really hard to find the cause for the not working importMappings after updating the plugin.

Swagger-codegen version

2.4.12 and above

Swagger declaration file content or url
          <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.4.15</version>
            <executions>
              <execution>
                <goals>
                  <goal>generate</goal>
                </goals>
                <configuration>
...
                  <language>java</language>
                  <additionalProperties>
                    <additionalProperty>ignoreImportMappings=false</additionalProperty>
                  </additionalProperties>
                  <importMappings>
...
                  </importMappings>
                </configuration>
              </execution>
            </executions>
          </plugin>
Suggest a fix/enhancement

Remove the default in https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractJavaCodegen.java, so the default from https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java is used.

@tibors13
Copy link

tibors13 commented Oct 6, 2020

The same problem is in V3 codegen (latest). On the beginning it is false, but at some point before handling importSettings has true value (when not set, as above or in code), so the list is ignored :(

@ajperez81
Copy link

I was experiencing a similar problem with v2.4.19:

The <importMappings> option was being used correctly for the imports inside the generated classes. However, the mapped classes were still being generated, without any need for them.

Adding <additionalProperty>ignoreImportMappings=false</additionalProperty> stopped them being generated.

@AttilaW-Keytiles
Copy link

I also ran into this and screwed me up seriously - while developing a customized codegen (extends JavaClientCodegen, which extends AbstractJavaCodegen)

Unless you explicitly provide the ignoreImportMappings option it is picked up during
processOpts() method and it is setting up the flag by invoking io.swagger.codegen.v3.generators.DefaultCodegenConfig.defaultIgnoreImportMappingOption() then.

Please note that the superclass of every Java code generation (io.swagger.codegen.v3.generators.java.AbstractJavaCodegen) overrides this defaultIgnoreImportMappingOption() method and here it returns false as default. So basically @tibors13 is kinda right: it is changing "somewhere on the road" to false :-)

I'm spending time around this option for a while now and I have the feeling it is not really a coincidence... Because the orchestrator of the whole generation, io.swagger.codegen.v3.DefaultGenerator private method generateModels(List<File>, List<Object>) has a piece of code pretty early in the process:

for (String name : modelKeys) {
	try {
		//don't generate models that have an import mapping
		if(!config.getIgnoreImportMapping() && config.importMapping().containsKey(name)) {
			LOGGER.info("Model " + name + " not imported due to import mapping");
			continue;
		}

		...
		
	} catch (Exception e) {
		throw new RuntimeException("Could not process model '" + name + "'" + ".Please make sure that your schema is correct!", e);
	}
}

The point is that in case that if decides to skip the model then it is not loaded, not parsed, will not be available as a CodegenModel anyhow for the later parts of the generator. This can cause issues (I just ran into this) when actually a class extends another class AND this superclass is "excluded" then it will be invisible for mustache templates too and it looks Java generation relies on superclasses. I guess that's why Java generator defaults the ignoreImportMappings to false...

But this is just a feeling so far. I would not say (yet) I get the situation 100% already but based on what I saw in the code so far I guess author(s) of Java codegen classes did not count in some side effects of this move. One clear side effect is that the classes will be generated anyways - however importMapping is about an existing class always...

@ydbondt
Copy link

ydbondt commented Jan 20, 2023

Same problem on the latest 3.0.37 version. Took me quite some time to figure out what (I assumed) I was doing wrong.
It is an unusual assumption that a user should disable a flag in order to use a certain feature.

Fixed it for now using the following gradle config (thnx to the comments above):

swaggerSources {
    myModel {
        inputFile = file('src/main/resources/spec.yaml')
        code {
            // ...
            additionalProperties = [
                    "ignoreImportMappings": "false"
            ]
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants