Add required swagger 1.2 items property for property type array

This commit is contained in:
Ritchie Martori 2014-07-31 15:52:52 -07:00
parent 8a1bd07f73
commit f7734fe5b3
2 changed files with 17 additions and 8 deletions

View File

@ -104,12 +104,18 @@ var modelHelper = module.exports = {
out.type = modelHelper.getPropType(out.type); out.type = modelHelper.getPropType(out.type);
if (out.type === 'array') { 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) { if (arrayItem) {
var arrayProp = prop.type[0]; if(typeof arrayItem === 'object') {
if (!arrayProp.type) arrayProp = {type: arrayProp}; out.items = modelHelper.LDLPropToSwaggerDataType(arrayItem);
out.items = modelHelper.LDLPropToSwaggerDataType(arrayProp); } 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') { } else if (out.type === 'date') {
out.type = 'string'; out.type = 'string';

View File

@ -86,7 +86,10 @@ describe('model-helper', function() {
array: [] array: []
}); });
var prop = def.properties.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() { it('converts [undefined] type', function() {
@ -96,7 +99,7 @@ describe('model-helper', function() {
array: [undefined] array: [undefined]
}); });
var prop = def.properties.array; 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() { it('converts "array" type', function() {
@ -104,7 +107,7 @@ describe('model-helper', function() {
array: 'array' array: 'array'
}); });
var prop = def.properties.array; var prop = def.properties.array;
expect(prop).to.eql({ type: 'array' }); expect(prop).to.eql({ type: 'array', items: { type: 'any' } });
}); });
}); });
}); });