Skip to content

Commit

Permalink
swagger-api#1892 fixed multiple definition loops
Browse files Browse the repository at this point in the history
  • Loading branch information
bodnia committed Jan 22, 2016
1 parent 9a5925f commit db32835
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 33 deletions.
30 changes: 20 additions & 10 deletions dist/swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -27127,15 +27127,18 @@ SwaggerUi.partials.signature = (function () {
}

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

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

function createSchemaXML (name, definition, models, config) {
var $ref = definition.$ref;
var $ref = _.isObject(definition) ? definition.$ref : null;
var output, index;
config = config || {};
config.modelsToIgnore = config.modelsToIgnore || [];
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models, config)
: getDescriptor(name, definition, models, config);

Expand All @@ -27145,14 +27148,23 @@ SwaggerUi.partials.signature = (function () {

switch (descriptor.type) {
case 'array':
return createArrayXML(descriptor);
output = createArrayXML(descriptor); break;
case 'object':
return createObjectXML(descriptor);
output = createObjectXML(descriptor); break;
case 'loop':
return getInfiniteLoopMessage(descriptor.name);
output = getInfiniteLoopMessage(descriptor.name); break;
default:
return createPrimitiveXML(descriptor);
output = createPrimitiveXML(descriptor);
}

if ($ref) {
index = config.modelsToIgnore.indexOf($ref);
if (index > -1) {
config.modelsToIgnore.splice(index, 1);
}
}

return output;
}

function Descriptor (name, type, definition, models, config) {
Expand All @@ -27174,12 +27186,10 @@ SwaggerUi.partials.signature = (function () {
var name = model.name || modelType;
var type = model.type || 'object';

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

if (!model.definition) {
Expand Down
20 changes: 10 additions & 10 deletions dist/swagger-ui.min.js

Large diffs are not rendered by default.

30 changes: 20 additions & 10 deletions src/main/javascript/view/partials/signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,15 +824,18 @@ SwaggerUi.partials.signature = (function () {
}

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

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

function createSchemaXML (name, definition, models, config) {
var $ref = definition.$ref;
var $ref = _.isObject(definition) ? definition.$ref : null;
var output, index;
config = config || {};
config.modelsToIgnore = config.modelsToIgnore || [];
var descriptor = _.isString($ref) ? getDescriptorByRef($ref, models, config)
: getDescriptor(name, definition, models, config);

Expand All @@ -842,14 +845,23 @@ SwaggerUi.partials.signature = (function () {

switch (descriptor.type) {
case 'array':
return createArrayXML(descriptor);
output = createArrayXML(descriptor); break;
case 'object':
return createObjectXML(descriptor);
output = createObjectXML(descriptor); break;
case 'loop':
return getInfiniteLoopMessage(descriptor.name);
output = getInfiniteLoopMessage(descriptor.name); break;
default:
return createPrimitiveXML(descriptor);
output = createPrimitiveXML(descriptor);
}

if ($ref) {
index = config.modelsToIgnore.indexOf($ref);
if (index > -1) {
config.modelsToIgnore.splice(index, 1);
}
}

return output;
}

function Descriptor (name, type, definition, models, config) {
Expand All @@ -871,12 +883,10 @@ SwaggerUi.partials.signature = (function () {
var name = model.name || modelType;
var type = model.type || 'object';

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

if (!model.definition) {
Expand Down
74 changes: 71 additions & 3 deletions test/unit/view/partials/signatureSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,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);
});
});

Expand Down Expand Up @@ -554,7 +554,7 @@ describe('SwaggerUi.partials.signature tests', function () {
'<id>1</id>' +
'<Loop2>' +
'<id>1</id>' +
'<!-- Infinite loop to model Loop1 -->' +
'<!-- Infinite loop $ref:Loop1 -->' +
'</Loop2>' +
'</Loop1>';
var schema = {
Expand All @@ -570,7 +570,7 @@ describe('SwaggerUi.partials.signature tests', function () {
'<id>1</id>' +
'<Loop2>' +
'<id>1</id>' +
'<!-- Infinite loop to model Loop1 -->' +
'<!-- Infinite loop $ref:Loop1 -->' +
'</Loop2>' +
'</Loop1>' +
'</Loop3>';
Expand All @@ -580,6 +580,74 @@ describe('SwaggerUi.partials.signature tests', function () {

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);
});
});
});
});

0 comments on commit db32835

Please sign in to comment.