From 400a6f73f16e22c56cbb8b64634af3c465ba2550 Mon Sep 17 00:00:00 2001 From: Robert MacLean Date: Wed, 25 May 2016 09:35:38 +0200 Subject: [PATCH 1/3] Content-type header works like the accept header, with support for arrays of values The content-type header did not support an array of header values (for example headers['Content-Type'] = ['application/json', 'text/json', 'application/xml', 'text/xml', 'application/x-www-form-urlencoded'];) it would just produce a single string (i.e. headers['Content-Type'] = 'application/json, text/json, application/xml, text/xml, application/x-www-form-urlencoded; Which is incorrect, it should be an array as Accept does. Refactored out the code from accept to a function (produceHeaderKeyValue) and changed Accept & Content-Type to use that one function. --- lib/codegen.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/codegen.js b/lib/codegen.js index 1226f870..d7893b34 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -54,6 +54,14 @@ var getPathToMethodName = function(opts, m, path){ return m.toLowerCase() + result[0].toUpperCase() + result.substring(1); }; +var produceHeaderKeyValue = function (name, values) { + var header = []; + header.value = []; + header.name = name; + header.value.push(values.map(function (value) { return '\'' + value + '\''; }).join(', ')); + return header; +} + var getViewForSwagger2 = function(opts, type){ var swagger = opts.swagger; var authorizedMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'COPY', 'HEAD', 'OPTIONS', 'LINK', 'UNLIK', 'PURGE', 'LOCK', 'UNLOCK', 'PROPFIND']; @@ -96,19 +104,13 @@ var getViewForSwagger2 = function(opts, type){ headers: [] }; - if(op.produces) { - var headers = []; - headers.value = []; - - headers.name = 'Accept'; - headers.value.push(op.produces.map(function(value) { return '\'' + value + '\''; }).join(', ')); - - method.headers.push(headers); + if (op.produces) { + method.headers.push(produceHeaderKeyValue("Accept", op.produces)); } var consumes = op.consumes || swagger.consumes; - if(consumes) { - method.headers.push({name: 'Content-Type', value: '\'' + consumes + '\'' }); + if (consumes) { + method.headers.push(produceHeaderKeyValue("Content-Type", consumes)); } var params = []; From 81015742e0f09069123dc665aff1372f88563f43 Mon Sep 17 00:00:00 2001 From: Robert MacLean Date: Wed, 25 May 2016 09:53:00 +0200 Subject: [PATCH 2/3] Strings to single quotes as per CircleCI --- lib/codegen.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/codegen.js b/lib/codegen.js index d7893b34..6a8d949f 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -105,12 +105,12 @@ var getViewForSwagger2 = function(opts, type){ }; if (op.produces) { - method.headers.push(produceHeaderKeyValue("Accept", op.produces)); + method.headers.push(produceHeaderKeyValue('Accept', op.produces)); } var consumes = op.consumes || swagger.consumes; if (consumes) { - method.headers.push(produceHeaderKeyValue("Content-Type", consumes)); + method.headers.push(produceHeaderKeyValue('Content-Type', consumes)); } var params = []; From 60945e85ee2809b40c5a92419d05e54c85ff1780 Mon Sep 17 00:00:00 2001 From: Robert MacLean Date: Wed, 25 May 2016 09:54:57 +0200 Subject: [PATCH 3/3] Adding missing semicolon --- lib/codegen.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/codegen.js b/lib/codegen.js index 6a8d949f..52614a1b 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -60,7 +60,7 @@ var produceHeaderKeyValue = function (name, values) { header.name = name; header.value.push(values.map(function (value) { return '\'' + value + '\''; }).join(', ')); return header; -} +}; var getViewForSwagger2 = function(opts, type){ var swagger = opts.swagger;