Skip to content

Commit

Permalink
swagger-api#1892 infinite loop when rendering self-references
Browse files Browse the repository at this point in the history
  • Loading branch information
bodnia committed Jan 21, 2016
1 parent a3311c0 commit 65cd92c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/html/css/print.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
.swagger-section pre .vhdl .attribute,
.swagger-section pre .clojure .attribute,
.swagger-section pre .coffeescript .property {
color: #8888ff;
color: #88F;
}
.swagger-section pre .keyword,
.swagger-section pre .id,
Expand Down
2 changes: 1 addition & 1 deletion src/main/html/css/screen.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
.swagger-section pre .vhdl .attribute,
.swagger-section pre .clojure .attribute,
.swagger-section pre .coffeescript .property {
color: #8888ff;
color: #88F;
}
.swagger-section pre .keyword,
.swagger-section pre .id,
Expand Down
47 changes: 33 additions & 14 deletions src/main/javascript/view/partials/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ SwaggerUi.partials.signature = (function () {

var createArrayXML = function (descriptor) {
var name = descriptor.name;
var config = descriptor.config;
var definition = descriptor.definition;
var models = descriptor.models;
var value;
Expand All @@ -720,7 +721,7 @@ SwaggerUi.partials.signature = (function () {

if (!items) { return getErrorMessage(); }

value = createSchemaXML(name, items, models);
value = createSchemaXML(name, items, models, config);

xml = xml || {};

Expand Down Expand Up @@ -779,8 +780,9 @@ SwaggerUi.partials.signature = (function () {
function createObjectXML (descriptor) {
var name = descriptor.name;
var definition = descriptor.definition;
var config = descriptor.config;
var models = descriptor.models;
var isParam = descriptor.isParam;
var isParam = descriptor.config.isParam;
var serializedProperties;
var attrs = [];
var properties = definition.properties;
Expand All @@ -799,10 +801,12 @@ SwaggerUi.partials.signature = (function () {
serializedProperties = _.map(properties, function (prop, key) {
var xml, result;

if (isParam && prop.readOnly) { return ''; }
if (isParam && prop.readOnly) {
return '';
}

xml = prop.xml || {};
result = createSchemaXML(key, prop, models);
result = createSchemaXML(key, prop, models, config);

if (xml.attribute) {
attrs.push(result);
Expand All @@ -819,14 +823,18 @@ SwaggerUi.partials.signature = (function () {
return wrapTag(name, serializedProperties, attrs);
}

function getInfiniteLoopMessage (name) {
return '<!-- Infinite loop to model ' + name + ' -->';
}

function getErrorMessage () {
return '<!-- invalid XML -->';
}

function createSchemaXML (name, definition, models, isParam) {
function createSchemaXML (name, definition, models, config) {
var $ref = definition.$ref;
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models)
: getDescriptor(name, definition, models, isParam);
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models, config)
: getDescriptor(name, definition, models, config);

if (!descriptor) {
return getErrorMessage();
Expand All @@ -837,37 +845,48 @@ SwaggerUi.partials.signature = (function () {
return createArrayXML(descriptor);
case 'object':
return createObjectXML(descriptor);
case 'loop':
return getInfiniteLoopMessage(descriptor.name);
default:
return createPrimitiveXML(descriptor);
}
}

function Descriptor (name, type, definition, models, isParam) {
function Descriptor (name, type, definition, models, config) {
if (arguments.length < 4) {
throw new Error();
}

this.config = config || {};
this.config.modelsToIgnore = this.config.modelsToIgnore || [];
this.name = name;
this.definition = definition;
this.models = models;
this.type = type;
this.isParam = isParam;
}

function getDescriptorByRef($ref, models) {
function getDescriptorByRef($ref, models, config) {
var modelType = simpleRef($ref);
var model = models[modelType] || {};
var name = model.name || modelType;
var type = model.type || 'object';

config = config || {};
config.modelsToIgnore = config.modelsToIgnore || [];
if (config.modelsToIgnore.indexOf(name) > -1) {
type = 'loop';
} else {
config.modelsToIgnore.push(modelType);
}

if (!model.definition) {
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 xml = definition.xml || {};

Expand All @@ -877,13 +896,13 @@ SwaggerUi.partials.signature = (function () {

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) {
var prolog = '<?xml version="1.0"?>';

return formatXml(prolog + createSchemaXML('', definition, models, isParam));
return formatXml(prolog + createSchemaXML('', definition, models, { isParam: isParam } ));
}

return {
Expand Down
72 changes: 71 additions & 1 deletion test/unit/view/partials/signatureSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,44 @@ describe('SwaggerUi.partials.signature tests', function () {
'xml': { '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'
}
};

Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -510,6 +548,38 @@ describe('SwaggerUi.partials.signature tests', function () {

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 to model 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 to model Loop1 -->' +
'</Loop2>' +
'</Loop1>' +
'</Loop3>';
var schema = {
$ref: '#/definitions/Loop3'
};

expect(sut.createSchemaXML('', schema, models)).to.equal(expected);
});
});
});
});

0 comments on commit 65cd92c

Please sign in to comment.