OpenAPITools / OpenAPITools/openapi-generator

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

Open
#16,424 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated ApiClient.java and its buildUrl method, then reproduce the issue using the shown OpenAPI document and Maven plugin configuration with serverVariableOverrides. Trace where the plugin's override mapping should reach serverVariables; done means the generated client applies the endpoint override and resolves the base URL to https://jira.domain.com/rest.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.