Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openapi-generator /maven plugin generates invalid java code with «, » characters

I just migrating my code from swagger-code generator to open-api code generator. I am using the generator through the maven plugin. I need it to generate a client for an external API (outside my control), the used swagger file is given.

my plugin setting is as follows:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.3.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <environmentVariables>
                    <supportingFiles>
                        ApiClient.java,Authentication.java,OAuth.java,ApiKeyAuth.java,HttpBasicAuth.java,RFC3339DateFormat.java
                    </supportingFiles>
                </environmentVariables>
                <inputSpec>${project.basedir}/src/main/resources/api-tpz.json</inputSpec>
                <generatorName>java</generatorName>
                <configOptions>
                    <library>resttemplate</library>
                    <dateLibrary>java8</dateLibrary>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

To have a minimal example i stripped down the source of swagger to:

{
    "swagger": "2.0",
    "info": {
        "description": "Api Documentation",
        "version": "1.0",
        "title": "Api Documentation",
        "termsOfService": "urn:tos",
        "contact": {

        },
        "license": {
            "name": "Apache 2.0",
            "url": "http://www.apache.org/licenses/LICENSE-2.0"
        }
    },
    "securityDefinitions": {
        "basicAuth": {
            "type": "basic"
        }
    },
    "security": [
        {
            "basicAuth": []
        }
    ],
    "host": "example.com",
    "basePath": "/api",
    "tags": [
    ],
    "paths": {
    },
    "definitions": {
        "GenericRow": {
            "type": "object",
            "title": "GenericRow",
            "additionalProperties": {
                "type": "object"
            }
        },
        "ResultList«GenericRow»": {
            "type": "object",
            "properties": {
                "offset": {
                    "type": "integer",
                    "format": "int64"
                },
                "overallCount": {
                    "type": "integer",
                    "format": "int64"
                },
                "results": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/GenericRow"
                    }
                }
            },
            "title": "ResultList«GenericRow»"
        }
    }
}

While the generator runs fine the code is invalid

@Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ResultListGenericRow resultList«GenericRow» = (ResultListGenericRow) o;
    return Objects.equals(this.offset, resultList«GenericRow».offset) &&
        Objects.equals(this.overallCount, resultList«GenericRow».overallCount) &&
        Objects.equals(this.results, resultList«GenericRow».results);
  }

resultList«GenericRow» is just wrong.

On the other hand the name of the Class is generated correctly ResultListGenericRow only variable names are handled wrong.

How to fix this problem?

Edit:

I don't have control over given spec file. i need to generate a valid client. swagger code gen worked fine (just removed the special characters). There is no hint about this in migration guide. How to get equivalent code with new generator?

like image 340
dermoritz Avatar asked Mar 19 '26 10:03

dermoritz


1 Answers

I had a similar problem when switching from from swagger-codegen-cli to openapi-generator-cli. In my case, addding forCodeGenerationin Swagger Configuration like that helped:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .forCodeGeneration(true);
   }
}
like image 166
Lisio Avatar answered Mar 21 '26 22:03

Lisio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!