How do I override the servers -> url (basepath) when generating client using openapi-generator?
Asked Answered
S

2

11

I have an OpenAPI specification document (that I do not control) that contains the following snippet:

servers:
  - url: http://www.[someservice].com/api

I am using this OpenAPI document to generate a typescript-angular client that I'm using in my Angular SPA. This works fine when I run this on production (my api backend is accessible at the url provided).

I'd like to use http://localhost:1234/api for local testing. How do I override the basepath using openapi-generator so that I can generate client code that works locally?

Simson answered 19/6, 2020 at 15:23 Comment(1)
Similar problem here: We're updating api.json for the client from one of three servers. After updating the generator to from 4.1.2 to 4.3.1 the client always connects to the URL that has been the source for the api.json update (and resides in the new ServerConfiguration.URL) and no longer to ApiClient.BasePath like before the upate. Options my be: Clean the server list and add the server with the URL like it is set in BasePath or maybe extend the API to let its setBasePath do this (thanks to my workmate for these hints).Sather
H
10

The generated angular client can be configured for the server url through the Configuration instance. Here is an extract from our app.module.ts to have the client call the server through the client-serving url (you should be able to define basePath to your value) :

@NgModule({
  declarations: [...],
  imports: [
    ...
    ApiModule.forRoot(() => {
      return new Configuration({
        basePath: ``,
      });
    }),
    ...
  ],
  ...
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})
export class AppModule {}
Headreach answered 4/2, 2021 at 12:48 Comment(1)
Thank you. Couldn't find it in the docs.Wareroom
S
5

until they put it in the documentation, if you are using openapi-generator Maven plugin, the concept is called server variables - it is documented here and passed e.g. to CLI generator as follows:

    --server-variables <server variables>
        sets server variables overrides for spec documents which support
        variable templating of servers.

E.g. (i took the example from the corresponding feature pull request):

openapi-generator generate -g java -i myproject/myapi.json -o myproject/generated --server-variables=host=myhost.com,basePath=myapi

The above would accomodate e.g. the scenario where myapi.json (the specification of our API) contained the following:

...    
"servers": [
    {
      "url": "https://{host}/{basePath}/v1/",
      "variables": {
        "host": {
          "default": "service.domain.org"
        },
        "basePath": {
          "default": "mythings"
        }
      }
    }
  ],
...

In which case, the generated ApiClient.java would contain the following:

private String basePath = "https://myhost.com/myapi/v1";

I haven't actually tried the above and assembled it as an example. What I tried in a working example was to use openapi-generator-maven-plugin in the following manner (see serverVariableOverrides):

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.4.0</version>
    <executions>
       <execution>
          <goals>
             <goal>generate</goal>
          </goals>
          <configuration>
             <inputSpec>${project.basedir}/src/main/resources/myapi.json</inputSpec>
             <generatorName>java</generatorName>
             <library>resttemplate</library>
             <apiPackage>com.mycompany.myapi.client.generated.api</apiPackage>
             <modelPackage>com.mycompany.myapi.client.generated.model</modelPackage>
             <invokerPackage>com.mycompany.myapi.client.generated.invoker</invokerPackage>
             <configOptions>
                <dateLibrary>java8</dateLibrary> <!-- otherwise we have a problem, threetenbp has to be used -->
             </configOptions>
             <serverVariableOverrides>host=myhost.com,basePath=myapi</serverVariableOverrides>
          </configuration>
       </execution>
    </executions>
 </plugin>
Scolex answered 10/5, 2022 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.