From f7734fe5b399c41cd9f0a95fb3ce6e95967e0d33 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Thu, 31 Jul 2014 15:52:52 -0700 Subject: [PATCH] Add required swagger 1.2 items property for property type array --- lib/model-helper.js | 16 +++++++++++----- test/model-helper.test.js | 9 ++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/model-helper.js b/lib/model-helper.js index d612489..e36ce6f 100644 --- a/lib/model-helper.js +++ b/lib/model-helper.js @@ -104,12 +104,18 @@ var modelHelper = module.exports = { out.type = modelHelper.getPropType(out.type); if (out.type === 'array') { - var hasItemType = typeof prop.type !== 'string' && prop.type[0]; + var hasItemType = Array.isArray(prop.type) && prop.type.length; + var arrayItem = hasItemType && prop.type[0]; - if (hasItemType) { - var arrayProp = prop.type[0]; - if (!arrayProp.type) arrayProp = {type: arrayProp}; - out.items = modelHelper.LDLPropToSwaggerDataType(arrayProp); + if (arrayItem) { + if(typeof arrayItem === 'object') { + out.items = modelHelper.LDLPropToSwaggerDataType(arrayItem); + } else { + out.items = { type: modelHelper.getPropType(arrayItem) }; + } + } else { + // NOTE: `any` is not a supported type in swagger 1.2 + out.items = { type: 'any' }; } } else if (out.type === 'date') { out.type = 'string'; diff --git a/test/model-helper.test.js b/test/model-helper.test.js index d196a26..0676767 100644 --- a/test/model-helper.test.js +++ b/test/model-helper.test.js @@ -86,7 +86,10 @@ describe('model-helper', function() { array: [] }); var prop = def.properties.array; - expect(prop).to.eql({ type: 'array' }); + expect(prop).to.eql({ + type: 'array', + items: { type: 'any' } + }); }); it('converts [undefined] type', function() { @@ -96,7 +99,7 @@ describe('model-helper', function() { array: [undefined] }); var prop = def.properties.array; - expect(prop).to.eql({ type: 'array' }); + expect(prop).to.eql({ type: 'array', items: { type: 'any' } }); }); it('converts "array" type', function() { @@ -104,7 +107,7 @@ describe('model-helper', function() { array: 'array' }); var prop = def.properties.array; - expect(prop).to.eql({ type: 'array' }); + expect(prop).to.eql({ type: 'array', items: { type: 'any' } }); }); }); });