This commit is contained in:
Tony Tam
2016-01-22 16:43:19 -08:00
4 changed files with 244 additions and 48 deletions

65
dist/swagger-ui.js vendored
View File

@@ -27038,6 +27038,7 @@ SwaggerUi.partials.signature = (function () {
var createArrayXML = function (descriptor) { var createArrayXML = function (descriptor) {
var name = descriptor.name; var name = descriptor.name;
var config = descriptor.config;
var definition = descriptor.definition; var definition = descriptor.definition;
var models = descriptor.models; var models = descriptor.models;
var value; var value;
@@ -27046,7 +27047,7 @@ SwaggerUi.partials.signature = (function () {
if (!items) { return getErrorMessage(); } if (!items) { return getErrorMessage(); }
value = createSchemaXML(name, items, models); value = createSchemaXML(name, items, models, config);
xml = xml || {}; xml = xml || {};
@@ -27105,8 +27106,9 @@ SwaggerUi.partials.signature = (function () {
function createObjectXML (descriptor) { function createObjectXML (descriptor) {
var name = descriptor.name; var name = descriptor.name;
var definition = descriptor.definition; var definition = descriptor.definition;
var config = descriptor.config;
var models = descriptor.models; var models = descriptor.models;
var isParam = descriptor.isParam; var isParam = descriptor.config.isParam;
var serializedProperties; var serializedProperties;
var attrs = []; var attrs = [];
var properties = definition.properties; var properties = definition.properties;
@@ -27125,10 +27127,12 @@ SwaggerUi.partials.signature = (function () {
serializedProperties = _.map(properties, function (prop, key) { serializedProperties = _.map(properties, function (prop, key) {
var xml, result; var xml, result;
if (isParam && prop.readOnly) { return ''; } if (isParam && prop.readOnly) {
return '';
}
xml = prop.xml || {}; xml = prop.xml || {};
result = createSchemaXML(key, prop, models); result = createSchemaXML(key, prop, models, config);
if (xml.attribute) { if (xml.attribute) {
attrs.push(result); attrs.push(result);
@@ -27145,14 +27149,21 @@ SwaggerUi.partials.signature = (function () {
return wrapTag(name, serializedProperties, attrs); return wrapTag(name, serializedProperties, attrs);
} }
function getInfiniteLoopMessage (name) {
return '<!-- Infinite loop $ref:' + name + ' -->';
}
function getErrorMessage () { function getErrorMessage () {
return '<!-- invalid XML -->'; return '<!-- invalid XML -->';
} }
function createSchemaXML (name, definition, models, isParam) { function createSchemaXML (name, definition, models, config) {
var $ref = definition.$ref; var $ref = _.isObject(definition) ? definition.$ref : null;
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models) var output, index;
: getDescriptor(name, definition, models, isParam); config = config || {};
config.modelsToIgnore = config.modelsToIgnore || [];
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models, config)
: getDescriptor(name, definition, models, config);
if (!descriptor) { if (!descriptor) {
return getErrorMessage(); return getErrorMessage();
@@ -27160,40 +27171,58 @@ SwaggerUi.partials.signature = (function () {
switch (descriptor.type) { switch (descriptor.type) {
case 'array': case 'array':
return createArrayXML(descriptor); output = createArrayXML(descriptor); break;
case 'object': case 'object':
return createObjectXML(descriptor); output = createObjectXML(descriptor); break;
case 'loop':
output = getInfiniteLoopMessage(descriptor.name); break;
default: default:
return createPrimitiveXML(descriptor); output = createPrimitiveXML(descriptor);
}
if ($ref) {
index = config.modelsToIgnore.indexOf($ref);
if (index > -1) {
config.modelsToIgnore.splice(index, 1);
} }
} }
function Descriptor (name, type, definition, models, isParam) { return output;
}
function Descriptor (name, type, definition, models, config) {
if (arguments.length < 4) { if (arguments.length < 4) {
throw new Error(); throw new Error();
} }
this.config = config || {};
this.config.modelsToIgnore = this.config.modelsToIgnore || [];
this.name = name; this.name = name;
this.definition = definition; this.definition = definition;
this.models = models; this.models = models;
this.type = type; this.type = type;
this.isParam = isParam;
} }
function getDescriptorByRef($ref, models) { function getDescriptorByRef($ref, models, config) {
var modelType = simpleRef($ref); var modelType = simpleRef($ref);
var model = models[modelType] || {}; var model = models[modelType] || {};
var name = model.name || modelType; var name = model.name || modelType;
var type = model.type || 'object'; var type = model.type || 'object';
if (config.modelsToIgnore.indexOf($ref) > -1) {
type = 'loop';
} else {
config.modelsToIgnore.push($ref);
}
if (!model.definition) { if (!model.definition) {
return null; return null;
} }
return new Descriptor (name, type, model.definition, models); return new Descriptor(name, type, model.definition, models, config);
} }
function getDescriptor (name, definition, models, isParam){ function getDescriptor (name, definition, models, config){
var type = definition.type || 'object'; var type = definition.type || 'object';
var xml = definition.xml || {}; var xml = definition.xml || {};
@@ -27203,13 +27232,13 @@ SwaggerUi.partials.signature = (function () {
name = getName(name, xml); name = getName(name, xml);
return new Descriptor(name, type, definition, models,isParam); return new Descriptor(name, type, definition, models, config);
} }
function createXMLSample (definition, models, isParam) { function createXMLSample (definition, models, isParam) {
var prolog = '<?xml version="1.0"?>'; var prolog = '<?xml version="1.0"?>';
return formatXml(prolog + createSchemaXML('', definition, models, isParam)); return formatXml(prolog + createSchemaXML('', definition, models, { isParam: isParam } ));
} }
return { return {

File diff suppressed because one or more lines are too long

View File

@@ -712,6 +712,7 @@ SwaggerUi.partials.signature = (function () {
var createArrayXML = function (descriptor) { var createArrayXML = function (descriptor) {
var name = descriptor.name; var name = descriptor.name;
var config = descriptor.config;
var definition = descriptor.definition; var definition = descriptor.definition;
var models = descriptor.models; var models = descriptor.models;
var value; var value;
@@ -720,7 +721,7 @@ SwaggerUi.partials.signature = (function () {
if (!items) { return getErrorMessage(); } if (!items) { return getErrorMessage(); }
value = createSchemaXML(name, items, models); value = createSchemaXML(name, items, models, config);
xml = xml || {}; xml = xml || {};
@@ -779,8 +780,9 @@ SwaggerUi.partials.signature = (function () {
function createObjectXML (descriptor) { function createObjectXML (descriptor) {
var name = descriptor.name; var name = descriptor.name;
var definition = descriptor.definition; var definition = descriptor.definition;
var config = descriptor.config;
var models = descriptor.models; var models = descriptor.models;
var isParam = descriptor.isParam; var isParam = descriptor.config.isParam;
var serializedProperties; var serializedProperties;
var attrs = []; var attrs = [];
var properties = definition.properties; var properties = definition.properties;
@@ -799,10 +801,12 @@ SwaggerUi.partials.signature = (function () {
serializedProperties = _.map(properties, function (prop, key) { serializedProperties = _.map(properties, function (prop, key) {
var xml, result; var xml, result;
if (isParam && prop.readOnly) { return ''; } if (isParam && prop.readOnly) {
return '';
}
xml = prop.xml || {}; xml = prop.xml || {};
result = createSchemaXML(key, prop, models); result = createSchemaXML(key, prop, models, config);
if (xml.attribute) { if (xml.attribute) {
attrs.push(result); attrs.push(result);
@@ -819,14 +823,21 @@ SwaggerUi.partials.signature = (function () {
return wrapTag(name, serializedProperties, attrs); return wrapTag(name, serializedProperties, attrs);
} }
function getInfiniteLoopMessage (name) {
return '<!-- Infinite loop $ref:' + name + ' -->';
}
function getErrorMessage () { function getErrorMessage () {
return '<!-- invalid XML -->'; return '<!-- invalid XML -->';
} }
function createSchemaXML (name, definition, models, isParam) { function createSchemaXML (name, definition, models, config) {
var $ref = definition.$ref; var $ref = _.isObject(definition) ? definition.$ref : null;
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models) var output, index;
: getDescriptor(name, definition, models, isParam); config = config || {};
config.modelsToIgnore = config.modelsToIgnore || [];
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models, config)
: getDescriptor(name, definition, models, config);
if (!descriptor) { if (!descriptor) {
return getErrorMessage(); return getErrorMessage();
@@ -834,40 +845,58 @@ SwaggerUi.partials.signature = (function () {
switch (descriptor.type) { switch (descriptor.type) {
case 'array': case 'array':
return createArrayXML(descriptor); output = createArrayXML(descriptor); break;
case 'object': case 'object':
return createObjectXML(descriptor); output = createObjectXML(descriptor); break;
case 'loop':
output = getInfiniteLoopMessage(descriptor.name); break;
default: default:
return createPrimitiveXML(descriptor); output = createPrimitiveXML(descriptor);
}
if ($ref) {
index = config.modelsToIgnore.indexOf($ref);
if (index > -1) {
config.modelsToIgnore.splice(index, 1);
} }
} }
function Descriptor (name, type, definition, models, isParam) { return output;
}
function Descriptor (name, type, definition, models, config) {
if (arguments.length < 4) { if (arguments.length < 4) {
throw new Error(); throw new Error();
} }
this.config = config || {};
this.config.modelsToIgnore = this.config.modelsToIgnore || [];
this.name = name; this.name = name;
this.definition = definition; this.definition = definition;
this.models = models; this.models = models;
this.type = type; this.type = type;
this.isParam = isParam;
} }
function getDescriptorByRef($ref, models) { function getDescriptorByRef($ref, models, config) {
var modelType = simpleRef($ref); var modelType = simpleRef($ref);
var model = models[modelType] || {}; var model = models[modelType] || {};
var name = model.name || modelType; var name = model.name || modelType;
var type = model.type || 'object'; var type = model.type || 'object';
if (config.modelsToIgnore.indexOf($ref) > -1) {
type = 'loop';
} else {
config.modelsToIgnore.push($ref);
}
if (!model.definition) { if (!model.definition) {
return null; return null;
} }
return new Descriptor (name, type, model.definition, models); return new Descriptor(name, type, model.definition, models, config);
} }
function getDescriptor (name, definition, models, isParam){ function getDescriptor (name, definition, models, config){
var type = definition.type || 'object'; var type = definition.type || 'object';
var xml = definition.xml || {}; var xml = definition.xml || {};
@@ -877,13 +906,13 @@ SwaggerUi.partials.signature = (function () {
name = getName(name, xml); name = getName(name, xml);
return new Descriptor(name, type, definition, models,isParam); return new Descriptor(name, type, definition, models, config);
} }
function createXMLSample (definition, models, isParam) { function createXMLSample (definition, models, isParam) {
var prolog = '<?xml version="1.0"?>'; var prolog = '<?xml version="1.0"?>';
return formatXml(prolog + createSchemaXML('', definition, models, isParam)); return formatXml(prolog + createSchemaXML('', definition, models, { isParam: isParam } ));
} }
return { return {

View File

@@ -61,6 +61,44 @@ describe('SwaggerUi.partials.signature tests', function () {
'xml': { 'name': 'Tag' } 'xml': { 'name': 'Tag' }
}, },
'name': 'Tag' 'name': 'Tag'
},
'Loop1': {
'definition': {
'type': 'object',
'properties': {
'id': { 'type': 'integer'},
'loop2': {
'$ref': '#/definitions/Loop2'
}
},
'xml': { 'name': 'Loop1' }
},
'name': 'Loop1'
},
'Loop2': {
'definition': {
'type': 'object',
'properties': {
'id': { 'type': 'integer'},
'loop1': {
'$ref': '#/definitions/Loop1'
}
},
'xml': { 'name': 'Loop2' }
},
'name': 'Loop2'
},
'Loop3': {
'definition': {
'type': 'object',
'properties' : {
'loop1': {
'$ref': '#/definitions/Loop1'
}
},
'xml': { 'name': 'Loop3' }
},
'name': 'Loop3'
} }
}; };
@@ -438,7 +476,7 @@ describe('SwaggerUi.partials.signature tests', function () {
} }
}; };
expect(sut.createSchemaXML(name, definition, models, true)).to.equal(expected); expect(sut.createSchemaXML(name, definition, models, { isParam: true })).to.equal(expected);
}); });
it('returns object with passed parameter as attribute', function () { it('returns object with passed parameter as attribute', function () {
@@ -464,7 +502,7 @@ describe('SwaggerUi.partials.signature tests', function () {
} }
}; };
expect(sut.createSchemaXML(name, definition, models, true)).to.equal(expected); expect(sut.createSchemaXML(name, definition, models, {isParam: true})).to.equal(expected);
}); });
}); });
@@ -510,6 +548,106 @@ describe('SwaggerUi.partials.signature tests', function () {
expect(sut.createSchemaXML('', schema, models)).to.equal(expected); expect(sut.createSchemaXML('', schema, models)).to.equal(expected);
}); });
it('infinite loop Loop1 => Loop2, Loop2 => Loop1', function () {
var expected = '<Loop1>' +
'<id>1</id>' +
'<Loop2>' +
'<id>1</id>' +
'<!-- Infinite loop $ref:Loop1 -->' +
'</Loop2>' +
'</Loop1>';
var schema = {
$ref: '#/definitions/Loop1'
};
expect(sut.createSchemaXML('', schema, models)).to.equal(expected);
});
it('infinite loop Loop3 => Loop1, Loop1 => Loop2, Loop2 => Loop1', function () {
var expected = '<Loop3>' +
'<Loop1>' +
'<id>1</id>' +
'<Loop2>' +
'<id>1</id>' +
'<!-- Infinite loop $ref:Loop1 -->' +
'</Loop2>' +
'</Loop1>' +
'</Loop3>';
var schema = {
$ref: '#/definitions/Loop3'
};
expect(sut.createSchemaXML('', schema, models)).to.equal(expected);
});
it('infinite loop Loop1 => Loop2, Loop2 => Loop1 with 2 different loops on one level', function () {
var expected = '<Pet><Loop1>' +
'<id>1</id>' +
'<Loop2>' +
'<id>1</id>' +
'<!-- Infinite loop $ref:Loop1 -->' +
'</Loop2>' +
'</Loop1>' +
'<Loop2>' +
'<id>1</id>' +
'<Loop1>' +
'<id>1</id>' +
'<!-- Infinite loop $ref:Loop2 -->' +
'</Loop1>' +
'</Loop2>' +
'</Pet>';
var schema = {
type: 'object',
properties: {
item1: {
$ref: '#/definitions/Loop1'
},
item2: {
$ref: '#/definitions/Loop2'
}
},
xml: {
name: 'Pet'
}
};
expect(sut.createSchemaXML('', schema, models)).to.equal(expected);
});
it('infinite loop Loop1 => Loop2, Loop2 => Loop1 with 2 different loops on one level', function () {
var expected = '<Pet><Loop1>' +
'<id>1</id>' +
'<Loop2>' +
'<id>1</id>' +
'<!-- Infinite loop $ref:Loop1 -->' +
'</Loop2>' +
'</Loop1>' +
'<Loop1>' +
'<id>1</id>' +
'<Loop2>' +
'<id>1</id>' +
'<!-- Infinite loop $ref:Loop1 -->' +
'</Loop2>' +
'</Loop1>' +
'</Pet>';
var schema = {
type: 'object',
properties: {
item1: {
$ref: '#/definitions/Loop1'
},
item2: {
$ref: '#/definitions/Loop1'
}
},
xml: {
name: 'Pet'
}
};
expect(sut.createSchemaXML('', schema, models)).to.equal(expected);
});
}); });
}); });
}); });