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

o0lyfsai  于 2023-04-11  发布在  React
关注(0)|答案(1)|浏览(152)

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

  • 删除node_modules文件夹并重新运行npm install
  • 删除package-lock.json文件并重新运行npm install
  • 重新运行npm install
  • 发现一个人有这个问题,他在GitHub上发布了一个问题:https://github.com/RicoSuter/NSwag/issues/3172
  • 搜索Google的Deep Dark第3页

脚本执行后

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

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"
  }
}
dsekswqp

dsekswqp1#

我们遇到了同样的问题。正如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动态运行。

相关问题