From 3f55a556a33c8438529c78a8e3e3b9b6aa1d5825 Mon Sep 17 00:00:00 2001 From: Pouyan Heyratpour Date: Mon, 9 Oct 2017 19:47:53 +0330 Subject: [PATCH 1/3] Header params without override $http header - Enabled basic auth using function input api[namespace][path.operationId]({ Authorization: 'Basic ' + btoa(username + ':' + password) }) Signed-off-by: Pouyan Heyratpour --- dist/angular-swagger2-client.js | 57 ++++++++++++++++++--------- dist/angular-swagger2-client.ts | 69 ++++++++++++++++++++------------- 2 files changed, 80 insertions(+), 46 deletions(-) diff --git a/dist/angular-swagger2-client.js b/dist/angular-swagger2-client.js index 8aad085..7fd9711 100644 --- a/dist/angular-swagger2-client.js +++ b/dist/angular-swagger2-client.js @@ -108,6 +108,9 @@ return 'boolean'; else if (this.isLong(value)) return 'long'; + else if (this.isString(value)) + return 'string' + return 'undefined'; }, /** @@ -266,9 +269,19 @@ angular.forEach(rule, function (value, property) { if (swaggerObject.securityDefinitions.hasOwnProperty(property)) { var security = swaggerObject.securityDefinitions[property]; + + switch (security.type) { + case 'basic': + security.in = 'header'; + security.name = 'Authorization'; + break; + } + swaggerRequest.params[security.name] = { + "name": security.name, "required": true, "type": 'string', + "expected": 'string', "in": security.in, "format": security.type }; @@ -346,6 +359,28 @@ values: swaggerRequest.input, params: swaggerRequest.params }; + + /** + * Apply a pre-validator: check required, data type and format + * */ + angular.forEach(swaggerRequest.params, function (item, param) { + /** + * Check if is required + * */ + if (!swaggerRequest.input.hasOwnProperty(param) && item.required === true) { + finalResponse.validation.push(item.name + ' is required and should be a ' + item.expected); + } else if (swaggerRequest.input.hasOwnProperty(param)) { + if (!Utils.isDatatype(swaggerRequest.input[param], item.type)) { + finalResponse.validation.push(item.name + ' should be a ' + item.expected); + } else if ('header' === item.in) { + swaggerRequest.data.headers[param] = swaggerRequest.input[param]; + } + } + /** + * @todo Check format + * */ + }); + /** * By default, $http don't set content-type:application/x-www-form-urlencoded for POST and PUT * So, if you need send a POST or PUT, angular-swagger2-client set this header. @@ -363,6 +398,7 @@ swaggerRequest.data.headers['content-type'] = 'application/x-www-form-urlencoded'; } } + /** * Prepare formData only if the method is POST, PUT, CONNECT or PATCH * @todo learn more about when, where and how to use form data @@ -387,6 +423,7 @@ * */ swaggerRequest.data.formData = $httpParamSerializer(swaggerRequest.data.formData); } + /** * Prepare query params for all methods and only if it have one or more items * */ @@ -403,25 +440,7 @@ }); finalResponse.uri += '?' + $httpParamSerializer(httpQueryObject_1); } - /** - * Apply a pre-validator: check required, data type and format - * */ - angular.forEach(swaggerRequest.params, function (item, param) { - /** - * Check if is required - * */ - if (!swaggerRequest.input.hasOwnProperty(param) && param.required === true) { - finalResponse.validation.push(param.name + ' is required and should be a ' + item.expected); - } - else if (swaggerRequest.input.hasOwnProperty(param)) { - if (!Utils.isDatatype(swaggerRequest.input[param], param.type)) { - finalResponse.validation.push(param.name + ' should be a ' + param.expected); - } - } - /** - * @todo Check format - * */ - }); + var httpConfig = angular.extend({ url: this.host + finalResponse.uri, method: method, diff --git a/dist/angular-swagger2-client.ts b/dist/angular-swagger2-client.ts index 5215fe9..87ccf1b 100644 --- a/dist/angular-swagger2-client.ts +++ b/dist/angular-swagger2-client.ts @@ -97,6 +97,8 @@ else if (this.isFloat(value)) return 'float'; else if (this.isBoolean(value)) return 'boolean'; else if (this.isLong(value)) return 'long'; + else if (this.isString(value)) return 'string'; + return 'undefined'; }, /** @@ -261,9 +263,19 @@ angular.forEach(rule, function (value:any, property:any) { if (swaggerObject.securityDefinitions.hasOwnProperty(property)) { let security = swaggerObject.securityDefinitions[property]; + + switch (security.type) { + case 'basic': + security.in = 'header'; + security.name = 'Authorization'; + break; + } + swaggerRequest.params[security.name] = { + "name" : security.name, "required": true, "type" : 'string', + "expected": 'string', "in" : security.in, "format" : security.type }; @@ -356,7 +368,34 @@ params : swaggerRequest.params }; - + /** + * Apply a pre-validator: check required, data type and format + * */ + angular.forEach (swaggerRequest.params, function (item:any, param:any) { + /** + * Check if is required + * */ + if (!swaggerRequest.input.hasOwnProperty(param) && item.required === true) { + finalResponse.validation.push( + item.name + ' is required and should be a ' + item.expected + ); + } + /** + * Check data type + * */ + else if (swaggerRequest.input.hasOwnProperty(param)) { + if (!Utils.isDatatype(swaggerRequest.input[param], item.type)) { + finalResponse.validation.push( + item.name + ' should be a ' + item.expected + ) + } else if ('header' === item.in) { + swaggerRequest.data.headers[param] = swaggerRequest.input[param]; + } + } + /** + * @todo Check format + * */ + }); /** * By default, $http don't set content-type:application/x-www-form-urlencoded for POST and PUT @@ -403,6 +442,7 @@ * */ swaggerRequest.data.formData = $httpParamSerializer(swaggerRequest.data.formData); } + /** * Prepare query params for all methods and only if it have one or more items * */ @@ -418,32 +458,7 @@ }); finalResponse.uri += '?' + $httpParamSerializer(httpQueryObject); } - /** - * Apply a pre-validator: check required, data type and format - * */ - angular.forEach (swaggerRequest.params, function (item:any, param:any) { - /** - * Check if is required - * */ - if (!swaggerRequest.input.hasOwnProperty(param) && param.required === true) { - finalResponse.validation.push( - param.name + ' is required and should be a ' + item.expected - ); - } - /** - * Check data type - * */ - else if (swaggerRequest.input.hasOwnProperty(param)) { - if (!Utils.isDatatype(swaggerRequest.input[param], param.type)) { - finalResponse.validation.push( - param.name + ' should be a ' + param.expected - ) - } - } - /** - * @todo Check format - * */ - }); + let httpConfig = angular.extend({ url : this.host + finalResponse.uri, method : method, From 71f9fa6fdb42164fbaaed45f753df9305e56e34f Mon Sep 17 00:00:00 2001 From: Pouyan Heyratpour Date: Mon, 9 Oct 2017 20:02:06 +0330 Subject: [PATCH 2/3] Why multipart/form-dataf-8? Signed-off-by: Pouyan Heyratpour --- dist/angular-swagger2-client.js | 2 +- dist/angular-swagger2-client.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/angular-swagger2-client.js b/dist/angular-swagger2-client.js index 7fd9711..c261304 100644 --- a/dist/angular-swagger2-client.js +++ b/dist/angular-swagger2-client.js @@ -392,7 +392,7 @@ * Enable file upload * */ if (swaggerRequest.consumes.indexOf('multipart/form-data') > -1 && swaggerRequest.data.formData.length > 0) { - swaggerRequest.data.headers['content-type'] = 'multipart/form-dataf-8'; + swaggerRequest.data.headers['content-type'] = 'multipart/form-data'; } else { swaggerRequest.data.headers['content-type'] = 'application/x-www-form-urlencoded'; diff --git a/dist/angular-swagger2-client.ts b/dist/angular-swagger2-client.ts index 87ccf1b..0cb8984 100644 --- a/dist/angular-swagger2-client.ts +++ b/dist/angular-swagger2-client.ts @@ -408,7 +408,7 @@ * Enable file upload * */ if (swaggerRequest.consumes.indexOf('multipart/form-data') > -1 && swaggerRequest.data.formData.length > 0) { - swaggerRequest.data.headers['content-type'] = 'multipart/form-dataf-8'; + swaggerRequest.data.headers['content-type'] = 'multipart/form-data'; } else { swaggerRequest.data.headers['content-type'] = 'application/x-www-form-urlencoded'; From 41bcc607d0619a09699e36bb9555223427778bb3 Mon Sep 17 00:00:00 2001 From: Pouyan Heyratpour Date: Tue, 10 Oct 2017 06:15:44 +0330 Subject: [PATCH 3/3] Avoid changing the main swaggerRequest - Changing swaggerRequest.data.{query, formData} caused wrong further requests Signed-off-by: Pouyan Heyratpour --- dist/angular-swagger2-client.js | 17 ++++++++--------- dist/angular-swagger2-client.ts | 18 +++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/dist/angular-swagger2-client.js b/dist/angular-swagger2-client.js index c261304..b711cd5 100644 --- a/dist/angular-swagger2-client.js +++ b/dist/angular-swagger2-client.js @@ -352,6 +352,7 @@ // var finalResponse = { uri: swaggerRequest.uri, + formData: [], status: null, statusText: null, data: null, @@ -404,24 +405,23 @@ * @todo learn more about when, where and how to use form data */ if (['post', 'put', 'connect', 'patch'].indexOf(method) > -1) { + var formData = {}; + angular.forEach(swaggerRequest.data.formData, function (name, index) { if (swaggerRequest.input.hasOwnProperty(name)) { - swaggerRequest.data.formData[name] = swaggerRequest.input[name]; + formData[name] = swaggerRequest.input[name]; /** * If the parameter is found, it must be removed so that it is not reused in another parameter, * otherwise it could generate a totally unnecessary conflict. * */ delete swaggerRequest.input[name]; } - else { - delete swaggerRequest.data.formData[index]; - } }); /** * After searching and assigning the values to formData, it is necessary to convert formData * into a valid notation. * */ - swaggerRequest.data.formData = $httpParamSerializer(swaggerRequest.data.formData); + finalResponse.formData = $httpParamSerializer(formData); } /** @@ -429,22 +429,21 @@ * */ if (swaggerRequest.data.query.length > 0) { var httpQueryObject_1 = {}; + angular.forEach(swaggerRequest.data.query, function (name, index) { if (swaggerRequest.input.hasOwnProperty(name)) { httpQueryObject_1[name] = swaggerRequest.input[name]; delete swaggerRequest.input[name]; } - else { - delete swaggerRequest.data.query[index]; - } }); + finalResponse.uri += '?' + $httpParamSerializer(httpQueryObject_1); } var httpConfig = angular.extend({ url: this.host + finalResponse.uri, method: method, - data: swaggerRequest.data.formData, + data: finalResponse.formData, headers: swaggerRequest.data.headers }, swaggerRequest.config); // prepare promise diff --git a/dist/angular-swagger2-client.ts b/dist/angular-swagger2-client.ts index 0cb8984..ad77236 100644 --- a/dist/angular-swagger2-client.ts +++ b/dist/angular-swagger2-client.ts @@ -360,6 +360,7 @@ // let finalResponse:any = { uri : swaggerRequest.uri, + formData : [], status : null, statusText : null, data : null, @@ -420,27 +421,23 @@ * @todo learn more about when, where and how to use form data */ if (['post', 'put', 'connect', 'patch'].indexOf(method) > -1) { + let formData:any = {}; + angular.forEach(swaggerRequest.data.formData, function (name:string, index:any) { if (swaggerRequest.input.hasOwnProperty(name)) { - swaggerRequest.data.formData[name] = swaggerRequest.input[name]; + formData[name] = swaggerRequest.input[name]; /** * If the parameter is found, it must be removed so that it is not reused in another parameter, * otherwise it could generate a totally unnecessary conflict. * */ delete swaggerRequest.input[name]; } - /** - * The user may enter data that is not required or not used and this should not represent an error. - * **/ - else { - delete swaggerRequest.data.formData[index]; - } }); /** * After searching and assigning the values to formData, it is necessary to convert formData * into a valid notation. * */ - swaggerRequest.data.formData = $httpParamSerializer(swaggerRequest.data.formData); + finalResponse.formData = $httpParamSerializer(formData); } /** @@ -448,12 +445,11 @@ * */ if (swaggerRequest.data.query.length > 0) { let httpQueryObject:any = {}; + angular.forEach(swaggerRequest.data.query, function (name:string, index:any) { if (swaggerRequest.input.hasOwnProperty(name)) { httpQueryObject[name] = swaggerRequest.input[name]; delete swaggerRequest.input[name]; - } else { - delete swaggerRequest.data.query[index]; } }); finalResponse.uri += '?' + $httpParamSerializer(httpQueryObject); @@ -462,7 +458,7 @@ let httpConfig = angular.extend({ url : this.host + finalResponse.uri, method : method, - data : swaggerRequest.data.formData, + data : finalResponse.formData, headers : swaggerRequest.data.headers }, swaggerRequest.config);