update for swagger.js#14, #138, #139

This commit is contained in:
Tony Tam
2013-01-31 20:06:34 -05:00
parent 7a23876ce7
commit 1ec55a8829
9 changed files with 114 additions and 44 deletions

12
dist/lib/swagger.js vendored
View File

@@ -252,12 +252,16 @@
}; };
SwaggerResource.prototype.addOperations = function(resource_path, ops) { SwaggerResource.prototype.addOperations = function(resource_path, ops) {
var o, op, _i, _len, _results; var consumes, o, op, _i, _len, _results;
if (ops) { if (ops) {
_results = []; _results = [];
for (_i = 0, _len = ops.length; _i < _len; _i++) { for (_i = 0, _len = ops.length; _i < _len; _i++) {
o = ops[_i]; o = ops[_i];
op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, o.errorResponses, this, o.supportedContentTypes); consumes = o.consumes;
if (o.supportedContentTypes) {
consumes = o.supportedContentTypes;
}
op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, o.errorResponses, this, o.consumes);
this.operations[op.nickname] = op; this.operations[op.nickname] = op;
_results.push(this.operationsArray.push(op)); _results.push(this.operationsArray.push(op));
} }
@@ -415,7 +419,7 @@
SwaggerOperation = (function() { SwaggerOperation = (function() {
function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, supportedContentTypes) { function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, consumes) {
var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2, var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2,
_this = this; _this = this;
this.nickname = nickname; this.nickname = nickname;
@@ -427,7 +431,7 @@
this.responseClass = responseClass; this.responseClass = responseClass;
this.errorResponses = errorResponses; this.errorResponses = errorResponses;
this.resource = resource; this.resource = resource;
this.supportedContentTypes = supportedContentTypes; this.consumes = consumes;
this["do"] = __bind(this["do"], this); this["do"] = __bind(this["do"], this);
if (this.nickname == null) { if (this.nickname == null) {

79
dist/swagger-ui.js vendored
View File

@@ -192,8 +192,8 @@ function program1(depth0,data) {
var buffer = "", stack1, stack2; var buffer = "", stack1, stack2;
buffer += "\n "; buffer += "\n ";
foundHelper = helpers.supportedContentTypes; foundHelper = helpers.consumes;
stack1 = foundHelper || depth0.supportedContentTypes; stack1 = foundHelper || depth0.consumes;
stack2 = helpers.each; stack2 = helpers.each;
tmp1 = self.program(2, program2, data); tmp1 = self.program(2, program2, data);
tmp1.hash = {}; tmp1.hash = {};
@@ -225,8 +225,8 @@ function program4(depth0,data) {
return "\n <option value=\"application/json\">application/json</option>\n";} return "\n <option value=\"application/json\">application/json</option>\n";}
buffer += "<label for=\"contentType\"></label>\n<select name=\"contentType\">\n"; buffer += "<label for=\"contentType\"></label>\n<select name=\"contentType\">\n";
foundHelper = helpers.supportedContentTypes; foundHelper = helpers.consumes;
stack1 = foundHelper || depth0.supportedContentTypes; stack1 = foundHelper || depth0.consumes;
stack2 = helpers['if']; stack2 = helpers['if'];
tmp1 = self.program(1, program1, data); tmp1 = self.program(1, program1, data);
tmp1.hash = {}; tmp1.hash = {};
@@ -1132,7 +1132,7 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
// Generated by CoffeeScript 1.3.3 // Generated by CoffeeScript 1.4.0
(function() { (function() {
var ContentTypeView, HeaderView, MainView, OperationView, ParameterView, ResourceView, SignatureView, StatusCodeView, SwaggerUi, var ContentTypeView, HeaderView, MainView, OperationView, ParameterView, ResourceView, SignatureView, StatusCodeView, SwaggerUi,
__hasProp = {}.hasOwnProperty, __hasProp = {}.hasOwnProperty,
@@ -1436,9 +1436,14 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
$('.model-signature', $(this.el)).html(this.model.responseClass); $('.model-signature', $(this.el)).html(this.model.responseClass);
} }
contentTypeModel = { contentTypeModel = {
isParam: false, isParam: false
supportedContentTypes: this.model.supportedContentTypes
}; };
if (this.model.supportedContentTypes) {
contentTypeModel.consumes = this.model.supportedContentTypes;
}
if (this.model.consumes) {
contentTypeModel.consumes = this.model.consumes;
}
contentTypeView = new ContentTypeView({ contentTypeView = new ContentTypeView({
model: contentTypeModel model: contentTypeModel
}); });
@@ -1476,7 +1481,7 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
}; };
OperationView.prototype.submitOperation = function(e) { OperationView.prototype.submitOperation = function(e) {
var bodyParam, error_free, form, headerParams, invocationUrl, isFileUpload, map, o, obj, param, paramContentTypeField, responseContentTypeField, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, var bodyParam, consumes, error_free, form, headerParams, invocationUrl, isFileUpload, isFormPost, map, o, obj, param, paramContentTypeField, responseContentTypeField, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4,
_this = this; _this = this;
if (e != null) { if (e != null) {
e.preventDefault(); e.preventDefault();
@@ -1505,25 +1510,51 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
map[o.name] = o.value; map[o.name] = o.value;
} }
} }
isFileUpload = $('input[type~="file"]').size !== 0; isFileUpload = form.children().find('input[type~="file"]').size() !== 0;
if (isFileUpload) { isFormPost = false;
bodyParam = new FormData(); consumes = "application/json";
if (this.model.consumes && this.model.consumes.length > 0) {
consumes = this.model.consumes[0];
} else {
_ref1 = this.model.parameters; _ref1 = this.model.parameters;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
param = _ref1[_j]; o = _ref1[_j];
if (o.paramType === 'form') {
isFormPost = true;
consumes = false;
}
}
if (isFileUpload) {
consumes = false;
} else if (this.model.httpMethod.toLowerCase() === "post" && isFormPost === false) {
consumes = "application/json";
}
}
if (isFileUpload) {
bodyParam = new FormData();
_ref2 = this.model.parameters;
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
param = _ref2[_k];
if (param.paramType === 'body' && param.name !== 'file') { if (param.paramType === 'body' && param.name !== 'file') {
bodyParam.append(param.name, map[param.name]); bodyParam.append(param.name, map[param.name]);
} }
} }
$.each($('input[type~="file"]'), function(i, el) { $.each(form.children().find('input[type~="file"]'), function(i, el) {
return bodyParam.append($(el).attr('name'), el.files[0]); return bodyParam.append($(el).attr('name'), el.files[0]);
}); });
console.log(bodyParam); console.log(bodyParam);
} else if (isFormPost) {
bodyParam = new FormData();
_ref3 = this.model.parameters;
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
param = _ref3[_l];
bodyParam.append(param.name, map[param.name]);
}
} else { } else {
bodyParam = null; bodyParam = null;
_ref2 = this.model.parameters; _ref4 = this.model.parameters;
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) {
param = _ref2[_k]; param = _ref4[_m];
if (param.paramType === 'body') { if (param.paramType === 'body') {
bodyParam = map[param.name]; bodyParam = map[param.name];
} }
@@ -1540,6 +1571,7 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
url: invocationUrl, url: invocationUrl,
headers: headerParams, headers: headerParams,
data: bodyParam, data: bodyParam,
contentType: consumes,
dataType: 'json', dataType: 'json',
processData: false, processData: false,
error: function(xhr, textStatus, error) { error: function(xhr, textStatus, error) {
@@ -1552,12 +1584,6 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
return _this.showCompleteStatus(data); return _this.showCompleteStatus(data);
} }
}; };
if (obj.type.toLowerCase() === "post" || obj.type.toLowerCase() === "put" || obj.type.toLowerCase() === "patch") {
obj.contentType = "application/json";
}
if (isFileUpload) {
obj.contentType = false;
}
paramContentTypeField = $("td select[name=contentType]", $(this.el)).val(); paramContentTypeField = $("td select[name=contentType]", $(this.el)).val();
if (paramContentTypeField) { if (paramContentTypeField) {
obj.contentType = paramContentTypeField; obj.contentType = paramContentTypeField;
@@ -1760,9 +1786,14 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
$('.model-signature', $(this.el)).html(this.model.signature); $('.model-signature', $(this.el)).html(this.model.signature);
} }
contentTypeModel = { contentTypeModel = {
isParam: true, isParam: false
supportedContentTypes: this.model.supportedContentTypes
}; };
if (this.model.supportedContentTypes) {
contentTypeModel.consumes = this.model.supportedContentTypes;
}
if (this.model.consumes) {
contentTypeModel.consumes = this.model.consumes;
}
contentTypeView = new ContentTypeView({ contentTypeView = new ContentTypeView({
model: contentTypeModel model: contentTypeModel
}); });

File diff suppressed because one or more lines are too long

View File

@@ -252,12 +252,16 @@
}; };
SwaggerResource.prototype.addOperations = function(resource_path, ops) { SwaggerResource.prototype.addOperations = function(resource_path, ops) {
var o, op, _i, _len, _results; var consumes, o, op, _i, _len, _results;
if (ops) { if (ops) {
_results = []; _results = [];
for (_i = 0, _len = ops.length; _i < _len; _i++) { for (_i = 0, _len = ops.length; _i < _len; _i++) {
o = ops[_i]; o = ops[_i];
op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, o.errorResponses, this, o.supportedContentTypes); consumes = o.consumes;
if (o.supportedContentTypes) {
consumes = o.supportedContentTypes;
}
op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, o.errorResponses, this, o.consumes);
this.operations[op.nickname] = op; this.operations[op.nickname] = op;
_results.push(this.operationsArray.push(op)); _results.push(this.operationsArray.push(op));
} }
@@ -415,7 +419,7 @@
SwaggerOperation = (function() { SwaggerOperation = (function() {
function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, supportedContentTypes) { function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, consumes) {
var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2, var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2,
_this = this; _this = this;
this.nickname = nickname; this.nickname = nickname;
@@ -427,7 +431,7 @@
this.responseClass = responseClass; this.responseClass = responseClass;
this.errorResponses = errorResponses; this.errorResponses = errorResponses;
this.resource = resource; this.resource = resource;
this.supportedContentTypes = supportedContentTypes; this.consumes = consumes;
this["do"] = __bind(this["do"], this); this["do"] = __bind(this["do"], this);
if (this.nickname == null) { if (this.nickname == null) {

View File

@@ -1,6 +1,6 @@
{ {
"name": "swagger-ui", "name": "swagger-ui",
"version": "0.1.8", "version": "0.1.9",
"description": "Swagger UI is a dependency-free collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API", "description": "Swagger UI is a dependency-free collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API",
"scripts": { "scripts": {
"build": "PATH=$PATH:./node_modules/.bin cake dist", "build": "PATH=$PATH:./node_modules/.bin cake dist",

View File

@@ -27,7 +27,13 @@ class OperationView extends Backbone.View
contentTypeModel = contentTypeModel =
isParam: false isParam: false
supportedContentTypes: @model.supportedContentTypes
# support old syntax
if @model.supportedContentTypes
contentTypeModel.consumes = @model.supportedContentTypes
if @model.consumes
contentTypeModel.consumes = @model.consumes
contentTypeView = new ContentTypeView({model: contentTypeModel}) contentTypeView = new ContentTypeView({model: contentTypeModel})
$('.content-type', $(@el)).append contentTypeView.render().el $('.content-type', $(@el)).append contentTypeView.render().el
@@ -70,7 +76,23 @@ class OperationView extends Backbone.View
if(o.value? && jQuery.trim(o.value).length > 0) if(o.value? && jQuery.trim(o.value).length > 0)
map[o.name] = o.value map[o.name] = o.value
isFileUpload = $('input[type~="file"]').size != 0 isFileUpload = form.children().find('input[type~="file"]').size() != 0
isFormPost = false
consumes = "application/json"
if @model.consumes and @model.consumes.length > 0
# honor the consumes setting above everything else
consumes = @model.consumes[0]
else
for o in @model.parameters
if o.paramType == 'form'
isFormPost = true
consumes = false
if isFileUpload
consumes = false
else if @model.httpMethod.toLowerCase() == "post" and isFormPost is false
consumes = "application/json"
if isFileUpload if isFileUpload
# requires HTML5 compatible browser # requires HTML5 compatible browser
@@ -82,10 +104,14 @@ class OperationView extends Backbone.View
bodyParam.append(param.name, map[param.name]) bodyParam.append(param.name, map[param.name])
# add files # add files
$.each $('input[type~="file"]'), (i, el) -> $.each form.children().find('input[type~="file"]'), (i, el) ->
bodyParam.append($(el).attr('name'), el.files[0]) bodyParam.append($(el).attr('name'), el.files[0])
console.log(bodyParam) console.log(bodyParam)
else if isFormPost
bodyParam = new FormData()
for param in @model.parameters
bodyParam.append(param.name, map[param.name])
else else
bodyParam = null bodyParam = null
for param in @model.parameters for param in @model.parameters
@@ -113,6 +139,7 @@ class OperationView extends Backbone.View
url: invocationUrl url: invocationUrl
headers: headerParams headers: headerParams
data: bodyParam data: bodyParam
contentType: consumes
dataType: 'json' dataType: 'json'
processData: false processData: false
error: (xhr, textStatus, error) => error: (xhr, textStatus, error) =>
@@ -122,8 +149,6 @@ class OperationView extends Backbone.View
complete: (data) => complete: (data) =>
@showCompleteStatus(data) @showCompleteStatus(data)
obj.contentType = "application/json" if (obj.type.toLowerCase() == "post" or obj.type.toLowerCase() == "put" or obj.type.toLowerCase() == "patch")
obj.contentType = false if isFileUpload
paramContentTypeField = $("td select[name=contentType]", $(@el)).val() paramContentTypeField = $("td select[name=contentType]", $(@el)).val()
if paramContentTypeField if paramContentTypeField
obj.contentType = paramContentTypeField obj.contentType = paramContentTypeField

View File

@@ -20,8 +20,14 @@ class ParameterView extends Backbone.View
$('.model-signature', $(@el)).html(@model.signature) $('.model-signature', $(@el)).html(@model.signature)
contentTypeModel = contentTypeModel =
isParam: true isParam: false
supportedContentTypes: @model.supportedContentTypes
# support old syntax
if @model.supportedContentTypes
contentTypeModel.consumes = @model.supportedContentTypes
if @model.consumes
contentTypeModel.consumes = @model.consumes
contentTypeView = new ContentTypeView({model: contentTypeModel}) contentTypeView = new ContentTypeView({model: contentTypeModel})
$('.content-type', $(@el)).append contentTypeView.render().el $('.content-type', $(@el)).append contentTypeView.render().el

View File

@@ -1,7 +1,7 @@
<label for="contentType"></label> <label for="contentType"></label>
<select name="contentType"> <select name="contentType">
{{#if supportedContentTypes}} {{#if consumes}}
{{#each supportedContentTypes}} {{#each consumes}}
<option value="{{{this}}}">{{{this}}}</option> <option value="{{{this}}}">{{{this}}}</option>
{{/each}} {{/each}}
{{else}} {{else}}