fmvilas / swagger-node-codegen

An OpenAPI 3.x/Swagger 2 code generator for Node.js
Apache License 2.0
200 stars 55 forks source link

fs Error - node v8.91 #8

Closed khannarishir closed 7 years ago

khannarishir commented 7 years ago

Hi, while using the generator under node v8.91, the following error is thrown

fs.js:75 throw new TypeError('"options" must be a string or an object, got ' + ^

TypeError: "options" must be a string or an object, got number instead. at getOptions (fs.js:75:11) at Object.fs.writeFile (fs.js:1261:13) at C:/Users/RK/Documents/github/org/swagger-node-codegen/dist/codegen.js:127:18 at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

This piece of code causing this is fs.writeFile(target_file, content, 0, 'utf8', (err) => { if (err) throw err; }); at lines 47, 79 & 100 of lib/codegen.js https://github.com/fmvilas/swagger-node-codegen/blob/master/lib/codegen.js

This was not an issue in node v6.x because passing 0 triggered the default options

https://github.com/nodejs/node/blob/v6.x/lib/fs.js line 1306 node v6.x lib/fs.js fs.writeFile = function(path, data, options, callback_) { var callback = maybeCallback(arguments[arguments.length - 1]); if (!options || typeof options === 'function') { options = { encoding: 'utf8', mode: 0o666, flag: 'w' }; } else if (typeof options === 'string') { options = { encoding: options, mode: 0o666, flag: 'w' }; } else if (typeof options !== 'object') { throwOptionsError(options); }

This behaviour was updated in v8.x and the error being thrown is from the getOptions function from the node library v8.x

This was reported earlier #4 and as per the suggestion provided there removing 0 from the piece of code fixes this issue however those changes were not made part of the pull request.

fmvilas commented 7 years ago

Great stuff, thanks for spotting and fixing it!