Compute and attach a path matcing RegExp to operations

This commit is contained in:
Jeremy Whitlock 2015-07-24 13:31:53 -06:00
Родитель 61edad938e
Коммит 4414f29a75
10 изменённых файлов: 954 добавлений и 74 удалений

40
browser/swagger-core-api-min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

40
browser/swagger-core-api-standalone-min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -24,13 +24,13 @@
**Kind**: global class
* [Operation](#Operation)
* [new Operation(api, path, method, ptr, definition)](#new_Operation_new)
* [new Operation(api, path, method, ptr, definition, regexp)](#new_Operation_new)
* [.getParameters()](#Operation+getParameters) ⇒ <code>[Array.&lt;Parameter&gt;](#Parameter)</code>
* [.getResponseSchema([code])](#Operation+getResponseSchema) ⇒ <code>object</code>
* [.getResponseSample([code])](#Operation+getResponseSample) ⇒ <code>\*</code>
<a name="new_Operation_new"></a>
### new Operation(api, path, method, ptr, definition)
### new Operation(api, path, method, ptr, definition, regexp)
The Swagger Operation object.
<strong>Note:</strong> Do not use directly.
@ -43,6 +43,7 @@ The Swagger Operation object.
| method | <code>string</code> | The operation method |
| ptr | <code>string</code> | The JSON Pointer to the operation |
| definition | <code>object</code> | The operation definition |
| regexp | <code>regexp</code> | The regexp used to match request paths against this operation |
<a name="Operation+getParameters"></a>
### operation.getParameters() ⇒ <code>[Array.&lt;Parameter&gt;](#Parameter)</code>

Просмотреть файл

@ -82,6 +82,7 @@ Swagger project itself has JavaScript libraries that support IE8+ and we wanted
they did just in case they wanted to use these libraries.)_
* [native-promise-only][native-promise-only]: Used to shim in [Promises][promises] support
* [path-loader][path-loader]: Used to load Swagger files from the local filesystem and remote URLs
* [path-to-regexp][path-to-regexp]: Used to create `RegExp` objects from Swagger paths
* [z-schema][z-schema]: Used for JSON Schema validation
[bower]: http://bower.io/
@ -93,9 +94,10 @@ they did just in case they wanted to use these libraries.)_
[json-schema-faker]: https://www.npmjs.com/package/json-schema-faker
[lodash-compat]: https://www.npmjs.com/package/lodash-compat
[native-promise-only]: https://www.npmjs.com/package/native-promise-only
[path-loader]: https://www.npmjs.com/package/path-loader
[promises]: https://www.promisejs.org/
[npm]: https://www.npmjs.org/
[path-loader]: https://www.npmjs.com/package/path-loader
[path-to-regexp]: https://github.com/pillarjs/path-to-regexp
[promises]: https://www.promisejs.org/
[version-2.0-documentation]: https://github.com/apigee-127/swagger-core-api/blob/master/docs/versions/2.0.md
[swagger]: http://swagger.io
[z-schema]: https://www.npmjs.com/package/z-schema

Просмотреть файл

@ -37,15 +37,17 @@ var debug = require('debug')('swagger-core-api');
* @param {string} method - The operation method
* @param {string} ptr - The JSON Pointer to the operation
* @param {object} definition - The operation definition
* @param {regexp} regexp - The regexp used to match request paths against this operation
*
* @constructor
*/
function Operation (api, path, method, ptr, definition) {
function Operation (api, path, method, ptr, definition, regexp) {
this.api = api;
this.path = path;
this.method = method;
this.ptr = ptr;
this.definition = definition;
this.regexp = regexp;
// Assign Swagger definition properties to the operation for easy access
_.assign(this, definition);

Просмотреть файл

@ -28,6 +28,7 @@ var _ = require('lodash-compat');
var JsonRefs = require('json-refs');
var formatGenerators = require('./format-generators');
var helpers = require('../../helpers');
var pathToRegexp = require('path-to-regexp');
var types = require('../../types');
var validators = require('./validators');
var vHelpers = require('./helpers');
@ -101,6 +102,12 @@ module.exports.getJSONSchemaValidator = function () {
*/
module.exports.getOperations = function (api) {
var operations = [];
var basePathPrefix = api.resolved.basePath || '/';
// Remove trailing slash from the basePathPrefix so we do not end up with double slashes
if (basePathPrefix.charAt(basePathPrefix.length - 1) === '/') {
basePathPrefix = basePathPrefix.substring(0, basePathPrefix.length - 1);
}
_.forEach(api.resolved.paths, function (pathDef, path) {
var pPath = ['paths', path];
@ -112,6 +119,7 @@ module.exports.getOperations = function (api) {
return parameters;
}, {});
var pRegexp = pathToRegexp(basePathPrefix + path.replace(/\{/g, ':').replace(/\}/g, ''));
_.forEach(pathDef, function (operation, method) {
// Do not process non-operations
@ -149,7 +157,7 @@ module.exports.getOperations = function (api) {
cOperation.security = api.resolved.security;
}
operations.push(new types.Operation(api, path, method, JsonRefs.pathToPointer(oPath), cOperation));
operations.push(new types.Operation(api, path, method, JsonRefs.pathToPointer(oPath), cOperation, pRegexp));
});
});

Просмотреть файл

@ -61,6 +61,7 @@
"lodash-compat": "^3.10.0",
"native-promise-only": "^0.8.0-a",
"path-loader": "0.1.0",
"path-to-regexp": "^1.2.0",
"z-schema": "^3.12.0"
}
}

Просмотреть файл

@ -289,6 +289,54 @@ describe('swagger-core-api (Swagger 2.0)', function () {
]);
});
function validateRegExps (api, basePath) {
var createPet = api.getOperation('/pet', 'post');
var updatePet = api.getOperation('/pet/{petId}', 'post');
// Make sure they are of the proper type
assert.ok(createPet.regexp instanceof RegExp);
assert.ok(updatePet.regexp instanceof RegExp);
// Make sure they have the proper keys
assert.equal(0, createPet.regexp.keys.length);
assert.equal(1, updatePet.regexp.keys.length);
assert.equal('petId', updatePet.regexp.keys[0].name);
// Make sure they match the expected URLs
assert.ok(_.isArray(createPet.regexp.exec(basePath + '/pet')));
assert.ok(!_.isArray(createPet.regexp.exec(basePath + '/pets')));
assert.ok(_.isArray(updatePet.regexp.exec(basePath + '/pet/1')));
assert.ok(!_.isArray(createPet.regexp.exec(basePath + '/pets/1')));
}
it('should create proper regexp (with basePath)', function () {
validateRegExps(swagger, swagger.basePath);
});
it('should create proper regexp (with basePath ending in slash)', function (done) {
var cSwagger = _.cloneDeep(swaggerDoc);
cSwagger.basePath = '/';
swaggerApi.create({definition: cSwagger})
.then(function (api) {
validateRegExps(api, '');
})
.then(done, done);
});
it('should create proper regexp (without basePath)', function (done) {
var cSwagger = _.cloneDeep(swaggerDoc);
delete cSwagger.basePath;
swaggerApi.create({definition: cSwagger})
.then(function (api) {
validateRegExps(api, '');
})
.then(done, done);
});
// More vigorous testing of the Parameter object itself and the parameter composition are done elsewhere
describe('#getParameters', function () {
it('should return the proper parameter objects', function () {