Adding 'snippet' tab to parameter datatype signature UI

This new section displays how a complex datatype should look like providing some sample code for the developer using Swagger.
This commit is contained in:
Alberto Pose
2012-11-14 17:33:38 -03:00
parent 086b24bb04
commit 9687a8084f
11 changed files with 176 additions and 5 deletions

View File

@@ -334,6 +334,17 @@
return returnVal;
};
SwaggerModel.prototype.createJSONSample = function(modelToIgnore) {
var prop, result, _i, _len, _ref;
result = {};
_ref = this.properties;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
prop = _ref[_i];
result[prop.name] = prop.getSampleValue(modelToIgnore);
}
return result;
};
return SwaggerModel;
})();
@@ -363,6 +374,24 @@
}
}
SwaggerModelProperty.prototype.getSampleValue = function(modelToIgnore) {
var result;
if ((this.refModel != null) && (!(this.refModel === modelToIgnore))) {
result = this.refModel.createJSONSample(this.refModel);
} else {
if (this.isArray) {
result = this.refDataType;
} else {
result = this.dataType;
}
}
if (this.isArray) {
return [result];
} else {
return result;
}
};
SwaggerModelProperty.prototype.toString = function() {
var str;
str = this.name + ': ' + this.dataTypeWithRef;
@@ -424,6 +453,7 @@
parameter.allowableValues.values = this.resource.api.booleanValues;
}
parameter.signature = this.getSignature(parameter.dataType, this.resource.models);
parameter.sampleJSON = this.getSampleJSON(parameter.dataType, this.resource.models);
if (parameter.allowableValues != null) {
if (parameter.allowableValues.valueType === "RANGE") {
parameter.isRange = true;
@@ -478,6 +508,17 @@
}
};
SwaggerOperation.prototype.getSampleJSON = function(dataType, models) {
var isPrimitive, listType, val;
listType = this.isListType(dataType);
isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true;
val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[dataType].createJSONSample());
if (val) {
val = listType ? [val] : val;
return JSON.stringify(val, null, 2);
}
};
SwaggerOperation.prototype["do"] = function(args, callback, error) {
var body, headers;
if (args == null) {