JohnRambo
JohnRambo

Reputation: 518

Nswag adds the path of the generated file at the end of the generated file

I am running into a strange issue with my Nswag generated file. After running the scrip for generating the code it is doing it successfully, but at the end of the file it is adding the path of the file itself.

I have already tried to:

After script execution

That is what I get after I generate the file

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

Any help is appreciated!

Setup:

Backend:

I have a C# project running as my backend providing all of the needed controllers that have to be translated with the Nswag

Frontend

I have a Microsoft SharePoint Online WebPart solution wrapped in a React framework.

Config files

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

Upvotes: 4

Views: 2327

Answers (1)

user8607541
user8607541

Reputation:

We had the same problem. As the github issue suggests, it only happens when using the extensionCode property. From our testing we worked out that this happens if nswag can't locate the extension code file relative to where the command is run from.

You should be able to use a relative path to make it work, if your package.json is at the top level for example that may be as simple as changing the extensionCode property to:

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

We chose to put our extension code file in the same location as our generated client and added a powershell script to call nswag from the same location i.e. in config.api.nswag:

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

and in src/backend we would have a generate.ps1:

Set-Location -Path $PSScriptRoot

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

which can be run on the fly from VS Code whenever we want to regenerate the client.

Upvotes: 1

Related Questions