From 03eb8023afc90f5afba7c7a1dbceccf23154212a Mon Sep 17 00:00:00 2001 From: Nick Pizzoferrato Date: Tue, 23 Aug 2016 00:11:32 -0500 Subject: [PATCH 1/5] make header parameters case insensitive --- lib/types/operation.js | 20 +++++++++++++------- test/headers.js | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/types/operation.js b/lib/types/operation.js index 036b12ba2..2247e40ff 100644 --- a/lib/types/operation.js +++ b/lib/types/operation.js @@ -431,20 +431,26 @@ Operation.prototype.supportedSubmitMethods = function () { Operation.prototype.getHeaderParams = function (args) { var headers = this.setContentTypes(args, {}); + var headerParamsByLowerCase = {}; for (var i = 0; i < this.parameters.length; i++) { var param = this.parameters[i]; - if (typeof args[param.name] !== 'undefined') { - if (param.in === 'header') { - var value = args[param.name]; + if (param.in === 'header') { + headerParamsByLowerCase[param.name.toLowerCase()] = param; + } + } - if (Array.isArray(value)) { - value = value.toString(); - } + for (var arg in args) { + var headerParam = headerParamsByLowerCase[arg.toLowerCase()]; + if (typeof headerParam !== 'undefined') { + var value = args[arg]; - headers[param.name] = value; + if (Array.isArray(value)) { + value = value.toString(); } + + headers[headerParam.name] = value; } } diff --git a/test/headers.js b/test/headers.js index 0a0069437..c47aae9f0 100644 --- a/test/headers.js +++ b/test/headers.js @@ -25,6 +25,24 @@ describe('header extraction', function () { expect(headers.myHeader).toBe('tony'); }); + it('should extract header params with case insensitivity', function () { + var parameters = [ + { + in: 'header', + name: 'myHeader', + type: 'string' + } + ]; + var op = new Operation({}, 'http', 'test', 'get', '/path', { parameters: parameters }); + var args = { + MyHeAdeR: 'nick' + }; + var url = op.urlify(args); + var headers = op.getHeaderParams(args); + + expect(url).toBe('http://localhost/path'); + expect(headers.myHeader).toBe('nick'); + }); it('should not URL encode header string values', function () { var parameters = [ From ee6f65b2c4a81809fc924b09bd2679d8f270556f Mon Sep 17 00:00:00 2001 From: cesine Date: Tue, 23 Aug 2016 11:34:38 -0400 Subject: [PATCH 2/5] making header parameters lowercase by default more examples of case insensitive headers --- lib/types/operation.js | 32 +++++++++++++++++++++++--------- test/client.js | 6 +++--- test/spec/v2/spec3.json | 14 +++++++++++++- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/lib/types/operation.js b/lib/types/operation.js index 2247e40ff..3251ea7c6 100644 --- a/lib/types/operation.js +++ b/lib/types/operation.js @@ -155,6 +155,11 @@ var Operation = module.exports = function (parent, scheme, operationId, httpMeth } } + // Make header params case insensitive RFC 2616 + if (param.in === 'header') { + param.name = param.name.toLowerCase(); + } + param.modelSignature = {type: innerType, definitions: this.models}; param.signature = this.getModelSignature(innerType, this.models).toString(); param.sampleJSON = this.getModelSampleJSON(innerType, this.models); @@ -431,27 +436,36 @@ Operation.prototype.supportedSubmitMethods = function () { Operation.prototype.getHeaderParams = function (args) { var headers = this.setContentTypes(args, {}); - var headerParamsByLowerCase = {}; + var headerParams = {}; + var headersSpecified = false; for (var i = 0; i < this.parameters.length; i++) { var param = this.parameters[i]; if (param.in === 'header') { - headerParamsByLowerCase[param.name.toLowerCase()] = param; + headerParams[param.name] = param; + headersSpecified = true; } } + if (!headersSpecified) { + return headers; + } + + // Look in args for for case-insenstive headers for (var arg in args) { - var headerParam = headerParamsByLowerCase[arg.toLowerCase()]; - if (typeof headerParam !== 'undefined') { - var value = args[arg]; + var headerParam = headerParams[arg.toLowerCase()]; + if (!headerParam) { + continue; + } - if (Array.isArray(value)) { - value = value.toString(); - } + var value = args[arg]; - headers[headerParam.name] = value; + if (Array.isArray(value)) { + value = value.toString(); } + + headers[headerParam.name] = value; } return headers; diff --git a/test/client.js b/test/client.js index 209ae7f6f..5832fa6dd 100644 --- a/test/client.js +++ b/test/client.js @@ -699,7 +699,7 @@ describe('SwaggerClient', function () { parameters: [ { in: 'header', - name: 'username', + name: 'UserNaMe', type: 'string' } ], @@ -731,7 +731,7 @@ describe('SwaggerClient', function () { **/ // ensure the headers are present - expect(requestObj.headers.username).toBe('bob'); + expect(requestObj.headers.username).toBe('Bob'); // rewrite this request to something that'll work locally requestObj.method = 'GET'; @@ -747,7 +747,7 @@ describe('SwaggerClient', function () { usePromise: true, requestInterceptor: interceptor.requestInterceptor }).then(function(client) { - client.nada.addFoo({username: 'bob'}).then(function (){ + client.nada.addFoo({Username: 'Bob'}).then(function (){ done(); }); }).catch(function(exception) { diff --git a/test/spec/v2/spec3.json b/test/spec/v2/spec3.json index b652beeb6..a92b766c8 100644 --- a/test/spec/v2/spec3.json +++ b/test/spec/v2/spec3.json @@ -25,6 +25,9 @@ "description": "Status values that need to be considered for filter", "required": false, "type": "string" + }, + { + "$ref": "#/parameters/LanguageHeader" } ], "responses": { @@ -40,5 +43,14 @@ } } } + }, + "parameters": { + "LanguageHeader": { + "name": "Accept-Language", + "in": "header", + "description": "Specify the user's language", + "required": false, + "type": "string" + } } -} \ No newline at end of file +} From b9dc78cc08f0673af6b2dc559e6a5538d929cf16 Mon Sep 17 00:00:00 2001 From: cesine Date: Tue, 23 Aug 2016 11:53:59 -0400 Subject: [PATCH 3/5] perserving case from swagger.json --- lib/types/operation.js | 32 +++++++++----------------------- test/client.js | 2 +- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/lib/types/operation.js b/lib/types/operation.js index 3251ea7c6..2247e40ff 100644 --- a/lib/types/operation.js +++ b/lib/types/operation.js @@ -155,11 +155,6 @@ var Operation = module.exports = function (parent, scheme, operationId, httpMeth } } - // Make header params case insensitive RFC 2616 - if (param.in === 'header') { - param.name = param.name.toLowerCase(); - } - param.modelSignature = {type: innerType, definitions: this.models}; param.signature = this.getModelSignature(innerType, this.models).toString(); param.sampleJSON = this.getModelSampleJSON(innerType, this.models); @@ -436,36 +431,27 @@ Operation.prototype.supportedSubmitMethods = function () { Operation.prototype.getHeaderParams = function (args) { var headers = this.setContentTypes(args, {}); - var headerParams = {}; - var headersSpecified = false; + var headerParamsByLowerCase = {}; for (var i = 0; i < this.parameters.length; i++) { var param = this.parameters[i]; if (param.in === 'header') { - headerParams[param.name] = param; - headersSpecified = true; + headerParamsByLowerCase[param.name.toLowerCase()] = param; } } - if (!headersSpecified) { - return headers; - } - - // Look in args for for case-insenstive headers for (var arg in args) { - var headerParam = headerParams[arg.toLowerCase()]; - if (!headerParam) { - continue; - } + var headerParam = headerParamsByLowerCase[arg.toLowerCase()]; + if (typeof headerParam !== 'undefined') { + var value = args[arg]; - var value = args[arg]; + if (Array.isArray(value)) { + value = value.toString(); + } - if (Array.isArray(value)) { - value = value.toString(); + headers[headerParam.name] = value; } - - headers[headerParam.name] = value; } return headers; diff --git a/test/client.js b/test/client.js index 5832fa6dd..b30f5f6b4 100644 --- a/test/client.js +++ b/test/client.js @@ -731,7 +731,7 @@ describe('SwaggerClient', function () { **/ // ensure the headers are present - expect(requestObj.headers.username).toBe('Bob'); + expect(requestObj.headers.UserNaMe).toBe('Bob'); // rewrite this request to something that'll work locally requestObj.method = 'GET'; From db693492ef2fe502f7c4de606c2c57b4a24bc66b Mon Sep 17 00:00:00 2001 From: cesine Date: Mon, 12 Sep 2016 20:21:30 -0400 Subject: [PATCH 4/5] adding docs for how to set headers --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index a3d590de1..e5db8b023 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,46 @@ You can add it to the swagger-client like such: client.clientAuthorizations.add('my-auth', new CustomRequestSigner()); ``` +### Setting headers + +Headers are a type of `parameter`, and can be passed with the other parameters. For example, if you supported translated pet details via the `Accept-Language` header: + +```js +"parameters": [ + { + "name": "petId", + "description": "ID of pet that needs to be fetched", + "required": true, + "type": "integer", + "format": "int64", + "paramType": "path", + "minimum": "1.0", + "defaultValue": 3, + "maximum": "100000.0" + }, + "LanguageHeader": { + "name": "Accept-Language", + "in": "header", + "description": "Specify the user's language", + "required": false, + "type": "string" + } +... +``` + +Then you would pass the header value via the parameters ([header parameters are case-insenstive](https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2)): + +```js + +client.pet.getPetById({ + petId: 7, + 'accept-language': 'fr' +}, function(pet){ + console.log('pet', pet); +}); + +``` + ### Using your own HTTP client Don't like [superagent](https://github.com/visionmedia/superagent)? Despise [JQuery](https://github.com/jquery/jquery)? Well, you're in luck. You can plug your own HTTP library easily: From e450b3cf4a53928c80d6018abf4687de431874b3 Mon Sep 17 00:00:00 2001 From: cesine Date: Mon, 12 Sep 2016 20:25:12 -0400 Subject: [PATCH 5/5] use tony --- test/headers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/headers.js b/test/headers.js index c47aae9f0..f1e4456b7 100644 --- a/test/headers.js +++ b/test/headers.js @@ -35,13 +35,13 @@ describe('header extraction', function () { ]; var op = new Operation({}, 'http', 'test', 'get', '/path', { parameters: parameters }); var args = { - MyHeAdeR: 'nick' + MyHeAdeR: 'tony' }; var url = op.urlify(args); var headers = op.getHeaderParams(args); expect(url).toBe('http://localhost/path'); - expect(headers.myHeader).toBe('nick'); + expect(headers.myHeader).toBe('tony'); }); it('should not URL encode header string values', function () {