Nswag在生成文件末尾添加生成文件的路径

问题描述 投票:0回答:1

我的 Nswag 生成文件遇到了一个奇怪的问题。在运行用于生成代码的脚本后,它成功地完成了它,但在文件末尾它添加了文件本身的路径。

我已经尝试过:

  • 删除node_modules文件夹,重新运行
    npm install
  • 删除package-lock.json文件,重新运行
    npm install
  • 重新运行
    npm install
  • 找到一个有这个问题的人,他在 GitHub 上发布了一个问题:https://github.com/RicoSuter/NSwag/issues/3172
  • 搜索谷歌深暗的第三页

脚本执行后

这就是我生成文件后得到的

function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
  throw new SwaggerException(message, status, response, headers, result);
}

C:\Users\USER_NAME\source\repos\SOLUTION_NAME\src\backend\api-client-extentions.ts

感谢任何帮助!

设置:

后端:

我有一个 C# 项目作为我的后端运行,提供了所有需要用 Nswag 翻译的控制器

前端

我有一个包含在 React 框架中的 Microsoft SharePoint Online WebPart 解决方案。

配置文件

config.api.nswag

{
  "runtime": "Default",
  "defaultVariables": null,
  "documentGenerator": {
    "fromDocument": {
      "url": "https://localhost:8081/swagger/BACKEND_PATH/swagger.json",
      "output": null,
      "newLineBehavior": "Auto"
    }
  },
  "codeGenerators": {
    "openApiToTypeScriptClient": {
      "className": "ApiClient",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.7,
      "template": "Fetch",
      "promiseType": "Promise",
      "httpClass": "HttpClient",
      "withCredentials": false,
      "useSingletonProvider": false,
      "injectionTokenType": "OpaqueToken",
      "rxJsVersion": 6.0,
      "dateTimeType": "Date",
      "nullValue": "Undefined",
      "generateClientClasses": true,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "exportTypes": true,
      "wrapDtoExceptions": true,
      "exceptionClass": "SwaggerException",
      "clientBaseClass": "ApiClientBase",
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "protectedMethods": [],
      "configurationClass": null,
      "useTransformOptionsMethod": true,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "SingleClientFromOperationId",
      "markOptionalProperties": true,
      "generateCloneMethod": false,
      "typeStyle": "Interface",
      "enumStyle": "Enum",
      "useLeafType": false,
      "classTypes": [],
      "extendedClasses": [],
      "extensionCode": "src/backend/api-client-extentions.ts",
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "excludedParameterNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": false,
      "baseUrlTokenName": "API_BASE_URL",
      "queryNullValue": "",
      "inlineNamedDictionaries": false,
      "inlineNamedAny": false,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "src/backend/api-client-generated.ts",
      "newLineBehavior": "Auto"
    },    
  }
}

package.json

{
  "name": "test-new-solution",
  "version": "0.0.1",
  "private": true,
  "main": "lib/index.js",
  "scripts": {
    "build": "gulp bundle",
    "clean": "gulp clean",
    "test": "gulp test",
    "generate:nswag:api": "nswag run config.api.nswag"
  },
  "dependencies": {
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "office-ui-fabric-react": "7.174.1",
    "@microsoft/sp-core-library": "1.14.0",
    "@microsoft/sp-property-pane": "1.14.0",
    "@microsoft/sp-webpart-base": "1.14.0",
    "@microsoft/sp-lodash-subset": "1.14.0",
    "@microsoft/sp-office-ui-fabric-core": "1.14.0",
    "nswag": "13.6.1"
  },
  "devDependencies": {
    "@types/react": "16.9.51",
    "@types/react-dom": "16.9.8",
    "@microsoft/sp-build-web": "1.14.0",
    "@microsoft/sp-tslint-rules": "1.14.0",
    "@microsoft/sp-module-interfaces": "1.14.0",
    "@microsoft/rush-stack-compiler-3.9": "0.4.47",
    "gulp": "~4.0.2",
    "ajv": "~5.2.2",
    "@types/webpack-env": "1.13.1",
    "@types/node": "^17.0.33"
  }
}

c# reactjs swagger web-parts nswag
1个回答
0
投票

我们有同样的问题。正如 github 问题所暗示的那样,它仅在使用 extensionCode 属性时发生。从我们的测试中我们发现,如果 nswag 找不到扩展代码文件 relative 到命令运行的位置,就会发生这种情况。

您应该能够使用相对路径使其工作,例如,如果您的

package.json
位于顶层,这可能就像将 extensionCode 属性更改为一样简单:

"extensionCode": "./src/backend/api-client-extentions.ts",

我们选择将我们的扩展代码文件放在与我们生成的客户端相同的位置,并添加了一个 powershell 脚本来从相同的位置调用 nswag,即在

config.api.nswag
:

"extensionCode": "api-client-extentions.ts",
...
"output": "api-client-generated.ts",

src/backend
中我们会有一个generate.ps1:

Set-Location -Path $PSScriptRoot

& "C:\Program Files (x86)\Rico Suter\NSwagStudio\Win\nswag.exe" run config.api.nswag

只要我们想重新生成客户端,它就可以从 VS Code 即时运行。

© www.soinside.com 2019 - 2024. All rights reserved.