diff --git a/lib/swagger.js b/lib/swagger.js index 1c08e41a..a7e75de5 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.3.3 (function() { - var SwaggerApi, SwaggerOperation, SwaggerRequest, SwaggerResource, + var SwaggerApi, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; SwaggerApi = (function() { @@ -110,6 +110,7 @@ return false; } } + this.setConsolidatedModels(); this.ready = true; if (this.success != null) { return this.success(); @@ -121,6 +122,29 @@ throw message; }; + SwaggerApi.prototype.setConsolidatedModels = function() { + var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results; + this.modelsArray = []; + this.models = {}; + _ref = this.resources; + for (resource_name in _ref) { + resource = _ref[resource_name]; + for (modelName in resource.models) { + if (!(this.models[modelName] != null)) { + this.models[modelName] = resource.models[modelName]; + this.modelsArray.push(resource.models[modelName]); + } + } + } + _ref1 = this.modelsArray; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + model = _ref1[_i]; + _results.push(model.setReferencedModels(this.models)); + } + return _results; + }; + SwaggerApi.prototype.suffixApiKey = function(url) { var sep; if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) { @@ -168,8 +192,11 @@ this.basePath = this.api.basePath; this.operations = {}; this.operationsArray = []; + this.modelsArray = []; + this.models = {}; if ((resourceObj.operations != null) && (this.api.resourcePath != null)) { - this.api.progress('reading resource ' + this.name + ' operations'); + this.api.progress('reading resource ' + this.name + ' models and operations'); + this.addModels(resourceObj.models); this.addOperations(resourceObj.path, resourceObj.operations); this.api[this.name] = this; } else { @@ -184,6 +211,7 @@ _this.basePath = response.basePath; _this.basePath = _this.basePath.replace(/\/$/, ''); } + _this.addModels(response.models); if (response.apis) { _ref = response.apis; for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -200,13 +228,31 @@ } } + SwaggerResource.prototype.addModels = function(models) { + var model, modelName, swaggerModel, _i, _len, _ref, _results; + if (models != null) { + for (modelName in models) { + swaggerModel = new SwaggerModel(modelName, models[modelName]); + this.modelsArray.push(swaggerModel); + this.models[modelName] = swaggerModel; + } + _ref = this.modelsArray; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + model = _ref[_i]; + _results.push(model.setReferencedModels(this.models)); + } + return _results; + } + }; + SwaggerResource.prototype.addOperations = function(resource_path, ops) { var o, op, _i, _len, _results; if (ops) { _results = []; for (_i = 0, _len = ops.length; _i < _len; _i++) { o = ops[_i]; - op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, this); + op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, this); this.operations[op.nickname] = op; _results.push(this.operationsArray.push(op)); } @@ -233,9 +279,109 @@ })(); + SwaggerModel = (function() { + + function SwaggerModel(modelName, obj) { + var propertyName; + this.name = obj.id != null ? obj.id : modelName; + this.properties = []; + for (propertyName in obj.properties) { + this.properties.push(new SwaggerModelProperty(propertyName, obj.properties[propertyName])); + } + } + + SwaggerModel.prototype.setReferencedModels = function(allModels) { + var prop, _i, _len, _ref, _results; + _ref = this.properties; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + prop = _ref[_i]; + if (allModels[prop.dataType] != null) { + _results.push(prop.refModel = allModels[prop.dataType]); + } else if ((prop.refDataType != null) && (allModels[prop.refDataType] != null)) { + _results.push(prop.refModel = allModels[prop.refDataType]); + } else { + _results.push(void 0); + } + } + return _results; + }; + + SwaggerModel.prototype.getMockSignature = function(prefix, modelToIgnore) { + var classClose, classOpen, prop, propertiesStr, returnVal, strong, strongClose, stronger, _i, _j, _len, _len1, _ref, _ref1; + propertiesStr = []; + _ref = this.properties; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + prop = _ref[_i]; + propertiesStr.push(prop.toString()); + } + strong = ''; + stronger = ''; + strongClose = ''; + classOpen = strong + 'class ' + this.name + '(' + strongClose; + classClose = strong + ')' + strongClose; + returnVal = classOpen + '' + propertiesStr.join(', ') + '' + classClose; + if (prefix != null) { + returnVal = stronger + prefix + strongClose + '
' + returnVal; + } + _ref1 = this.properties; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + prop = _ref1[_j]; + if ((prop.refModel != null) && (!(prop.refModel === modelToIgnore))) { + returnVal = returnVal + ('
' + prop.refModel.getMockSignature(void 0, this)); + } + } + return returnVal; + }; + + return SwaggerModel; + + })(); + + SwaggerModelProperty = (function() { + + function SwaggerModelProperty(name, obj) { + this.name = name; + this.dataType = obj.type; + this.isArray = this.dataType.toLowerCase() === 'array'; + this.descr = obj.description; + if (obj.items != null) { + if (obj.items.type != null) { + this.refDataType = obj.items.type; + } + if (obj.items.$ref != null) { + this.refDataType = obj.items.$ref; + } + } + this.dataTypeWithRef = this.refDataType != null ? this.dataType + '[' + this.refDataType + ']' : this.dataType; + if (obj.allowableValues != null) { + this.valueType = obj.allowableValues.valueType; + this.values = obj.allowableValues.values; + if (this.values != null) { + this.valuesString = "'" + this.values.join("' or '") + "'"; + } + } + } + + SwaggerModelProperty.prototype.toString = function() { + var str; + str = this.name + ': ' + this.dataTypeWithRef; + if (this.values != null) { + str += " = ['" + this.values.join("' or '") + "']"; + } + if (this.descr != null) { + str += ' {' + this.descr + '}'; + } + return str; + }; + + return SwaggerModelProperty; + + })(); + SwaggerOperation = (function() { - function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, resource) { + function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, resource) { var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _this = this; this.nickname = nickname; @@ -244,6 +390,7 @@ this.parameters = parameters != null ? parameters : []; this.summary = summary; this.notes = notes; + this.responseClass = responseClass; this.resource = resource; this["do"] = __bind(this["do"], this); @@ -260,6 +407,12 @@ this.httpMethod = this.httpMethod.toLowerCase(); this.isGetMethod = this.httpMethod === "get"; this.resourceName = this.resource.name; + if (this.responseClass.toLowerCase() === 'void') { + this.responseClass = void 0; + } + if (this.responseClass != null) { + this.responseClassSignature = this.getSignature(this.responseClass, this.resource.models); + } _ref = this.parameters; for (_i = 0, _len = _ref.length; _i < _len; _i++) { parameter = _ref[_i]; @@ -268,6 +421,7 @@ parameter.allowableValues = {}; parameter.allowableValues.values = this.resource.api.booleanValues; } + parameter.signature = this.getSignature(parameter.dataType, this.resource.models); if (parameter.allowableValues != null) { if (parameter.allowableValues.valueType === "RANGE") { parameter.isRange = true; @@ -299,6 +453,29 @@ }; } + SwaggerOperation.prototype.isListType = function(dataType) { + if (dataType.indexOf('[') >= 0) { + return dataType.substring(dataType.indexOf('[') + 1, dataType.indexOf(']')); + } else { + return void 0; + } + }; + + SwaggerOperation.prototype.getSignature = function(dataType, models) { + var isPrimitive, listType; + listType = this.isListType(dataType); + isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true; + if (isPrimitive) { + return dataType; + } else { + if (listType != null) { + return models[listType].getMockSignature(dataType); + } else { + return models[dataType].getMockSignature(dataType); + } + } + }; + SwaggerOperation.prototype["do"] = function(args, callback, error) { var body, headers; if (args == null) { diff --git a/src/main/coffeescript/SwaggerUi.coffee b/src/main/coffeescript/SwaggerUi.coffee index d7962a0c..10a0a1a3 100644 --- a/src/main/coffeescript/SwaggerUi.coffee +++ b/src/main/coffeescript/SwaggerUi.coffee @@ -54,7 +54,7 @@ class SwaggerUi extends Backbone.Router switch @options.docExpansion when "full" then Docs.expandOperationsForResource('') when "list" then Docs.collapseOperationsForResource('') - @options.onComplete() if @options.onComplete + @options.onComplete(@api, @) if @options.onComplete setTimeout( => Docs.shebang() diff --git a/src/main/coffeescript/view/ParameterView.coffee b/src/main/coffeescript/view/ParameterView.coffee index 8146dbd3..c6c5dd55 100644 --- a/src/main/coffeescript/view/ParameterView.coffee +++ b/src/main/coffeescript/view/ParameterView.coffee @@ -6,6 +6,7 @@ class ParameterView extends Backbone.View template = @template() $(@el).html(template(@model)) + @ # Return an appropriate template based on if the parameter is a list, readonly, required diff --git a/src/main/html/css/screen.css b/src/main/html/css/screen.css index e9d3bdd7..1f6d21c0 100644 --- a/src/main/html/css/screen.css +++ b/src/main/html/css/screen.css @@ -1502,3 +1502,16 @@ body ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operatio max-height: 400px; overflow-y: auto; } + + +.model-signature { + font-family: "Droid Sans", sans-serif; + font-size: 1em; + line-height: 1.5em; +} +.model-signature span { + font-size: 0.9em; + line-height: 1.5em; +} +.model-signature span:nth-child(odd) { color:#333; } +.model-signature span:nth-child(even) { color:#C5862B; } diff --git a/src/main/html/index.html b/src/main/html/index.html index 27acc8fe..d78acc38 100644 --- a/src/main/html/index.html +++ b/src/main/html/index.html @@ -47,8 +47,12 @@ dom_id:"swagger-ui-container", supportHeaderParams: false, supportedSubmitMethods: ['get', 'post', 'put'], - onComplete: function(){ - if(console) console.log("Loaded SwaggerUI") + onComplete: function(swaggerApi, swaggerUi){ + if(console) { + console.log("Loaded SwaggerUI") + console.log(swaggerApi); + console.log(swaggerUi); + } }, onFailure: function(data) { if(console) { @@ -56,7 +60,7 @@ console.log(data); } }, - docExpansion: "list" + docExpansion: "none" }); window.swaggerUi.load(); diff --git a/src/main/template/operation.handlebars b/src/main/template/operation.handlebars index db92675f..4398caed 100644 --- a/src/main/template/operation.handlebars +++ b/src/main/template/operation.handlebars @@ -21,15 +21,24 @@

Implementation Notes

{{{notes}}}

{{/if}} + {{#if responseClass}} +

Response Class

+ {{#if responseClassSignature}} +

{{{responseClassSignature}}}

+ {{else}} +

{{{responseClass}}}

+ {{/if}} + {{/if}}

Parameters

- - - + + + + diff --git a/src/main/template/param.handlebars b/src/main/template/param.handlebars index 00ff87e9..628f3577 100644 --- a/src/main/template/param.handlebars +++ b/src/main/template/param.handlebars @@ -17,5 +17,8 @@ {{/if}} - + + diff --git a/src/main/template/param_list.handlebars b/src/main/template/param_list.handlebars index 39c9633b..74ad29ff 100644 --- a/src/main/template/param_list.handlebars +++ b/src/main/template/param_list.handlebars @@ -17,5 +17,5 @@ {{/each}} - - + + \ No newline at end of file diff --git a/src/main/template/param_readonly.handlebars b/src/main/template/param_readonly.handlebars index 4730baf5..8d1a1af0 100644 --- a/src/main/template/param_readonly.handlebars +++ b/src/main/template/param_readonly.handlebars @@ -6,5 +6,5 @@ {{defaultValue}} {{/if}} - - + + diff --git a/src/main/template/param_readonly_required.handlebars b/src/main/template/param_readonly_required.handlebars index 84479fc8..3dcf7231 100644 --- a/src/main/template/param_readonly_required.handlebars +++ b/src/main/template/param_readonly_required.handlebars @@ -6,4 +6,5 @@ {{defaultValue}} {{/if}} - + + \ No newline at end of file diff --git a/src/main/template/param_required.handlebars b/src/main/template/param_required.handlebars index 7f7d6ec5..105e7064 100644 --- a/src/main/template/param_required.handlebars +++ b/src/main/template/param_required.handlebars @@ -15,6 +15,7 @@ {{/if}} {{/if}} - + \ No newline at end of file
ParameterValueDescriptionParameterValueDescriptionData Type
{{{description}}}{{{description}}} + {{{signature}}} + {{{description}}}{{{description}}}{{{signature}}}{{{description}}}{{{description}}}{{{signature}}}{{{description}}}{{{description}}}{{{signature}}} + {{{description}}} {{{signature}}}