#1248 moved render of parameter model to swagger-ui
This commit is contained in:
9227
dist/swagger-ui.js
vendored
9227
dist/swagger-ui.js
vendored
File diff suppressed because one or more lines are too long
20
dist/swagger-ui.min.js
vendored
20
dist/swagger-ui.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -229,7 +229,7 @@ window.SwaggerUi = Backbone.Router.extend({
|
|||||||
});
|
});
|
||||||
|
|
||||||
window.SwaggerUi.Views = {};
|
window.SwaggerUi.Views = {};
|
||||||
window.SwaggerUi.Views.partials = {};
|
window.SwaggerUi.partials = {};
|
||||||
|
|
||||||
// don't break backward compatibility with previous versions and warn users to upgrade their code
|
// don't break backward compatibility with previous versions and warn users to upgrade their code
|
||||||
(function(){
|
(function(){
|
||||||
|
|||||||
@@ -172,9 +172,9 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
|
|||||||
this.model.successDescription = value.description;
|
this.model.successDescription = value.description;
|
||||||
this.model.headers = this.parseResponseHeaders(value.headers);
|
this.model.headers = this.parseResponseHeaders(value.headers);
|
||||||
signatureModel = {
|
signatureModel = {
|
||||||
sampleJSON: JSON.stringify(SwaggerUi.Views.partials.jsonSignature.createJSONSample(value), void 0, 2),
|
sampleJSON: JSON.stringify(SwaggerUi.partials.jsonSignature.createJSONSample(value), void 0, 2),
|
||||||
isParam: false,
|
isParam: false,
|
||||||
signature: SwaggerUi.Views.partials.jsonSignature.getModelSignature(value.name, value.definition, value.models, value.modelPropertyMacro)
|
signature: SwaggerUi.partials.jsonSignature.getModelSignature(value.name, value.definition, value.models, value.modelPropertyMacro)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
|
|||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
var type = this.model.type || this.model.dataType;
|
var type = this.model.type || this.model.dataType;
|
||||||
|
var modelType = this.model.modelSignature.type;
|
||||||
|
var modelDefinitions = this.model.modelSignature.definitions;
|
||||||
|
|
||||||
if (typeof type === 'undefined') {
|
if (typeof type === 'undefined') {
|
||||||
var schema = this.model.schema;
|
var schema = this.model.schema;
|
||||||
@@ -47,9 +49,9 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
|
|||||||
$(this.el).html(template(this.model));
|
$(this.el).html(template(this.model));
|
||||||
|
|
||||||
var signatureModel = {
|
var signatureModel = {
|
||||||
sampleJSON: this.model.sampleJSON,
|
sampleJSON: SwaggerUi.partials.jsonSignature.createParameterJSONSample(modelType, modelDefinitions),
|
||||||
isParam: true,
|
isParam: true,
|
||||||
signature: this.model.signature,
|
signature: SwaggerUi.partials.jsonSignature.getParameterModelSignature(modelType, modelDefinitions),
|
||||||
defaultRendering: this.model.defaultRendering
|
defaultRendering: this.model.defaultRendering
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -65,12 +67,12 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
|
|||||||
|
|
||||||
if( this.options.swaggerOptions.jsonEditor && this.model.isBody && this.model.schema){
|
if( this.options.swaggerOptions.jsonEditor && this.model.isBody && this.model.schema){
|
||||||
var $self = $(this.el);
|
var $self = $(this.el);
|
||||||
this.model.jsonEditor =
|
this.model.jsonEditor =
|
||||||
/* global JSONEditor */
|
/* global JSONEditor */
|
||||||
new JSONEditor($('.editor_holder', $self)[0],
|
new JSONEditor($('.editor_holder', $self)[0],
|
||||||
{schema: this.model.schema, startval : this.model.default,
|
{schema: this.model.schema, startval : this.model.default,
|
||||||
ajax:true,
|
ajax:true,
|
||||||
disable_properties:true,
|
disable_properties:true,
|
||||||
disable_edit_json:true,
|
disable_edit_json:true,
|
||||||
iconlib: 'swagger' });
|
iconlib: 'swagger' });
|
||||||
// This is so that the signature can send back the sample to the json editor
|
// This is so that the signature can send back the sample to the json editor
|
||||||
|
|||||||
@@ -1,28 +1,113 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
SwaggerUi.Views.partials.jsonSignature = (function () {
|
/* jshint -W122 */
|
||||||
|
SwaggerUi.partials.jsonSignature = (function () {
|
||||||
|
var resolveSchema = function (schema) {
|
||||||
|
if (_.isPlainObject(schema.schema)) {
|
||||||
|
schema = resolveSchema(schema.schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
return schema;
|
||||||
|
};
|
||||||
|
|
||||||
|
var simpleRef = function (name) {
|
||||||
|
if (typeof name === 'undefined') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name.indexOf('#/definitions/') === 0) {
|
||||||
|
return name.substring('#/definitions/'.length);
|
||||||
|
} else {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var getInlineModel = function(inlineStr) {
|
||||||
|
if(/^Inline Model \d+$/.test(inlineStr)) {
|
||||||
|
var id = parseInt(inlineStr.substr('Inline Model'.length).trim(),10); //
|
||||||
|
var model = this.inlineModels[id];
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
// I'm returning null here, should I rather throw an error?
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
var formatXml = function(xml) {
|
||||||
|
var contexp, fn, formatted, indent, l, lastType, len, lines, ln, pad, reg, transitions, wsexp;
|
||||||
|
reg = /(>)(<)(\/*)/g;
|
||||||
|
wsexp = /[ ]*(.*)[ ]+\n/g;
|
||||||
|
contexp = /(<.+>)(.+\n)/g;
|
||||||
|
xml = xml.replace(reg, '$1\n$2$3').replace(wsexp, '$1\n').replace(contexp, '$1\n$2');
|
||||||
|
pad = 0;
|
||||||
|
formatted = '';
|
||||||
|
lines = xml.split('\n');
|
||||||
|
indent = 0;
|
||||||
|
lastType = 'other';
|
||||||
|
transitions = {
|
||||||
|
'single->single': 0,
|
||||||
|
'single->closing': -1,
|
||||||
|
'single->opening': 0,
|
||||||
|
'single->other': 0,
|
||||||
|
'closing->single': 0,
|
||||||
|
'closing->closing': -1,
|
||||||
|
'closing->opening': 0,
|
||||||
|
'closing->other': 0,
|
||||||
|
'opening->single': 1,
|
||||||
|
'opening->closing': 0,
|
||||||
|
'opening->opening': 1,
|
||||||
|
'opening->other': 1,
|
||||||
|
'other->single': 0,
|
||||||
|
'other->closing': -1,
|
||||||
|
'other->opening': 0,
|
||||||
|
'other->other': 0
|
||||||
|
};
|
||||||
|
fn = function(ln) {
|
||||||
|
var fromTo, j, key, padding, type, types, value;
|
||||||
|
types = {
|
||||||
|
single: Boolean(ln.match(/<.+\/>/)),
|
||||||
|
closing: Boolean(ln.match(/<\/.+>/)),
|
||||||
|
opening: Boolean(ln.match(/<[^!?].*>/))
|
||||||
|
};
|
||||||
|
type = ((function() {
|
||||||
|
var results;
|
||||||
|
results = [];
|
||||||
|
for (key in types) {
|
||||||
|
value = types[key];
|
||||||
|
if (value) {
|
||||||
|
results.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
})())[0];
|
||||||
|
type = type === void 0 ? 'other' : type;
|
||||||
|
fromTo = lastType + '->' + type;
|
||||||
|
lastType = type;
|
||||||
|
padding = '';
|
||||||
|
indent += transitions[fromTo];
|
||||||
|
padding = ((function() {
|
||||||
|
var m, ref1, results;
|
||||||
|
results = [];
|
||||||
|
for (j = m = 0, ref1 = indent; 0 <= ref1 ? m < ref1 : m > ref1; j = 0 <= ref1 ? ++m : --m) {
|
||||||
|
results.push(' ');
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
})()).join('');
|
||||||
|
if (fromTo === 'opening->closing') {
|
||||||
|
formatted = formatted.substr(0, formatted.length - 1) + ln + '\n';
|
||||||
|
} else {
|
||||||
|
formatted += padding + ln + '\n';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for (l = 0, len = lines.length; l < len; l++) {
|
||||||
|
ln = lines[l];
|
||||||
|
fn(ln);
|
||||||
|
}
|
||||||
|
return formatted;
|
||||||
|
};
|
||||||
|
|
||||||
var getModelSignature = function (name, schema, models, modelPropertyMacro) {
|
var getModelSignature = function (name, schema, models, modelPropertyMacro) {
|
||||||
var strongOpen = '<span class="strong">';
|
var strongOpen = '<span class="strong">';
|
||||||
var strongClose = '</span>';
|
var strongClose = '</span>';
|
||||||
var resolveSchema = function (schema) {
|
|
||||||
if (_.isPlainObject(schema.schema)) {
|
|
||||||
schema = resolveSchema(schema.schema);
|
|
||||||
}
|
|
||||||
|
|
||||||
return schema;
|
|
||||||
};
|
|
||||||
|
|
||||||
var simpleRef = function (name) {
|
|
||||||
if (typeof name === 'undefined') {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name.indexOf('#/definitions/') === 0) {
|
|
||||||
return name.substring('#/definitions/'.length);
|
|
||||||
} else {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var optionHtml = function (label, value) {
|
var optionHtml = function (label, value) {
|
||||||
return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
|
return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
|
||||||
@@ -372,26 +457,6 @@ SwaggerUi.Views.partials.jsonSignature = (function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var schemaToJSON = function (schema, models, modelsToIgnore, modelPropertyMacro) {
|
var schemaToJSON = function (schema, models, modelsToIgnore, modelPropertyMacro) {
|
||||||
var resolveSchema = function (schema) {
|
|
||||||
if (_.isPlainObject(schema.schema)) {
|
|
||||||
schema = resolveSchema(schema.schema);
|
|
||||||
}
|
|
||||||
|
|
||||||
return schema;
|
|
||||||
};
|
|
||||||
|
|
||||||
var simpleRef = function (name) {
|
|
||||||
if (typeof name === 'undefined') {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name.indexOf('#/definitions/') === 0) {
|
|
||||||
return name.substring('#/definitions/'.length);
|
|
||||||
} else {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Resolve the schema (Handle nested schemas)
|
// Resolve the schema (Handle nested schemas)
|
||||||
schema = resolveSchema(schema);
|
schema = resolveSchema(schema);
|
||||||
|
|
||||||
@@ -497,9 +562,92 @@ SwaggerUi.Views.partials.jsonSignature = (function () {
|
|||||||
return schemaToJSON(value.definition, value.models, modelsToIgnore, value.modelPropertyMacro);
|
return schemaToJSON(value.definition, value.models, modelsToIgnore, value.modelPropertyMacro);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var getParameterModelSignature = function (type, definitions) {
|
||||||
|
var isPrimitive, listType;
|
||||||
|
|
||||||
|
if (type instanceof Array) {
|
||||||
|
listType = true;
|
||||||
|
type = type[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert undefined to string of 'undefined'
|
||||||
|
if (typeof type === 'undefined') {
|
||||||
|
type = 'undefined';
|
||||||
|
isPrimitive = true;
|
||||||
|
|
||||||
|
} else if (definitions[type]){
|
||||||
|
// a model def exists?
|
||||||
|
type = definitions[type]; /* Model */
|
||||||
|
isPrimitive = false;
|
||||||
|
|
||||||
|
} else if (getInlineModel(type)) {
|
||||||
|
type = getInlineModel(type); /* Model */
|
||||||
|
isPrimitive = false;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// We default to primitive
|
||||||
|
isPrimitive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPrimitive) {
|
||||||
|
if (listType) {
|
||||||
|
return 'Array[' + type + ']';
|
||||||
|
} else {
|
||||||
|
return type.toString();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (listType) {
|
||||||
|
return 'Array[' + getModelSignature(type.name, type.definition, type.models, type.modelPropertyMacro) + ']';
|
||||||
|
} else {
|
||||||
|
return getModelSignature(type.name, type.definition, type.models, type.modelPropertyMacro);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var createParameterJSONSample = function (type, models) {
|
||||||
|
var listType, sampleJson, innerType;
|
||||||
|
models = models || {};
|
||||||
|
|
||||||
|
listType = (type instanceof Array);
|
||||||
|
innerType = listType ? type[0] : type;
|
||||||
|
|
||||||
|
if(models[innerType]) {
|
||||||
|
sampleJson = models[innerType].createJSONSample();
|
||||||
|
} else if (getInlineModel(innerType)){
|
||||||
|
sampleJson = getInlineModel(innerType).createJSONSample(); // may return null, if type isn't correct
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (sampleJson) {
|
||||||
|
sampleJson = listType ? [sampleJson] : sampleJson;
|
||||||
|
|
||||||
|
if (typeof sampleJson === 'string') {
|
||||||
|
return sampleJson;
|
||||||
|
} else if (_.isObject(sampleJson)) {
|
||||||
|
var t = sampleJson;
|
||||||
|
|
||||||
|
if (sampleJson instanceof Array && sampleJson.length > 0) {
|
||||||
|
t = sampleJson[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t.nodeName && typeof t === 'Node') {
|
||||||
|
var xmlString = new XMLSerializer().serializeToString(t);
|
||||||
|
|
||||||
|
return formatXml(xmlString);
|
||||||
|
} else {
|
||||||
|
return JSON.stringify(sampleJson, null, 2);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return sampleJson;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
getModelSignature: getModelSignature,
|
getModelSignature: getModelSignature,
|
||||||
createJSONSample: createJSONSample
|
createJSONSample: createJSONSample,
|
||||||
|
getParameterModelSignature: getParameterModelSignature,
|
||||||
|
createParameterJSONSample: createParameterJSONSample
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user