Skip to content

Commit

Permalink
added sample representations for swagger-api/swagger-ui#934
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Feb 20, 2015
1 parent 052cb26 commit c16f451
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
26 changes: 17 additions & 9 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,23 +1275,23 @@ var Model = function(name, definition) {
};

Model.prototype.createJSONSample = function(modelsToIgnore) {
var i, result = {};
var i, result = {}, representations = {};
modelsToIgnore = (modelsToIgnore||{});
modelsToIgnore[this.name] = this;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
var sample = prop.getSampleValue(modelsToIgnore);
var sample = prop.getSampleValue(modelsToIgnore, representations);
result[prop.name] = sample;
}
delete modelsToIgnore[this.name];
return result;
};

Model.prototype.getSampleValue = function(modelsToIgnore) {
var i, obj = {};
var i, obj = {}, representations = {};
for(i = 0; i < this.properties.length; i++ ) {
var property = this.properties[i];
obj[property.name] = property.sampleValue(false, modelsToIgnore);
obj[property.name] = property.sampleValue(false, modelsToIgnore, representations);
}
return obj;
};
Expand Down Expand Up @@ -1356,8 +1356,8 @@ var Property = function(name, obj, required) {
this.multipleOf = obj.multipleOf || null;
};

Property.prototype.getSampleValue = function (modelsToIgnore) {
return this.sampleValue(false, modelsToIgnore);
Property.prototype.getSampleValue = function (modelsToIgnore, representations) {
return this.sampleValue(false, modelsToIgnore, representations);
};

Property.prototype.isArray = function () {
Expand All @@ -1368,21 +1368,29 @@ Property.prototype.isArray = function () {
return false;
};

Property.prototype.sampleValue = function(isArray, ignoredModels) {
Property.prototype.sampleValue = function(isArray, ignoredModels, representations) {
isArray = (isArray || this.isArray());
ignoredModels = (ignoredModels || {});
// representations = (representations || {});

var type = getStringSignature(this.obj, true);
var output;

if(this.$ref) {
var refModelName = simpleRef(this.$ref);
var refModel = models[refModelName];
if(typeof representations[type] !== 'undefined') {
return representations[type];
}
else

if(refModel && typeof ignoredModels[type] === 'undefined') {
ignoredModels[type] = this;
output = refModel.getSampleValue(ignoredModels);
output = refModel.getSampleValue(ignoredModels, representations);
representations[type] = output;
}
else {
output = refModelName;
output = (representations[type] || refModelName);
}
}
else if(this.example)
Expand Down
2 changes: 1 addition & 1 deletion lib/swagger-client.min.js

Large diffs are not rendered by default.

26 changes: 17 additions & 9 deletions src/js/swagger-a.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,23 +972,23 @@ var Model = function(name, definition) {
};

Model.prototype.createJSONSample = function(modelsToIgnore) {
var i, result = {};
var i, result = {}, representations = {};
modelsToIgnore = (modelsToIgnore||{});
modelsToIgnore[this.name] = this;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
var sample = prop.getSampleValue(modelsToIgnore);
var sample = prop.getSampleValue(modelsToIgnore, representations);
result[prop.name] = sample;
}
delete modelsToIgnore[this.name];
return result;
};

Model.prototype.getSampleValue = function(modelsToIgnore) {
var i, obj = {};
var i, obj = {}, representations = {};
for(i = 0; i < this.properties.length; i++ ) {
var property = this.properties[i];
obj[property.name] = property.sampleValue(false, modelsToIgnore);
obj[property.name] = property.sampleValue(false, modelsToIgnore, representations);
}
return obj;
};
Expand Down Expand Up @@ -1053,8 +1053,8 @@ var Property = function(name, obj, required) {
this.multipleOf = obj.multipleOf || null;
};

Property.prototype.getSampleValue = function (modelsToIgnore) {
return this.sampleValue(false, modelsToIgnore);
Property.prototype.getSampleValue = function (modelsToIgnore, representations) {
return this.sampleValue(false, modelsToIgnore, representations);
};

Property.prototype.isArray = function () {
Expand All @@ -1065,21 +1065,29 @@ Property.prototype.isArray = function () {
return false;
};

Property.prototype.sampleValue = function(isArray, ignoredModels) {
Property.prototype.sampleValue = function(isArray, ignoredModels, representations) {
isArray = (isArray || this.isArray());
ignoredModels = (ignoredModels || {});
// representations = (representations || {});

var type = getStringSignature(this.obj, true);
var output;

if(this.$ref) {
var refModelName = simpleRef(this.$ref);
var refModel = models[refModelName];
if(typeof representations[type] !== 'undefined') {
return representations[type];
}
else

if(refModel && typeof ignoredModels[type] === 'undefined') {
ignoredModels[type] = this;
output = refModel.getSampleValue(ignoredModels);
output = refModel.getSampleValue(ignoredModels, representations);
representations[type] = output;
}
else {
output = refModelName;
output = (representations[type] || refModelName);
}
}
else if(this.example)
Expand Down
35 changes: 35 additions & 0 deletions test/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,39 @@ describe('models', function() {
var sig = model.getMockSignature();
expect(sig).toEqual('<span class="strong">array {</span><div></div><span class="strong">}</span>');
});

it('should show the correct models', function() {
var test1 = {
"required": [ "test1" ],
"properties": {
"test1": {
"$ref": "#/definitions/badgerfishstring"
},
"test2": {
"$ref": "#/definitions/badgerfishstring"
},
"test3": {
"$ref": "#/definitions/badgerfishstring"
}
}
};

var badgerfishstring = {
"required": [ "$" ],
"properties": {
"$": { "type": "string" }
}
}

var test1Model = new swagger.Model('test1', test1);
swagger.addModel('test1', test1Model);
var badgerfishstringModel = new swagger.Model('badgerfishstring', badgerfishstring);
swagger.addModel('badgerfishstring', badgerfishstringModel);

expect(test1Model.createJSONSample()).toEqual({
test1: { '$': 'string' },
test2: { '$': 'string' },
test3: { '$': 'string' }
});
});
});

0 comments on commit c16f451

Please sign in to comment.