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

2

8

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?

Quadruplex answered 25/11, 2020 at 13:51 Comment(2)
Did you manage to implement this? Can you share how? Thanks.Downs
Did you get how to do this? can you share please?Gallimaufry
C
0

You can create a bean of your generated api client class and an ApiClient bean configured with your desired endpoint to that class.

@Configuration 
public class YourGeneratedApiConfig {
  
  @Value("${rest.clients.yourGeneratedApi.endpoint}")
  private String endpoint;
  
  @Bean public YourGeneratedApi yourGeneratedApi() {
    return new YourGeneratedApi(apiClient); 
  }

  @Bean public ApiClient apiclient(RestTemplate restTemplate) {
    ApiClient apiclient = new ApiClient(restTemplate);
    apiclient.setBasePath(endpoint);
    return apiclient; 
  }

  @Bean public RestTemplate restTemplate (RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder.build();
  }

}
Cardboard answered 30/9, 2022 at 9:6 Comment(0)
E
0

Did you try using <serverVariableOverrides>? See the answer here: OpenAPI 3 path substitution with openapi-generator-maven-plugin.

Also consider using maven-replacer-plugin. See the working example in this question: maven-replacer-plugin and multiple files

Enlil answered 16/9, 2023 at 14:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.