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

Generated Java class is overwritten by a class of same name with different case. #10008

Closed
christophermarianolumiradx opened this issue Jan 27, 2020 · 3 comments · Fixed by #10529
Assignees
Milestone

Comments

@christophermarianolumiradx
Description

We are using 2 java classes in our code: SystemDto, and SystemDTO (different casing in the class suffix, Dto vs DTO). The former is local from the current project while the latter is an external dependency.
I noticed that only one class is generated with one class overwriting the other.

Screen Shot 2020-01-27 at 2 05 39 PM
Notice that a file JsonSystemDTO is generated with the class name as JsonSystemDto
In this scenario, I believe that swagger codegen is resolving the files in a case insensitive manner and overwriting each other.

Swagger-codegen version

Using swagger-codegen-maven-plugin version: 2.4.10

Maven Plugin config
<!-- maven plugin to support the swagger code generation project -->
      <plugin>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>${swagger-codegen-maven-plugin.version}</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.build.outputDirectory}/static/apidocs/ui/swagger.json</inputSpec>
              <language>java</language>
              <configOptions>
                <sourceFolder>src/main/java</sourceFolder>
                <output>${project.build.outputDirectory}/generated-sources/swagger</output>
                <groupId>com.company.router</groupId>
                <artifactId>company-router-sdk</artifactId>
                <artifactVersion>6.5.0-SNAPSHOT</artifactVersion>
                <modelPackage>com.company.router.sdk.model</modelPackage>
                <apiPackage>com.company.router.sdk.api</apiPackage>
                <invokerPackage>com.company.router.sdk</invokerPackage>
                <variableNamingConvention>camelCase</variableNamingConvention>
                <serializableModel>true</serializableModel>
                <fullJavaUtil>true</fullJavaUtil>
                <library>resteasy</library>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
Command line used for generation

Since this is attached to our build, code was generated using mvn clean install

Suggest a fix/enhancement

I am not entirely sure since I have not debugged the project, but I noticed that io.swagger.codegen.DefaultCodegen has code which capitalizes model names, and api names. This can cause the following map below to have the same map key:
io.swagger.codegen.DefaultGenerator.java line 385

Model model = definitions.get(name);
Map<String, Model> modelMap = new HashMap<String, Model>();
modelMap.put(name, model);
Map<String, Object> models = processModels(config, modelMap, definitions);
if (models != null) {
        models.put("classname", config.toModelName(name));
        models.putAll(config.additionalProperties());
        allProcessedModels.put(name, models);
 }

io.swagger.codegen.DefaultCodegen

/**
     * Output the API (class) name (capitalized) ending with "Api"
     * Return DefaultApi if name is empty
     *
     * @param name the name of the Api
     * @return capitalized Api name ending with "Api"
     */
    public String toApiName(String name) {
        if (name.length() == 0) {
            return "DefaultApi";
        }
        return initialCaps(name) + "Api";
    }

    /**
     * Output the proper model name (capitalized).
     * In case the name belongs to the TypeSystem it won't be renamed.
     *
     * @param name the name of the model
     * @return capitalized model name
     */
    public String toModelName(final String name) {
        return initialCaps(modelNamePrefix + name + modelNameSuffix);
    }

It may or may not be related.

@Nandy-Saran
Copy link

Hi team, I am facing a similar issue where the names are capitalized. Would be better if it was case insensitive. Please look into the issue, thank you.

@HugoMario HugoMario self-assigned this Jul 17, 2020
@HugoMario HugoMario added this to the M10 milestone Oct 24, 2020
@HugoMario
Copy link
Contributor

Hi @christophermarianolumiradx @Nandy-Saran

The codegen generator actually tries to write the files, but one file is overwritn because the OS option. what suggestion you have for codegen in this scenario?

One option i have in mind is renaming one of the "repeated" definition. What do you guys think?

@thanhnguyenchisong
Copy link

I tried your new fixing issue that changed the class name but don't replace all the import class with the new name. There is any the fixing for that

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

Successfully merging a pull request may close this issue.

4 participants