Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use configuration file (openapitools.json) with @openapitools/openapi-generator-cli?

I use @openapitools/openapi-generator-cli(v2.1.7) to generate the API library on client side.

It works pretty well, except I am not able to format the code generated as I want.

I just noticed there is a new option that allows to configure the spaces as mentioned in the example ("spaces": 2):

{
  "$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "4.3.1",
    "storageDir": "~/my/custom/storage/dir", // optional
    "generators": { // optional
      "v2.0": { // any name you like (just printed to the console log) 
        "generatorName": "typescript-angular",
        "output": "#{cwd}/output/v2.0/#{ext}/#{name}",
        "glob": "examples/v2.0/{json,yaml}/*.{json,yaml}",
        "additionalProperties": {
          "ngVersion": "6.1.7",
          "npmName": "restClient",
          "supportsES6": "true",
          "npmVersion": "6.9.0",
          "withInterfaces": true
        }
      },
      "v3.0": { // any name you like (just printed to the console log) 
        "generatorName": "typescript-fetch",
        "output": "#{cwd}/output/v3.0/#{ext}/#{name}",
        "glob": "examples/v3.0/petstore.{json,yaml}"
      }
    }
  }
}

This sounds great!

The problem is that I am not able to use the configuration file as specified in the official page:

If openapi-generator-cli generate is called without further arguments, then the configuration is automatically used to generate your code.

When I do that:

openapi-generator-cli generate

I keep stuck with an error:

[error] Required option '-i' is missing

And if I add the -i parameter, for example:

openapi-generator-cli generate -i http://localhost:8081/v2/api-docs

Then the "openapitools.json" file is ignored and overwritten by the default configuration:

{
  "$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "4.3.1"
  }
}

I also tried to do it the way I've used to do it up to now plus adding the parameter "-spaces=2":

openapi-generator-cli generate -i http://localhost:8081/v2/api-docs -g typescript-angular -o src/app/tools/openapi -spaces=2

But again it didn't work, plus I have now a useless file (openapitools.json) annoying my obsessive-compulsive disorder!

For info, my npm version (npm -v) is:

6.14.8

And I am using the current last Angular version:

11.0.0

like image 404
Kr1 Avatar asked Dec 08 '20 10:12

Kr1


People also ask

What is OpenAPI generator CLI?

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (both 2.0 and 3.0 are supported). Please see OpenAPITools/openapi-generator. The OpenAPI Generator is a Java project.

What is OpenAPI config?

OpenAPI Generator supports global properties for selective generation -- such as apis -- to have either a blank value or a comma-separated list of selected values. We would define this in CLI as --global-property apis or --global-property apis=Equipment .

How does OpenAPI generator work?

OpenAPI Generator provides tooling which allows you to extract the templates embedded in its JAR file into a directory, and then use the template files from that directory when generating code, providing simple customization without the need for recompiling the Java application.

What is OpenAPI generator Maven plugin?

OpenAPI Generator is a open-source project to generate REST API clients, server stubs and documentation from on an OpenAPI specification (fka Swagger specification) document. There are a number of ways to use OpenAPI Generator: Openapi generator cli. With Plugins (Maven or Gradle)


2 Answers

I have had similar problems as described by you; in particular the "overwritten by the default configuration".

I do think that a non-valid opanapitools.json is causing it to be completely reset. It seems like the comments are causing these validation errors.

Maybe remove them and try again.

Hope this helps, I got it running with package.json->scripts like this.

Although the "spaces" property seems only to be used, when the cli is writing it's own config file.

like image 94
Philipp Ostmeyer Avatar answered Oct 14 '22 05:10

Philipp Ostmeyer


You can try this, it will work

your openapitools.json content should be like this:

{
  "$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "5.3.0",
     "generators":{
         "v2.0": {
        "generatorName": "javascript",
        "output": "./output",
        "inputSpec": "http://localhost:44301/swagger/v1/swagger.json",
        "additionalProperties": {
          "npmName": "restClient",
          "supportsES6": "true",
          "withInterfaces": true
        }
      }
     }
  }
}

I know based on the documentation here https://www.npmjs.com/package/@openapitools/openapi-generator-cli there should be a "glob": "examples/v2.0/{json,yaml}/*.{json,yaml}", under your generator key but for a strange reason, the "glob" will work with the file but not URLs! if you want to get your Open API Specification file directly from Swagger URL in your API server you should use "inputSpec" instead and it worked for me. in my case it is:

  "inputSpec": "http://localhost:44301/swagger/v1/swagger.json",

if you set your openapitools.json like above code you can generate for example your javascript client with this command:

 openapi-generator-cli generate  --generator-key v1.0
like image 1
Amir H Rahimi Avatar answered Oct 14 '22 03:10

Amir H Rahimi