Update SwaggerApi#getOperation to work with request objects

This commit is contained in:
Jeremy Whitlock 2015-07-24 14:12:36 -06:00
Родитель 4414f29a75
Коммит f29642d388
7 изменённых файлов: 10301 добавлений и 7336 удалений

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

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

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

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

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

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

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

@ -126,7 +126,7 @@ Returns a sample value for the parameter based on its schema;
* [new SwaggerApi(plugin, definition, resolved, references, options)](#new_SwaggerApi_new)
* [.getLastErrors()](#SwaggerApi+getLastErrors) ⇒ <code>Array.&lt;object&gt;</code>
* [.getLastWarnings()](#SwaggerApi+getLastWarnings) ⇒ <code>Array.&lt;object&gt;</code>
* [.getOperation(path, method)](#SwaggerApi+getOperation) ⇒ <code>[Operation](#Operation)</code>
* [.getOperation(pathOrReq, [method])](#SwaggerApi+getOperation) ⇒ <code>[Operation](#Operation)</code>
* [.getOperations([path])](#SwaggerApi+getOperations) ⇒ <code>[Array.&lt;Operation&gt;](#Operation)</code>
* [.registerValidator(validator)](#SwaggerApi+registerValidator)
* [.validate()](#SwaggerApi+validate) ⇒ <code>boolean</code>
@ -160,8 +160,11 @@ Returns the warnings from the last validate call.
**Kind**: instance method of <code>[SwaggerApi](#SwaggerApi)</code>
**Returns**: <code>Array.&lt;object&gt;</code> - The warnings from the previous call to validate or undefined if validate was never called
<a name="SwaggerApi+getOperation"></a>
### swaggerApi.getOperation(path, method) ⇒ <code>[Operation](#Operation)</code>
Returns the operation for the provided path and method.
### swaggerApi.getOperation(pathOrReq, [method]) ⇒ <code>[Operation](#Operation)</code>
Returns the operation for the given path and operation.
**Note:** If you pass in an `http.clientRequest` *(or equivalent)*, the `method` and `url` properties are use to
perform the matching. *(See: [https://nodejs.org/api/http.html#http_class_http_clientrequest](https://nodejs.org/api/http.html#http_class_http_clientrequest))*
**Kind**: instance method of <code>[SwaggerApi](#SwaggerApi)</code>
**Returns**: <code>[Operation](#Operation)</code> - The operation for the provided path and method or undefined if there is no operation for that
@ -169,8 +172,8 @@ Returns the operation for the provided path and method.
| Param | Type | Description |
| --- | --- | --- |
| path | <code>string</code> | The Swagger path |
| method | <code>string</code> | The Swagger operation method |
| pathOrReq | <code>string</code> &#124; <code>object</code> | The Swagger path string or the http client request |
| [method] | <code>string</code> | The Swagger operation method |
<a name="SwaggerApi+getOperations"></a>
### swaggerApi.getOperations([path]) ⇒ <code>[Array.&lt;Operation&gt;](#Operation)</code>

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

@ -26,6 +26,7 @@
var _ = require('lodash-compat');
var debug = require('debug')('swagger-core-api');
var parseUrl = require('url').parse;
/**
* The Swagger Operation object.
@ -242,18 +243,36 @@ SwaggerApi.prototype.getLastWarnings = function () {
};
/**
* Returns the operation for the provided path and method.
* Returns the operation for the given path and operation.
*
* @param {string} path - The Swagger path
* @param {string} method - The Swagger operation method
* **Note:** If you pass in an `http.clientRequest` *(or equivalent)*, the `method` and `url` properties are use to
* perform the matching. *(See: {@link https://nodejs.org/api/http.html#http_class_http_clientrequest})*
*
* @param {string|object} pathOrReq - The Swagger path string or the http client request
* @param {string} [method] - The Swagger operation method
*
* @returns {Operation} The operation for the provided path and method or undefined if there is no operation for that
* path and method combination.
*/
SwaggerApi.prototype.getOperation = function (path, method) {
return _.find(this.operationObjects, function (operation) {
return operation.path === path && operation.method === method.toLowerCase();
});
SwaggerApi.prototype.getOperation = function (pathOrReq, method) {
var predicate;
var url;
if (_.isObject(pathOrReq)) {
method = pathOrReq.method;
url = parseUrl(pathOrReq.url).pathname;
predicate = function (operation) {
return operation.method === method && _.isArray(operation.regexp.exec(url));
};
} else {
predicate = function (operation) {
return operation.path === pathOrReq && operation.method === method;
};
}
method = method.toLowerCase();
return _.find(this.operationObjects, predicate);
};
/**

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

@ -566,18 +566,43 @@ describe('swagger-core-api (Swagger 2.0)', function () {
});
describe('#getOperation', function () {
it('should return the expected operation', function () {
var operation = swagger.getOperation('/pet/{petId}', 'get');
describe('path + method', function () {
it('should return the expected operation', function () {
var operation = swagger.getOperation('/pet/{petId}', 'get');
assert.ok(!_.isUndefined(operation));
assert.ok(!_.isUndefined(operation));
});
it('should return no operation for missing path', function () {
assert.ok(_.isUndefined(swagger.getOperation('/petz/{petId}', 'get')));
});
it('should return no operation for missing method', function () {
assert.ok(_.isUndefined(swagger.getOperation('/pet/{petId}', 'head')));
});
});
it('should return no operation for missing path', function () {
assert.ok(_.isUndefined(swagger.getOperation('/petz/{petId}', 'get')));
});
describe('http.ClientRequest (or similar)', function () {
it('should return the expected operation', function () {
assert.ok(!_.isUndefined(swagger.getOperation({
method: 'GET',
url: swagger.basePath + '/pet/1'
})));
});
it('should return no operation for missing method', function () {
assert.ok(_.isUndefined(swagger.getOperation('/pet/{petId}', 'head')));
it('should return no operation for missing path', function () {
assert.ok(_.isUndefined(swagger.getOperation({
method: 'GET',
url: swagger.basePath + '/petz/1'
})));
});
it('should return no operation for missing method', function () {
assert.ok(_.isUndefined(swagger.getOperation({
method: 'HEAD',
url: swagger.basePath + '/pet/1'
})));
});
});
});