Kr1
Kr1

Reputation: 1303

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

Upvotes: 15

Views: 25512

Answers (2)

Amir H Rahimi
Amir H Rahimi

Reputation: 179

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 v2.0

Upvotes: 6

Philipp Ostmeyer
Philipp Ostmeyer

Reputation: 51

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.

Upvotes: 5

Related Questions