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

2

14

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

Ideatum answered 8/12, 2020 at 10:14 Comment(1)
I think that the -i option is inside of your v2.0 | v3.0 under key "input-spec":"http://localhost:port/whatever"Ague
S
4

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.

Schelling answered 31/12, 2020 at 15:14 Comment(2)
Thanks for the suggestion. I will have a try as soon as I have some time. I ended up going back to an earlier version.Ideatum
I had the same behavior. It was caused by an invalid Json file. Fixing the formal of the file fixed the problem.Dextrosinistral
S
4

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
Sigman answered 30/10, 2021 at 8:18 Comment(2)
Is the generator key 'v1.0' correct? I guess it's a typo and should be 'v2.0', right?Orogeny
you are right, thank you, it's a typo and should be v2.0Sigman

© 2022 - 2024 — McMap. All rights reserved.