You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NSwag生成文件末尾自动添加文件路径问题求助

NSwag生成文件末尾自动添加自身路径的异常问题

问题描述

使用NSwag生成TypeScript客户端文件时,脚本执行成功后,生成的文件末尾会自动追加该文件自身的本地路径,导致文件出现语法错误。

生成的文件示例:

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

已尝试的排查操作

  • 删除node_modules文件夹后重新执行npm install
  • 删除package-lock.json文件后重新执行npm install
  • 多次重新执行npm install
  • 查找并参考过类似的NSwag社区问题
  • 全网搜索相关解决方案

环境配置

  • 后端:提供Swagger接口的C#项目
  • 前端:基于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"
  }
}

求各位提供可行的解决方案,谢谢!

内容的提问来源于stack exchange,提问作者JohnRambo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 15:15:35