Merge pull request #1770 from seanpkps/master

provide option for controling rendering of model objects
This commit is contained in:
Tony Tam
2015-12-01 23:06:30 -08:00
16 changed files with 110 additions and 58 deletions

2
.gitignore vendored
View File

@@ -11,3 +11,5 @@ swagger-ui.sublime-workspace
.project .project
node_modules/* node_modules/*
/nbproject/private/ /nbproject/private/
dist/specs/
test/specs/local/

View File

@@ -98,6 +98,7 @@ booleanValues | SwaggerUI renders boolean data types as a dropdown. By default i
docExpansion | Controls how the API listing is displayed. It can be set to 'none' (default), 'list' (shows operations for each resource), or 'full' (fully expanded: shows operations and their details). docExpansion | Controls how the API listing is displayed. It can be set to 'none' (default), 'list' (shows operations for each resource), or 'full' (fully expanded: shows operations and their details).
apisSorter | Apply a sort to the API/tags list. It can be 'alpha' (sort by name) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged. apisSorter | Apply a sort to the API/tags list. It can be 'alpha' (sort by name) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
operationsSorter | Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged. operationsSorter | Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
defaultModelRendering | Controls how models are shown when the API is first rendered. (The user can always switch the rendering for a given model by clicking the 'Model' and 'Model Schema' links.) It can be set to 'model' or 'schema', and the default is 'schema'.
onComplete | This is a callback function parameter which can be passed to be notified of when SwaggerUI has completed rendering successfully. onComplete | This is a callback function parameter which can be passed to be notified of when SwaggerUI has completed rendering successfully.
onFailure | This is a callback function parameter which can be passed to be notified of when SwaggerUI encountered a failure was unable to render. onFailure | This is a callback function parameter which can be passed to be notified of when SwaggerUI encountered a failure was unable to render.
highlightSizeThreshold | Any size response below this threshold will be highlighted syntactically, attempting to highlight large responses can lead to browser hangs, not including a threshold will default to highlight all returned responses. highlightSizeThreshold | Any size response below this threshold will be highlighted syntactically, attempting to highlight large responses can lead to browser hangs, not including a threshold will default to highlight all returned responses.

2
dist/css/print.css vendored
View File

@@ -82,7 +82,7 @@
.swagger-section pre .vhdl .attribute, .swagger-section pre .vhdl .attribute,
.swagger-section pre .clojure .attribute, .swagger-section pre .clojure .attribute,
.swagger-section pre .coffeescript .property { .swagger-section pre .coffeescript .property {
color: #8888ff; color: #88F;
} }
.swagger-section pre .keyword, .swagger-section pre .keyword,
.swagger-section pre .id, .swagger-section pre .id,

2
dist/css/screen.css vendored
View File

@@ -82,7 +82,7 @@
.swagger-section pre .vhdl .attribute, .swagger-section pre .vhdl .attribute,
.swagger-section pre .clojure .attribute, .swagger-section pre .clojure .attribute,
.swagger-section pre .coffeescript .property { .swagger-section pre .coffeescript .property {
color: #8888ff; color: #88F;
} }
.swagger-section pre .keyword, .swagger-section pre .keyword,
.swagger-section pre .id, .swagger-section pre .id,

1
dist/index.html vendored
View File

@@ -70,6 +70,7 @@
}, },
docExpansion: "none", docExpansion: "none",
apisSorter: "alpha", apisSorter: "alpha",
defaultModelRendering: 'schema',
showRequestHeaders: false showRequestHeaders: false
}); });

30
dist/swagger-ui.js vendored
View File

@@ -24558,7 +24558,12 @@ window.SwaggerUi = Backbone.Router.extend({
// SwaggerUi accepts all the same options as SwaggerApi // SwaggerUi accepts all the same options as SwaggerApi
initialize: function(options) { initialize: function(options) {
options = options || {}; options = options || {};
if(!options.highlightSizeThreshold) {
if (options.defaultModelRendering !== 'model') {
options.defaultModelRendering = 'schema';
}
if (!options.highlightSizeThreshold) {
options.highlightSizeThreshold = 100000; options.highlightSizeThreshold = 100000;
} }
@@ -25136,6 +25141,14 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
this.parentId = this.model.parentId; this.parentId = this.model.parentId;
this.nickname = this.model.nickname; this.nickname = this.model.nickname;
this.model.encodedParentId = encodeURIComponent(this.parentId); this.model.encodedParentId = encodeURIComponent(this.parentId);
if (opts.swaggerOptions) {
this.model.defaultRendering = opts.swaggerOptions.defaultModelRendering;
if (opts.swaggerOptions.showRequestHeaders) {
this.model.showRequestHeaders = true;
}
}
return this; return this;
}, },
@@ -25291,12 +25304,9 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
signature: this.model.responseClassSignature signature: this.model.responseClassSignature
}; };
} }
var opts = this.options.swaggerOptions;
if (opts.showRequestHeaders) {
this.model.showRequestHeaders = true;
}
$(this.el).html(Handlebars.templates.operation(this.model)); $(this.el).html(Handlebars.templates.operation(this.model));
if (signatureModel) { if (signatureModel) {
signatureModel.defaultRendering = this.model.defaultRendering;
responseSignatureView = new SwaggerUi.Views.SignatureView({ responseSignatureView = new SwaggerUi.Views.SignatureView({
model: signatureModel, model: signatureModel,
router: this.router, router: this.router,
@@ -25355,6 +25365,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addParameter: function(param, consumes) { addParameter: function(param, consumes) {
// Render a parameter // Render a parameter
param.consumes = consumes; param.consumes = consumes;
param.defaultRendering = this.model.defaultRendering;
var paramView = new SwaggerUi.Views.ParameterView({ var paramView = new SwaggerUi.Views.ParameterView({
model: param, model: param,
tagName: 'tr', tagName: 'tr',
@@ -25365,6 +25376,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addStatusCode: function(statusCode) { addStatusCode: function(statusCode) {
// Render status codes // Render status codes
statusCode.defaultRendering = this.model.defaultRendering;
var statusCodeView = new SwaggerUi.Views.StatusCodeView({ var statusCodeView = new SwaggerUi.Views.StatusCodeView({
model: statusCode, model: statusCode,
tagName: 'tr', tagName: 'tr',
@@ -25893,7 +25905,8 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
var signatureModel = { var signatureModel = {
sampleJSON: this.model.sampleJSON, sampleJSON: this.model.sampleJSON,
isParam: true, isParam: true,
signature: this.model.signature signature: this.model.signature,
defaultRendering: this.model.defaultRendering
}; };
if (this.model.sampleJSON) { if (this.model.sampleJSON) {
@@ -26052,7 +26065,11 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
$(this.el).html(Handlebars.templates.signature(this.model)); $(this.el).html(Handlebars.templates.signature(this.model));
if (this.model.defaultRendering === 'model') {
this.switchToDescription();
} else {
this.switchToSnippet(); this.switchToSnippet();
}
this.isParam = this.model.isParam; this.isParam = this.model.isParam;
@@ -26113,6 +26130,7 @@ SwaggerUi.Views.StatusCodeView = Backbone.View.extend({
sampleJSON: JSON.stringify(this.router.api.models[this.model.responseModel].createJSONSample(), null, 2), sampleJSON: JSON.stringify(this.router.api.models[this.model.responseModel].createJSONSample(), null, 2),
isParam: false, isParam: false,
signature: this.router.api.models[this.model.responseModel].getMockSignature(), signature: this.router.api.models[this.model.responseModel].getMockSignature(),
defaultRendering: this.model.defaultRendering
}; };
var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'}); var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'});

File diff suppressed because one or more lines are too long

View File

@@ -13,9 +13,10 @@ var declare = require('gulp-declare');
var watch = require('gulp-watch'); var watch = require('gulp-watch');
var connect = require('gulp-connect'); var connect = require('gulp-connect');
var header = require('gulp-header'); var header = require('gulp-header');
var pkg = require('./package.json');
var order = require('gulp-order'); var order = require('gulp-order');
var jshint = require('gulp-jshint'); var jshint = require('gulp-jshint');
var pkg = require('./package.json');
var banner = ['/**', var banner = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>', ' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>', ' * @version v<%= pkg.version %>',
@@ -34,21 +35,6 @@ gulp.task('clean', function() {
.on('error', log); .on('error', log);
}); });
/**
* Processes Handlebars templates
*/
function templates() {
return gulp
.src(['./src/main/template/**/*'])
.pipe(handlebars())
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'Handlebars.templates',
noRedeclare: true, // Avoid duplicate declarations
}))
.on('error', log);
}
/** /**
* JShint all *.js files * JShint all *.js files
*/ */
@@ -61,19 +47,27 @@ gulp.task('lint', function () {
/** /**
* Build a distribution * Build a distribution
*/ */
gulp.task('dist', ['clean','lint'], function() { gulp.task('dist', ['clean', 'lint'], _dist);
function _dist() {
return es.merge( return es.merge(
gulp.src([ gulp.src([
'./src/main/javascript/**/*.js', './src/main/javascript/**/*.js',
'./node_modules/swagger-client/browser/swagger-client.js' './node_modules/swagger-client/browser/swagger-client.js'
]), ]),
templates() gulp
.src(['./src/main/template/**/*'])
.pipe(handlebars())
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'Handlebars.templates',
noRedeclare: true, // Avoid duplicate declarations
}))
.on('error', log)
) )
.pipe(order(['scripts.js', 'templates.js'])) .pipe(order(['scripts.js', 'templates.js']))
.pipe(concat('swagger-ui.js')) .pipe(concat('swagger-ui.js'))
.pipe(wrap('(function(){<%= contents %>}).call(this);')) .pipe(wrap('(function(){<%= contents %>}).call(this);'))
.pipe(header(banner, { pkg: pkg } )) .pipe(header(banner, { pkg: pkg }))
.pipe(gulp.dest('./dist')) .pipe(gulp.dest('./dist'))
.pipe(uglify()) .pipe(uglify())
.on('error', log) .on('error', log)
@@ -81,13 +75,14 @@ gulp.task('dist', ['clean','lint'], function() {
.on('error', log) .on('error', log)
.pipe(gulp.dest('./dist')) .pipe(gulp.dest('./dist'))
.pipe(connect.reload()); .pipe(connect.reload());
}); }
gulp.task('dev-dist', ['lint', 'dev-copy'], _dist);
/** /**
* Processes less files into CSS files * Processes less files into CSS files
*/ */
gulp.task('less', ['clean'], function() { gulp.task('less', ['clean'], _less);
function _less() {
return gulp return gulp
.src([ .src([
'./src/main/less/screen.less', './src/main/less/screen.less',
@@ -99,14 +94,14 @@ gulp.task('less', ['clean'], function() {
.on('error', log) .on('error', log)
.pipe(gulp.dest('./src/main/html/css/')) .pipe(gulp.dest('./src/main/html/css/'))
.pipe(connect.reload()); .pipe(connect.reload());
}); }
gulp.task('dev-less', _less);
/** /**
* Copy lib and html folders * Copy lib and html folders
*/ */
gulp.task('copy', ['less'], function() { gulp.task('copy', ['less'], _copy);
function _copy() {
// copy JavaScript files inside lib folder // copy JavaScript files inside lib folder
gulp gulp
.src(['./lib/**/*.{js,map}']) .src(['./lib/**/*.{js,map}'])
@@ -124,14 +119,28 @@ gulp.task('copy', ['less'], function() {
.src(['./src/main/html/**/*']) .src(['./src/main/html/**/*'])
.pipe(gulp.dest('./dist')) .pipe(gulp.dest('./dist'))
.on('error', log); .on('error', log);
}
gulp.task('dev-copy', ['dev-less', 'copy-local-specs'], _copy);
gulp.task('copy-local-specs', function () {
// copy the test specs
return gulp
.src(['./test/specs/**/*'])
.pipe(gulp.dest('./dist/specs'))
.on('error', log);
}); });
/** /**
* Watch for changes and recompile * Watch for changes and recompile
*/ */
gulp.task('watch', function() { gulp.task('watch', ['copy-local-specs'], function() {
return watch(['./src/**/*.{js,less,handlebars}'], function() { return watch([
gulp.start('default'); './src/**/*.{js,less,handlebars}',
'./src/main/html/*.html',
'./test/specs/**/*.{json,yaml}'
],
function() {
gulp.start('dev-dist');
}); });
}); });
@@ -149,6 +158,8 @@ function log(error) {
console.error(error.toString && error.toString()); console.error(error.toString && error.toString());
} }
gulp.task('default', ['dist', 'copy']); gulp.task('default', ['dist', 'copy']);
gulp.task('serve', ['connect', 'watch']); gulp.task('serve', ['connect', 'watch']);
gulp.task('dev', ['default'], function () {
gulp.start('serve');
});

View File

@@ -82,7 +82,7 @@
.swagger-section pre .vhdl .attribute, .swagger-section pre .vhdl .attribute,
.swagger-section pre .clojure .attribute, .swagger-section pre .clojure .attribute,
.swagger-section pre .coffeescript .property { .swagger-section pre .coffeescript .property {
color: #8888ff; color: #88F;
} }
.swagger-section pre .keyword, .swagger-section pre .keyword,
.swagger-section pre .id, .swagger-section pre .id,

View File

@@ -82,7 +82,7 @@
.swagger-section pre .vhdl .attribute, .swagger-section pre .vhdl .attribute,
.swagger-section pre .clojure .attribute, .swagger-section pre .clojure .attribute,
.swagger-section pre .coffeescript .property { .swagger-section pre .coffeescript .property {
color: #8888ff; color: #88F;
} }
.swagger-section pre .keyword, .swagger-section pre .keyword,
.swagger-section pre .id, .swagger-section pre .id,

View File

@@ -70,6 +70,7 @@
}, },
docExpansion: "none", docExpansion: "none",
apisSorter: "alpha", apisSorter: "alpha",
defaultModelRendering: 'schema',
showRequestHeaders: false showRequestHeaders: false
}); });

View File

@@ -13,7 +13,12 @@ window.SwaggerUi = Backbone.Router.extend({
// SwaggerUi accepts all the same options as SwaggerApi // SwaggerUi accepts all the same options as SwaggerApi
initialize: function(options) { initialize: function(options) {
options = options || {}; options = options || {};
if(!options.highlightSizeThreshold) {
if (options.defaultModelRendering !== 'model') {
options.defaultModelRendering = 'schema';
}
if (!options.highlightSizeThreshold) {
options.highlightSizeThreshold = 100000; options.highlightSizeThreshold = 100000;
} }

View File

@@ -19,6 +19,14 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
this.parentId = this.model.parentId; this.parentId = this.model.parentId;
this.nickname = this.model.nickname; this.nickname = this.model.nickname;
this.model.encodedParentId = encodeURIComponent(this.parentId); this.model.encodedParentId = encodeURIComponent(this.parentId);
if (opts.swaggerOptions) {
this.model.defaultRendering = opts.swaggerOptions.defaultModelRendering;
if (opts.swaggerOptions.showRequestHeaders) {
this.model.showRequestHeaders = true;
}
}
return this; return this;
}, },
@@ -174,12 +182,9 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
signature: this.model.responseClassSignature signature: this.model.responseClassSignature
}; };
} }
var opts = this.options.swaggerOptions;
if (opts.showRequestHeaders) {
this.model.showRequestHeaders = true;
}
$(this.el).html(Handlebars.templates.operation(this.model)); $(this.el).html(Handlebars.templates.operation(this.model));
if (signatureModel) { if (signatureModel) {
signatureModel.defaultRendering = this.model.defaultRendering;
responseSignatureView = new SwaggerUi.Views.SignatureView({ responseSignatureView = new SwaggerUi.Views.SignatureView({
model: signatureModel, model: signatureModel,
router: this.router, router: this.router,
@@ -238,6 +243,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addParameter: function(param, consumes) { addParameter: function(param, consumes) {
// Render a parameter // Render a parameter
param.consumes = consumes; param.consumes = consumes;
param.defaultRendering = this.model.defaultRendering;
var paramView = new SwaggerUi.Views.ParameterView({ var paramView = new SwaggerUi.Views.ParameterView({
model: param, model: param,
tagName: 'tr', tagName: 'tr',
@@ -248,6 +254,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addStatusCode: function(statusCode) { addStatusCode: function(statusCode) {
// Render status codes // Render status codes
statusCode.defaultRendering = this.model.defaultRendering;
var statusCodeView = new SwaggerUi.Views.StatusCodeView({ var statusCodeView = new SwaggerUi.Views.StatusCodeView({
model: statusCode, model: statusCode,
tagName: 'tr', tagName: 'tr',

View File

@@ -49,7 +49,8 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
var signatureModel = { var signatureModel = {
sampleJSON: this.model.sampleJSON, sampleJSON: this.model.sampleJSON,
isParam: true, isParam: true,
signature: this.model.signature signature: this.model.signature,
defaultRendering: this.model.defaultRendering
}; };
if (this.model.sampleJSON) { if (this.model.sampleJSON) {

View File

@@ -15,7 +15,11 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
$(this.el).html(Handlebars.templates.signature(this.model)); $(this.el).html(Handlebars.templates.signature(this.model));
if (this.model.defaultRendering === 'model') {
this.switchToDescription();
} else {
this.switchToSnippet(); this.switchToSnippet();
}
this.isParam = this.model.isParam; this.isParam = this.model.isParam;

View File

@@ -14,6 +14,7 @@ SwaggerUi.Views.StatusCodeView = Backbone.View.extend({
sampleJSON: JSON.stringify(this.router.api.models[this.model.responseModel].createJSONSample(), null, 2), sampleJSON: JSON.stringify(this.router.api.models[this.model.responseModel].createJSONSample(), null, 2),
isParam: false, isParam: false,
signature: this.router.api.models[this.model.responseModel].getMockSignature(), signature: this.router.api.models[this.model.responseModel].getMockSignature(),
defaultRendering: this.model.defaultRendering
}; };
var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'}); var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'});