swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.97k stars 6.04k forks source link

[JAVA] skipInlineModelMatches doesn't generate unique classes for models with the same content #12205

Closed ivansla closed 1 year ago

ivansla commented 1 year ago
Description

Swagger codegen, doesn't generate correct ContentBody when using skipInlineModelMatches, or --flatten-inline-schema or --resolve-fully

public class ContentBody {
    @SerializedName("view")
    private ContentBody view = null;
    @SerializedName("export_view")
    private ContentBody exportView = null;
    }

However when I add <modelsToGenerate>Content,ContentBody</modelsToGenerate> to Maven configuration, it is correctly generated:

public class ContentBody {
    @SerializedName("value")
    private String value = null;
}
Swagger-codegen version

version: 3.0.43

Swagger declaration file content or url

https://gist.github.com/ivansla/2463bdfeb866d8cc300091b8c2ce9b2f

Command line used for generation

In Maven: <skipInlineModelMatches>true</skipInlineModelMatches>

In CLI: java -jar swagger-codegen-cli.jar generate -l java -i test.json -o ./target/java3/ --flatten-inline-schema java -jar swagger-codegen-cli.jar generate -l java -i test.json -o ./target/java3/ --resolve-fully

Java 11

Steps to reproduce

mvn clean verify, after the plugin is configured.

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.43</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/test.json</inputSpec>
                <language>java</language>
                <configOptions>
                    <dateLibrary>java11</dateLibrary>
                </configOptions>
                <skipInlineModelMatches>true</skipInlineModelMatches>
                <!--<modelsToGenerate>Content,ContentBody</modelsToGenerate>-->
                <generateModels>true</generateModels>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateModelTests>false</generateModelTests>
                <generateApis>false</generateApis>
                <modelPackage>my.confluence.model</modelPackage>
                <generateSupportingFiles>true</generateSupportingFiles>
                <supportingFilesToGenerate>
                    JSON.java
                </supportingFilesToGenerate>
            </configuration>
        </execution>
    </executions>
</plugin>
Related issues/PRs

I believe it is somewhat related to these issues: https://github.com/swagger-api/swagger-codegen/issues/4883 https://github.com/swagger-api/swagger-parser/issues/1158

ivansla commented 1 year ago

Probably I misunderstood the configuration. However, what I have noticed is in my logs: [INFO] Inline schema created as Content_body. To have complete control of the model name, set thetitlefield or use the inlineSchemaNameMapping option (--inline-schema-name-mappings in CLI).

Basically what happens is, that codegen overwrites my ContentBody type, with inline schema Content_body. Once I added configuration, two classes were generated, original ContentBody and MyContentBody:

<inlineSchemaNameMappings>
      Content_body=MyContentBody
</inlineSchemaNameMappings>

Not sure if this is a feature or bug, but it definitely got me perplexed. Maybe this default behavior of naming inline schema requires revision, one idea is to check whether similar type already exists.

ivansla commented 1 year ago

I have mixed maven plugin. This solution is for: openapi-generator-maven-plugin