openapi-generator-maven-plugin: change rest client base path during runtime

Viewed 1391

I have a Spring Boot Application and I use openapi-generator-maven-plugin for generating rest client. I want to have a option to change url during runtime.

The url of the rest server is now hardcoded in the following snippet of OpenAPI definition:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080'
    description: Generated server url

Configuration of the maven plugin:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.3.1</version>
<execution>
                        <id>vydejClient</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/manualni_kodovani_vydej.yaml
                            </inputSpec>
                            <generatorName>java</generatorName>
                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>
                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                            </configOptions>
                            <library>resttemplate</library>
                            <typeMappings>
                                <typeMapping>File=org.springframework.core.io.Resource</typeMapping>
                            </typeMappings>
                            <apiPackage>client</apiPackage>
                            <modelPackage>client.model</modelPackage>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

This code is generated

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-11-23T14:40:42.232315+01:00[Europe/Prague]")
@Component("ApiClient")
public class ApiClient {
    ...
    private String basePath = "http://localhost:8080";
    ...
     /**
     * Set the base path, which should include the host
     * @param basePath the base path
     * @return ApiClient this client
     */
    public ApiClient setBasePath(String basePath) {
        this.basePath = basePath;
        return this;
    }


}

I need to have this attribute configurable. Any idea how to do it?

0 Answers
Related