From c7fa44cd0cc6cdfdf31f19e2eb8b7fb2422396df Mon Sep 17 00:00:00 2001 From: xhh Date: Thu, 4 Feb 2016 08:43:13 +0800 Subject: [PATCH] Fix ApiClient.isFileParam in browserify runtime Closes #2028 --- .../src/main/resources/Javascript/ApiClient.mustache | 12 +++++++++--- samples/client/petstore/javascript/src/ApiClient.js | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache index 006d8d50eb7..257eefbd2ad 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache @@ -88,9 +88,15 @@ * Check if the given parameter value is like file content. */ ApiClient.prototype.isFileParam = function isFileParam(param) { - // Buffer or fs.ReadStream in Node.js - if (typeof module === 'object' && module.exports && - (param instanceof Buffer || param instanceof require('fs').ReadStream)) { + // fs.ReadStream in Node.js (but not in runtime like browserify) + if (typeof window === 'undefined' && + typeof require === 'function' && + require('fs') && + param instanceof require('fs').ReadStream) { + return true; + } + // Buffer in Node.js + if (typeof Buffer === 'function' && param instanceof Buffer) { return true; } // Blob in browser diff --git a/samples/client/petstore/javascript/src/ApiClient.js b/samples/client/petstore/javascript/src/ApiClient.js index 7fdc0ca81dc..6a4bb33b082 100644 --- a/samples/client/petstore/javascript/src/ApiClient.js +++ b/samples/client/petstore/javascript/src/ApiClient.js @@ -88,9 +88,15 @@ * Check if the given parameter value is like file content. */ ApiClient.prototype.isFileParam = function isFileParam(param) { - // Buffer or fs.ReadStream in Node.js - if (typeof module === 'object' && module.exports && - (param instanceof Buffer || param instanceof require('fs').ReadStream)) { + // fs.ReadStream in Node.js (but not in runtime like browserify) + if (typeof window === 'undefined' && + typeof require === 'function' && + require('fs') && + param instanceof require('fs').ReadStream) { + return true; + } + // Buffer in Node.js + if (typeof Buffer === 'function' && param instanceof Buffer) { return true; } // Blob in browser