question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injection of current folder path into TypeScript file

See original GitHub issue

I’m using nswag run /runtime:NetCore31 with the below nswag.json config file. In the settings for the openApiToTypeScriptClient code generator, when I added the value for extensionCode it does prepend the imports for the base classes to the file, but at the end of the file, just before the closing brace, it also injects a line of code that is the current file path (which results in code that doesn’t work).

{
  "runtime": "NetCore31",
  "defaultVariables": "Configuration=Release",
  "documentGenerator": {
    "webApiToOpenApi": {
      "defaultReferenceTypeNullHandling": "Null",
      "defaultDictionaryValueReferenceTypeNullHandling": "NotNull",
      "generateAbstractProperties": false,
      "flattenInheritanceHierarchy": false,
      "generateAbstractSchemas": true,
      "generateKnownTypes": true,
      "generateXmlObjects": false,
      "ignoreObsoleteProperties": false,
      "allowReferencesWithProperties": false,
      "generateEnumMappingDescription": true,
      "alwaysAllowAdditionalObjectProperties": false,
      "generateExamples": false,
      "schemaType": "OpenApi3",
      "serializerSettings": {
        "referenceLoopHandling": "Ignore",
        "maxDepth": 2
      },
      "serializerOptions": {},
      "excludedTypeNames": [],
      "defaultPropertyNameHandling": "Default",
      "defaultEnumHandling": "Integer",
      "title": "API",
      "description": "",
      "version": "1.0.0",
      "allowNullableBodyParameters": true,
      "defaultResponseReferenceTypeNullHandling": "Null",
      "useControllerSummaryAsTagDescription": true,
      "defaultUrlTemplate": "{controller}/{id?}",
      "isAspNetCore": true,
      "addMissingPathParameters": false,
      "controllerNames": [],
      "assemblyPaths": ["../bin/$(Configuration)/netcoreapp3.1/bootstrap.dll"],
      "referencePaths": ["../bin/$(Configuration)/netcoreapp3.1/"],
      "output": "swagger.json",
      "outputType": "OpenApi3"
    }
  },
  "codeGenerators": {
    "openApiToTypeScriptClient": {
      "codeGeneratorSettings": {},
      "className": "{controller}Client",
      "generateDtoTypes": true,
      "generateClientInterfaces": true,
      "generateClientClasses": true,
      "generateOptionalParameters": false,
      "excludedParameterNames": [],
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "template": "Axios",
      "promiseType": "Promise",
      "wrapDtoExceptions": false,
      "clientBaseClass": "BaseClient",
      "configurationClass": "ClientOptions",
      "useTransformOptionsMethod": true,
      "useTransformResultMethod": true,
      "baseUrlTokenName": "API_BASE_URL",
      "protectedMethods": [],
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": true,
      "queryNullValue": "",
      "exceptionClass": "ApiException",
      "useAbortSignal": false,
      "httpClass": "HttpClient",
      "withCredentials": true,
      "rxJsVersion": 6.0,
      "useSingletonProvider": false,
      "injectionTokenType": "OpaqueToken",
      "moduleName": "MyApi",
      "namespace": "",
      "typeScriptVersion": 2.7,
      "dateTimeType": "Date",
      "nullValue": "Undefined",
      "exportTypes": true,
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "markOptionalProperties": true,
      "generateCloneMethod": false,
      "typeStyle": "Class",
      "enumStyle": "Enum",
      "useLeafType": false,
      "classTypes": [],
      "extendedClasses": [],
      "extensionCode": "import { BaseClient, ClientOptions } from './base';",
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "inlineNamedDictionaries": false,
      "inlineNamedAny": false,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": "src/index.ts",
      "newLineBehavior": "Auto"
    }
  }
}

The command-line output looks like this:

> nswag run /runtime:NetCore31

NSwag NPM CLI
NSwag command line tool for .NET Core NetCore31, toolchain v13.9.2.0 (NJsonSchema v10.3.1.0 (Newtonsoft.Json v12.0.0.0))
Visit http://NSwag.org for more information.
NSwag bin directory: /home/drv/Repositories/path/node_modules/nswag/bin/binaries/NetCore31

Executing file 'nswag.json' with variables ''...
Done.

Duration: 00:00:01.9931161

The end of the generated typescript file looks like this:

/* tslint:disable */
/* eslint-disable */
//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v13.9.2.0 (NJsonSchema v10.3.1.0 (Newtonsoft.Json v12.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming

import { BaseClient, ClientOptions } from './base';
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelToken } from 'axios';

export module MyApi {

//... about 10,400 lines of typescript

function isAxiosError(obj: any | undefined): obj is AxiosError {
    return obj && obj.isAxiosError === true;
}

/home/drv/Repositories/path/
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
vozeldrcommented, Jul 28, 2021

Adding // to the beginning (or the end) didn’t work for me… but adding /* fix bug */ to the beginning did. Resulting line for extension code looks like this:

"extensionCode": "/* fix bug */ import { BaseClient, ClientOptions } from './base';",

This adds the import to the top where it belongs and then adds a line with the comment before the closing bracket in the file:

/* fix bug */
}
1reaction
vozeldrcommented, Feb 25, 2021

Yes, it is definitely using the extensionCode property that introduced the issue I reported. Another interesting thing is that on Windows, when it inserts the import statement it changes ‘./base’ to ‘\base’ which is another bug.

I changed my usage to instead have the fix-index postgenerate script insert the import statement and I stopped using extensionCode.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Current directory + file names in TypeScript - angular
In TypeScript, what is the right way to reference the current file and directory, like __filename and __dirname in Node.js, that would work ......
Read more >
Configuring TypeScript compiler
This article explores in depth essential TypeScript configuration options. We'll learn how to configure input and output files location, file types, ...
Read more >
TSConfig Reference - Docs on every TSConfig option
A TSConfig file in a directory indicates that the directory is the root of a TypeScript or JavaScript project... Compiler Options. Top Level....
Read more >
How to Add TypeScript to a JavaScript Project
5. run the tsc command to run the TypeScript compiler on the current folder. 6. notice that you got a hello.js file that...
Read more >
Understanding TypeScript's “Compilation Process” & the ...
When you run tsc command in a directory, TypeScript compiler looks for the tsconfig.json file in the current directory and if it doesn't ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found