Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 533
Mark Avatar asked Jun 19 '20 15:06

Mark


2 Answers

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 {}
like image 185
Vincent AUDIBERT Avatar answered Oct 02 '22 06:10

Vincent AUDIBERT


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>
like image 45
hello_earth Avatar answered Oct 02 '22 05:10

hello_earth