Open vozeldr opened 3 years ago
This problem is still occurring and can be replicated (100% of the time) on another developer's machine running Windows. I have a hacky workaround to the problem, but it would be great to have it fixed.
My package.json has the following scripts:
"generate": "nswag run /runtime:NetCore31",
"postgenerate": "node fix-index.js",
And then fix-index.js does the following:
const path = require('path');
const fs = require('fs');
/**
* For some reason, when nswag generates the index.ts file, it is placing the current directory path
* as the next-to-last line of the file, which is invalid javascript since it's like /home/...
* This removes lines that start with a / not immediately followed by another / or a *
*/
function removeBadLineFromGeneratedFile() {
const file = path.resolve(__dirname, 'src', 'index.ts');
fs.readFile(file, 'utf-8', (err, data) => {
if (err) throw new Error('Failed to read index.ts.');
const inputLines = data.toString().split('\n');
const outputLines = [];
inputLines.forEach((line) => {
if (line.toLowerCase().startsWith('c:') ||
(line.startsWith('/') && (line.charAt(1) !== '/' && line.charAt(1) !== '*'))) {
return;
}
outputLines.push(line);
});
fs.writeFile(file, outputLines.join('\n'), 'utf-8', (err) => {
if (err) throw new Error('Failed to write index.ts');
});
});
}
removeBadLineFromGeneratedFile();
could it be caused by the extensionCode parameter? does it create that line without extensionCode? what about an extension file?
Yes, it creates the extensionCode at the top of the file to inject my import statement. I'm not sure about an extension file. The problem did not occur before I added the extensionCode value.
I just verified the problem still exists with the 13.10.5 version of nswag.
Indeed, but if I remember correctly, the extension code is split : the imports are placed at the top while the rest is placed at the bottom. I suspect something is wrong here, as it's probably not-so-commonly used. Could you try to remove the extensionCode property completely? EDIT: other suggestion, could you try to add // to the end of the extension code and see what's generated?
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.
Taking some inspiration from @jeremyVignelles... Adding //
to the beginning of the extensionCode
value resolved the issue.
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 */
}
I suspect something is wrong here, as it's probably not-so-commonly used.
That's well possible as I also do not use this anymore... would need to be tested and fixed in NJsonSchema
I'm wondering why it is not fixed during 4 years :) ? And how is the NSwag usable at all if I cannot reference my custom code for token injection, etc.? This is important issue and it is 100% reproducible
@omykhayl we have been waiting for a brave soul like you who will implement it, please create a PR with the missing functionality.
I'm using
nswag run /runtime:NetCore31
with the belownswag.json
config file. In the settings for theopenApiToTypeScriptClient
code generator, when I added the value forextensionCode
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).The command-line output looks like this:
The end of the generated typescript file looks like this: