зеркало из https://github.com/Azure/sway.git
Update SwaggerApi#getOperation to work with request objects
This commit is contained in:
Родитель
4414f29a75
Коммит
f29642d388
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
13
docs/API.md
13
docs/API.md
|
@ -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.<object></code>
|
||||
* [.getLastWarnings()](#SwaggerApi+getLastWarnings) ⇒ <code>Array.<object></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.<Operation>](#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.<object></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> | <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.<Operation>](#Operation)</code>
|
||||
|
|
33
lib/types.js
33
lib/types.js
|
@ -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'
|
||||
})));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче