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 46f603c commit 46556f5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion 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 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 swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -27015,6 +27015,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 @@ -27023,7 +27024,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 @@ -27082,8 +27083,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 @@ -27102,10 +27104,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 @@ -27122,14 +27126,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 @@ -27140,37 +27148,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 @@ -27180,13 +27199,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
6 changes: 3 additions & 3 deletions swagger-ui.min.js

Large diffs are not rendered by default.

0 comments on commit 46556f5

Please sign in to comment.