openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [Java] Generated methods have a "_<number>" suffix

Open ahoehma opened this issue 1 year ago • 3 comments

Description

Since version 7.9.0 my generated java-client looks different. Almost all methods got an additional "_NN" on there names which is breaking my surrounding java code.

openapi-generator version

7.9.0

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "paths": {
     "/api/v1/brain/product/localizations/{productId}/{version}": {
      "post": {
        "tags": [
          "read",
          "service",
          "v1.24"
        ],
        "summary": "Get localization data for a product. This was introduced in 2024.15.0 with api 1.24.",
        "operationId": "getProductLocalizations",
        "parameters": [
          {
            "name": "productId",
            "in": "path",
            "description": "Provide the id of the product.",
            "required": true,
            "schema": {
              "type": "string"
            },
            "example": "1LE1X"
          },
          {
            "name": "version",
            "in": "path",
            "description": "Provide the version of the product. Could be 'LATEST' to retrieve the newest version.",
            "required": true,
            "schema": {
              "type": "string"
            },
            "example": "LATEST"
          }
        ],
        "requestBody": {
          "description": "Required payload for the request.",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/GetProductLocalizationsPayload"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GetProductLocalizationsResult"
                }
              }
            }
          }
      }
    }
}
...

I can't post the whole definition because of closed source ... but the interesting part is here I hope. It's all about the "operationId": "getProductLocalizations" ... and all the "operationId's" are unique in the openapi.json.

Generation Details
<plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>7.9.0</version>
        <configuration>
          <!-- https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/java.md -->
          <generatorName>java</generatorName>
          <addCompileSourceRoot>true</addCompileSourceRoot>
          <generateModelTests>false</generateModelTests>
          <skipOverwrite>false</skipOverwrite>
          <indentSize>2</indentSize>
          <lineLength>200</lineLength>
          <verbose>false</verbose>
          <skipIfSpecIsUnchanged>false</skipIfSpecIsUnchanged>
          <configOptions>
            <additionalModelTypeAnnotations><![CDATA[ @SuppressWarnings("all") ]]></additionalModelTypeAnnotations>
            <enumUnknownDefaultCase>true</enumUnknownDefaultCase>
            <library>resttemplate</library>
            <java8>true</java8>
            <dateLibrary>java8</dateLibrary>
            <failOnUnknownProperties>false</failOnUnknownProperties>
            <sortModelPropertiesByRequiredFlag>false</sortModelPropertiesByRequiredFlag>
            <sortParamsByRequiredFlag>false</sortParamsByRequiredFlag>
          </configOptions>
        </configuration>
<executions>
<execution>
            <id>generate-apiclient-cc-configuration-brain</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${openapi.input.cerebellum}</inputSpec> <!-- kleinhirn -->
              <output>${project.build.directory}/generated-sources/cc-api-cerebellum</output>
              <apiPackage>com.foo.spice.cc.client.brain.api</apiPackage>
              <modelPackage>com.foo.spice.cc.client.brain.model</modelPackage>
              <invokerPackage>com.foo.spice.cc.client.brain.handler</invokerPackage>
            </configuration>
          </execution>
<executions>
</plugin>

The generated code looks like this:

/**
     * Get localization data for a product. This was introduced in 2024.15.0 with api 1.24.
     * 
     * <p><b>200</b> - OK
     * @param productId Provide the id of the product. (required)
     * @param version Provide the version of the product. Could be &#39;LATEST&#39; to retrieve the newest version. (required)
     * @param getProductLocalizationsPayload Required payload for the request. (required)
     * @return GetProductLocalizationsResult
     * @throws RestClientException if an error occurs while attempting to invoke the API
     */
    public GetProductLocalizationsResult getProductLocalizations_0(String productId, String version, GetProductLocalizationsPayload getProductLocalizationsPayload) throws RestClientException {
        return getProductLocalizations_0WithHttpInfo(productId, version, getProductLocalizationsPayload).getBody();
    }

How to disable this additional suffixes? And why there are even there ? :-)

Kind regards Andreas

ahoehma avatar Oct 14 '24 13:10 ahoehma

I enabled verbose logging and I found something like this (here another method but same effect):

"operationIdOriginal" : "removeServiceProduct",
"operationIdLowerCase" : "removeserviceproduct_0",
"operationIdCamelCase" : "RemoveServiceProduct0",
"operationIdSnakeCase" : "remove_service_product_0",

ahoehma avatar Oct 14 '24 14:10 ahoehma

Hello, I have noticed something similar with the "kotlin-spring" generator since 7.9.0, but in the following case: The "_0" is applied on function that have in the OpenAPI definition file the same "operationId", event with different tags (leading them to be generated in different Kotlin interfaces). During the OpenAPI code generation (via gradle plugin), we can see this message: generated unique operationId '<operationId>_0'

@ahoehma do you have in your OpenAPI definition file another operationId "getProductLocalizations"?

Kuba15 avatar Oct 17 '24 08:10 Kuba15

@ahoehma do you have in your OpenAPI definition file another operationId "getProductLocalizations"?

No. I checked that many times :) The openapi.json looks fine to me. And with 7.8.0 everything is also fine. It must be something new since 7.9.0.

ahoehma avatar Oct 17 '24 21:10 ahoehma

Looks like the issue is related to the fact that you're having more than one tag per operationId. The issue was introduced by the fix for #19715 and looks like it's being addressed in #19913

CaptainAye avatar Oct 20 '24 20:10 CaptainAye

+1

GreenRover avatar Oct 22 '24 07:10 GreenRover

OT: Originally I thought the tags such there to leave some more "details" about my api and have some quick filter in swagger-ui. But meanwhile I realized that for each tag the generator creates it's own interface, ServiceApi, ReadApi etc. Since last version I started adding the api-version as tag which created just more none-sense interfaces like V123Api. Is there a best practice for tags?

ahoehma avatar Oct 24 '24 05:10 ahoehma