OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.33k stars 6.45k forks source link

[BUG] Mapping of serverVariableOverrides is missing in generated API Client #16424

Open adrian-haenni opened 1 year ago

adrian-haenni commented 1 year ago

Bug Report Checklist

Description

When using the Maven plugin with "serverVariableOverrides" the generated API Client (ApiClient.java) does not have the variables mapping set.

openapi-generator version

Last working version was 6.2.1, subsequent versions have this issue (using okhttp-gson library).

OpenAPI declaration file content or url
  {
    "openapi": "3.0.1",
    "info": {
      "title": "Jira API",
      "description": "Description.",
      "version": "v1.0.0"
    },
    "servers": [
      {
        "url": "{endpoint}/rest/",
        "description": "Server url"
      }
    ],

  }
Generation Details
<build>
    <plugins>
        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>7.0.0</version>
            <executions>
                <execution>
                    <id>generate-jira-api-client</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/openapi.json</inputSpec>
                <output>${project.build.directory}/generated-sources</output>

                <generatorName>java</generatorName>
                <groupId>com.domain.api.internal</groupId>
                <artifactId>com.domain.api.internal.jira-client</artifactId>
                <artifactVersion>1.0.0</artifactVersion>

                <serverVariableOverrides>
                    <override>endpoint=https://jira.domain.com</override>
                </serverVariableOverrides>

                <packageName>com.domain.api.internal.jira.client</packageName>
                <apiPackage>com.domain.api.internal.jira.client.api</apiPackage>
                <modelPackage>com.domain.api.internal.jira.client.model</modelPackage>
                <invokerPackage>com.domain.api.internal.jira.client.invoker</invokerPackage>
            </configuration>

        </plugin>
    </plugins>
</build>

The generated ApiClient then looks like this:

public class ApiClient {
    private String basePath = "https://jira.domain.com/rest";
    protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>(Arrays.asList(
    new ServerConfiguration(
      "{endpoint}/rest",
      "server url",
      new HashMap<String, ServerVariable>()
    )
  ));
    protected Integer serverIndex = 0;
    protected Map<String, String> serverVariables = null;
    private boolean debugging = false;
    private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
    private Map<String, String> defaultCookieMap = new HashMap<String, String>();
    private String tempFolderPath = null;
...
}

Then the issue arises in the buildUrl Method:

    public String buildUrl(String baseUrl, String path, List<Pair> queryParams, List<Pair> collectionQueryParams) {
        final StringBuilder url = new StringBuilder();
        if (baseUrl != null) {
            url.append(baseUrl).append(path);
        } else {
            String baseURL;
            if (serverIndex != null) {
                if (serverIndex < 0 || serverIndex >= servers.size()) {
                    throw new ArrayIndexOutOfBoundsException(String.format(
                    "Invalid index %d when selecting the host settings. Must be less than %d", serverIndex, servers.size()
                    ));
                }
                baseURL = servers.get(serverIndex).URL(serverVariables);
            } else {
                baseURL = basePath;
            }
            url.append(baseURL).append(path);
        }
....

The baseUrl argument is null and the serverIndex class variable is not null, so it will create the base URL with baseURL = servers.get(serverIndex).URL(serverVariables); but serverVariables is null. In consequence the resolved base url is {endpoint}/rest/ instead of https://jira.domain.com/rest

wing328 commented 1 year ago

@adrian-haenni thanks for reporting the issue. Do you mind doing a git bisect to identify the commit causing the issue?

adrian-haenni commented 1 year ago

@wing328 it was introduced in https://github.com/OpenAPITools/openapi-generator/pull/14179 in commit 9450984af84e02cdfc96560147e9f87c4bc381d9

the {{#variables}} variable seem to be empty as well when generating the client.

wing328 commented 1 year ago

@outscale-mdr can you please take a look at the issue when you've time? Thanks.

and-sm commented 5 months ago

Probably the same for typescript-axios and still for java.

config.json:

{
  "apiPackage": "api",
  "modelPackage": "models",
  "withInterfaces": true,
  "withSeparateModelsAndApi": true,
  "supportsES6": true,
  "servers": [
    {
      "url": "http://{host}:{port}/",
      "variables": {
        "host": {
          "default": "localhost"
        },
        "port": {
          "default": "8000"
        }
      }
    }
  ]
}
npx --yes @openapitools/openapi-generator-cli generate -i openapi.json -g typescript-axios -c config.json -o client --server-variables=host=localhost1,port=9999

Result (base.ts): export const BASE_PATH = "http://localhost".replace(/\/+$/, "");