OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.91k stars 6.58k forks source link

[BUG] typescript-axios(API Clients) could not build because of --typeRoots option(TypeScriptCompiler) when using yarn workspace #7104

Open hagevvashi opened 4 years ago

hagevvashi commented 4 years ago

Bug Report Checklist

Description

The issue is the build failure of API Clients - axios-typescript. The reason why this issue is problem for me is that I cannnot publish api client used in our project.

openapi-generator version

I use the latest version of openapi-generator (becasue it happend soon after I execute yarn add @openapitools/openapi-generator-cli).

OpenAPI declaration file content or url

The issue is not related to api specification.

Command line used for generation

I use npm.

openapi-generator generate --input-spec <specPath> --generator-name typescript-axios --output <dist> --config api.json
Steps to reproduce
.
├─node_modules
├─packages
│  ├─spec
│  │  ├─api.json
│  │  ├─package.json
│  │  └─openapi.yaml
│  └─client
│    └─...autoGeneratedFiles
├─package.json
└─yarn.lock

{
  "scripts": {
    "generate:client": "yarn workspace <workspaceName-spec> generate:client",
    "client:build": "yarn workspace <workspaceName-client> build"
  },
  "dependencies": {
    "axios": "^0.18.0"
  },
  "devDependencies": {
    "@types/node": "^12.11.5",
    "lerna": "^3.22.1",
    "typescript": "^3.6.4"
  },
  "workspaces": {
    "packages": {
      "packages/*"
    }
  }
}
{
  "scripts": {
    "generate:client": "openapi-generator generate --input-spec ./openapi.yaml --generator-name typescript-axios ../client --config api.json"
  },
  "devDependencies": {
    "@openapitools/openapi-generator-cli": "^1.0.15-4.3.1"
  }
}

Execute yarn client:build and you can see errors below.

yarn run v1.22.4
$ yarn workspace <workspaceName> build
$ tsc --outDir dist/
api.ts:15:34 - error TS2307: Cannot find module 'url' or its corresponding type declarations.
15 import * as globalImportUrl from 'url';
                                    ~~~~~
Found 1 error.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 2
Command: /Users/<Username>/.nodenv/versions/12.16.2/bin/node
Arguments: /Users/<Username>/Applications/homebrew/Cellar/yarn/1.22.4/libexec/lib/cli.js build
Directory: <ProjectPath>/packages/client
Output:
info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Related issues/PRs

In order to resolve #2394, --typeRoots was introduced.

Suggest a fix

I manually changed the generated tsconfig.json and then build succeeded.

diff is here

-    ],
-    "typeRoots": [
-      "node_modules/@types"
-    ]
+    ]

https://github.com/OpenAPITools/openapi-generator/blob/28ddad44e57109989b6fd17fffe0fa2403046102/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.mustache#L15-L17

or how about introducing options that disables the --typeRoots or enables the --typeRoots?

wing328 commented 4 years ago

@hagevvashi thanks for reporting the issue. Do you mind filing a PR with the suggested fix so that we can review and test more easily?

hagevvashi commented 4 years ago

@wing328

Thank you for your reply.

No, I don't mind filing a PR. I'm going to submit a PR.

sahanatroam commented 4 years ago

@hagevvashi @wing328 are you sure you don't need the url dependency in the codebase for the request to work? In my petstore example, it generated the following code that depends on the url package to be available as a dependency.

createPets: async (options: any = {}): Promise<RequestArgs> => {
            const localVarPath = `/pets`;
            const localVarUrlObj = globalImportUrl.parse(localVarPath, true);
            let baseOptions;
            if (configuration) {
                baseOptions = configuration.baseOptions;
            }
            const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
            const localVarHeaderParameter = {} as any;
            const localVarQueryParameter = {} as any;

            localVarUrlObj.query = {...localVarUrlObj.query, ...localVarQueryParameter, ...options.query};
            // fix override query string Detail: https://stackoverflow.com/a/7517673/1077943
            delete localVarUrlObj.search;
            let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
            localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};

            return {
                url: globalImportUrl.format(localVarUrlObj),
                options: localVarRequestOptions,
            };
        },
hagevvashi commented 4 years ago

@sahanatroam I don't know why generated code depends on url package. But I realize that below code resolve the issue.

    "typeRoots": [
      "node_modules/@types"
+     "node_modules/@types",
+     "../node_modules/@types"
    ]