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

4
.gitignore vendored
View File

@@ -10,4 +10,6 @@ swagger-ui.sublime-workspace
.idea
.project
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).
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.
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.
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.

2
dist/css/print.css vendored
View File

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

2
dist/css/screen.css vendored
View File

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

1
dist/index.html vendored
View File

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

34
dist/swagger-ui.js vendored
View File

@@ -24558,7 +24558,12 @@ window.SwaggerUi = Backbone.Router.extend({
// SwaggerUi accepts all the same options as SwaggerApi
initialize: function(options) {
options = options || {};
if(!options.highlightSizeThreshold) {
if (options.defaultModelRendering !== 'model') {
options.defaultModelRendering = 'schema';
}
if (!options.highlightSizeThreshold) {
options.highlightSizeThreshold = 100000;
}
@@ -25136,6 +25141,14 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
this.parentId = this.model.parentId;
this.nickname = this.model.nickname;
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;
},
@@ -25291,12 +25304,9 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
signature: this.model.responseClassSignature
};
}
var opts = this.options.swaggerOptions;
if (opts.showRequestHeaders) {
this.model.showRequestHeaders = true;
}
$(this.el).html(Handlebars.templates.operation(this.model));
if (signatureModel) {
signatureModel.defaultRendering = this.model.defaultRendering;
responseSignatureView = new SwaggerUi.Views.SignatureView({
model: signatureModel,
router: this.router,
@@ -25355,6 +25365,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addParameter: function(param, consumes) {
// Render a parameter
param.consumes = consumes;
param.defaultRendering = this.model.defaultRendering;
var paramView = new SwaggerUi.Views.ParameterView({
model: param,
tagName: 'tr',
@@ -25365,6 +25376,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
addStatusCode: function(statusCode) {
// Render status codes
statusCode.defaultRendering = this.model.defaultRendering;
var statusCodeView = new SwaggerUi.Views.StatusCodeView({
model: statusCode,
tagName: 'tr',
@@ -25893,7 +25905,8 @@ SwaggerUi.Views.ParameterView = Backbone.View.extend({
var signatureModel = {
sampleJSON: this.model.sampleJSON,
isParam: true,
signature: this.model.signature
signature: this.model.signature,
defaultRendering: this.model.defaultRendering
};
if (this.model.sampleJSON) {
@@ -26052,8 +26065,12 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
$(this.el).html(Handlebars.templates.signature(this.model));
this.switchToSnippet();
if (this.model.defaultRendering === 'model') {
this.switchToDescription();
} else {
this.switchToSnippet();
}
this.isParam = this.model.isParam;
if (this.isParam) {
@@ -26113,6 +26130,7 @@ SwaggerUi.Views.StatusCodeView = Backbone.View.extend({
sampleJSON: JSON.stringify(this.router.api.models[this.model.responseModel].createJSONSample(), null, 2),
isParam: false,
signature: this.router.api.models[this.model.responseModel].getMockSignature(),
defaultRendering: this.model.defaultRendering
};
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 connect = require('gulp-connect');
var header = require('gulp-header');
var pkg = require('./package.json');
var order = require('gulp-order');
var jshint = require('gulp-jshint');
var pkg = require('./package.json');
var banner = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
@@ -34,21 +35,6 @@ gulp.task('clean', function() {
.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
*/
@@ -61,19 +47,27 @@ gulp.task('lint', function () {
/**
* Build a distribution
*/
gulp.task('dist', ['clean','lint'], function() {
gulp.task('dist', ['clean', 'lint'], _dist);
function _dist() {
return es.merge(
gulp.src([
gulp.src([
'./src/main/javascript/**/*.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(concat('swagger-ui.js'))
.pipe(wrap('(function(){<%= contents %>}).call(this);'))
.pipe(header(banner, { pkg: pkg } ))
.pipe(header(banner, { pkg: pkg }))
.pipe(gulp.dest('./dist'))
.pipe(uglify())
.on('error', log)
@@ -81,13 +75,14 @@ gulp.task('dist', ['clean','lint'], function() {
.on('error', log)
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});
}
gulp.task('dev-dist', ['lint', 'dev-copy'], _dist);
/**
* Processes less files into CSS files
*/
gulp.task('less', ['clean'], function() {
gulp.task('less', ['clean'], _less);
function _less() {
return gulp
.src([
'./src/main/less/screen.less',
@@ -99,14 +94,14 @@ gulp.task('less', ['clean'], function() {
.on('error', log)
.pipe(gulp.dest('./src/main/html/css/'))
.pipe(connect.reload());
});
}
gulp.task('dev-less', _less);
/**
* Copy lib and html folders
*/
gulp.task('copy', ['less'], function() {
gulp.task('copy', ['less'], _copy);
function _copy() {
// copy JavaScript files inside lib folder
gulp
.src(['./lib/**/*.{js,map}'])
@@ -124,15 +119,29 @@ gulp.task('copy', ['less'], function() {
.src(['./src/main/html/**/*'])
.pipe(gulp.dest('./dist'))
.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
*/
gulp.task('watch', function() {
return watch(['./src/**/*.{js,less,handlebars}'], function() {
gulp.start('default');
});
gulp.task('watch', ['copy-local-specs'], function() {
return watch([
'./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());
}
gulp.task('default', ['dist', 'copy']);
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 .clojure .attribute,
.swagger-section pre .coffeescript .property {
color: #8888ff;
color: #88F;
}
.swagger-section pre .keyword,
.swagger-section pre .id,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -15,8 +15,12 @@ SwaggerUi.Views.SignatureView = Backbone.View.extend({
$(this.el).html(Handlebars.templates.signature(this.model));
this.switchToSnippet();
if (this.model.defaultRendering === 'model') {
this.switchToDescription();
} else {
this.switchToSnippet();
}
this.isParam = this.model.isParam;
if (this.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),
isParam: false,
signature: this.router.api.models[this.model.responseModel].getMockSignature(),
defaultRendering: this.model.defaultRendering
};
var responseModelView = new SwaggerUi.Views.SignatureView({model: responseModel, tagName: 'div'});