Ported extensions for more Swagger 1.2 metadata, returns+errors as responseMessages, consumes+produces, and X-Forwarded-Proto for reverse-proxying from HTTPS to HTTP

This commit is contained in:
Shelby Sanders 2014-08-04 18:19:09 -07:00
parent 2c737d40b6
commit 3dc7de6881
4 changed files with 37 additions and 17 deletions

View File

@ -23,7 +23,7 @@ var classHelper = module.exports = {
*/ */
generateAPIDoc: function(aClass, opts) { generateAPIDoc: function(aClass, opts) {
return { return {
apiVersion: opts.version, apiVersion: opts.version || '1',
swaggerVersion: opts.swaggerVersion, swaggerVersion: opts.swaggerVersion,
basePath: opts.basePath, basePath: opts.basePath,
resourcePath: urlJoin('/', opts.resourcePath), resourcePath: urlJoin('/', opts.resourcePath),

View File

@ -119,6 +119,16 @@ var routeHelper = module.exports = {
// be removed. // be removed.
var accepts = routeHelper.convertAcceptsToSwagger(route, classDef); var accepts = routeHelper.convertAcceptsToSwagger(route, classDef);
var returns = routeHelper.convertReturnsToSwagger(route, classDef); var returns = routeHelper.convertReturnsToSwagger(route, classDef);
var responseMessages = [
{
code: 200,
message: 'Request was successful',
responseModel: returns.model || prepareDataType(returns.type) || 'void'
}
];
if (route.errors) {
responseMessages.push.apply(responseMessages, route.errors);
}
debug('route %j', route); debug('route %j', route);
@ -128,17 +138,19 @@ var routeHelper = module.exports = {
// `items` and `format` fields. // `items` and `format` fields.
operations: [routeHelper.extendWithType({ operations: [routeHelper.extendWithType({
method: routeHelper.convertVerb(route.verb), method: routeHelper.convertVerb(route.verb),
deprecated: route.deprecated,
// [rfeng] Swagger UI doesn't escape '.' for jQuery selector // [rfeng] Swagger UI doesn't escape '.' for jQuery selector
nickname: route.method.replace(/\./g, '_'), nickname: route.method.replace(/\./g, '_'),
// Per the spec: // Per the spec:
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#523-operation-object // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#523-operation-object
// This is the only object that may have a type of 'void'. // This is the only object that may have a type of 'void'.
type: returns.model || returns.type || 'void', type: returns.model || returns.type || 'void',
parameters: accepts,
// TODO(schoon) - We don't have descriptions for this yet.
responseMessages: [],
summary: route.description, // TODO(schoon) - Excerpt? summary: route.description, // TODO(schoon) - Excerpt?
notes: '' // TODO(schoon) - `description` metadata? notes: route.notes, // TODO(schoon) - `description` metadata?
consumes: ['application/json', 'application/xml', 'text/xml'],
produces: ['application/json', 'application/javascript', 'application/xml', 'text/javascript', 'text/xml'],
parameters: accepts,
responseMessages: responseMessages
})] })]
}; };
@ -192,15 +204,21 @@ var routeHelper = module.exports = {
} }
var out = { var out = {
paramType: paramType || type,
name: name, name: name,
description: accepts.description,
type: accepts.type,
required: !!accepts.required, required: !!accepts.required,
paramType: paramType || type,
type: accepts.type,
$ref: accepts.model,
items: accepts.items,
uniqueItems: accepts.uniqueItems,
format: accepts.format,
pattern: accepts.pattern,
defaultValue: accepts.defaultValue, defaultValue: accepts.defaultValue,
enum: accepts.enum,
minimum: accepts.minimum, minimum: accepts.minimum,
maximum: accepts.maximum, maximum: accepts.maximum,
allowMultiple: false allowMultiple: accepts.allowMultiple,
description: accepts.description
}; };
out = routeHelper.extendWithType(out); out = routeHelper.extendWithType(out);

View File

@ -100,7 +100,8 @@ function addRoute(app, uri, doc) {
if (hasBasePath) { if (hasBasePath) {
var headers = req.headers; var headers = req.headers;
var host = headers.Host || headers.host; var host = headers.Host || headers.host;
doc.basePath = req.protocol + '://' + host + initialPath; var protocol = headers['x-forwarded-proto'] || headers['X-Forwarded-Proto'] || ctx.req.protocol
doc.basePath = protocol + '://' + host + initialPath;
} }
res.status(200).send(doc); res.status(200).send(doc);
}); });
@ -116,13 +117,14 @@ function generateResourceDoc(opts) {
return { return {
swaggerVersion: opts.swaggerVersion, swaggerVersion: opts.swaggerVersion,
apiVersion: opts.version, apiVersion: opts.version,
apis: [],
// See https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#513-info-object // See https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#513-info-object
info: opts.apiInfo info: opts.apiInfo,
// TODO Authorizations // TODO Authorizations
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#514-authorizations-object // https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#514-authorizations-object
// TODO Produces/Consumes consumes: ['application/json', 'application/xml', 'text/xml'],
// https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#52-api-declaration produces: ['application/json', 'application/javascript', 'application/xml', 'text/javascript', 'text/xml'],
apis: [],
models: opts.models
}; };
} }
@ -135,7 +137,7 @@ function getVersion() {
try { try {
version = require(path.join(process.cwd(), 'package.json')).version; version = require(path.join(process.cwd(), 'package.json')).version;
} catch(e) { } catch(e) {
version = ''; version = '1';
} }
return version; return version;
} }

View File

@ -21,7 +21,7 @@
"url": "https://github.com/strongloop/loopback-explorer/issues" "url": "https://github.com/strongloop/loopback-explorer/issues"
}, },
"devDependencies": { "devDependencies": {
"loopback": "1.x", "loopback": "git+https://github.com/shelbys/loopback.git#loopback-v2",
"mocha": "~1.20.1", "mocha": "~1.20.1",
"supertest": "~0.13.0", "supertest": "~0.13.0",
"chai": "~1.9.1" "chai": "~1.9.1"
@ -31,7 +31,7 @@
"url": "https://github.com/strongloop/loopback-explorer/blob/master/LICENSE" "url": "https://github.com/strongloop/loopback-explorer/blob/master/LICENSE"
}, },
"dependencies": { "dependencies": {
"swagger-ui": "~2.0.18", "swagger-ui": "git+https://github.com/shelbys/swagger-ui.git",
"debug": "~1.0.3", "debug": "~1.0.3",
"lodash.clonedeep": "^2.4.1", "lodash.clonedeep": "^2.4.1",
"lodash.defaults": "^2.4.1", "lodash.defaults": "^2.4.1",