Skip to content

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

@adrian-haenni

Description

@adrian-haenni

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions