Skip to content

Commit

Permalink
Merge pull request #6 from kibertoad/master
Browse files Browse the repository at this point in the history
refs #5 Fix crash on endpoints without any parameters.
  • Loading branch information
idanto authored Dec 24, 2017
2 parents f355c00 + 99e7ac2 commit cd52a32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ function init(swaggerPath, options) {
.forEach(function (currentMethod) {
schemas[parsedPath][currentMethod.toLowerCase()] = {};

let bodySchema = dereferenced.paths[currentPath][currentMethod].parameters.filter(function (parameter) { return parameter.in === 'body' });
const parameters = dereferenced.paths[currentPath][currentMethod].parameters || [];
let bodySchema = parameters.filter(function (parameter) { return parameter.in === 'body' });
if (bodySchema.length > 0) {
schemas[parsedPath][currentMethod].body = buildBodyValidation(bodySchema[0].schema, dereferenced.definitions, swaggers[1], currentPath, currentMethod, parsedPath);
}

let localParameters = dereferenced.paths[currentPath][currentMethod].parameters.filter(function (parameter) {
let localParameters = parameters.filter(function (parameter) {
return parameter.in !== 'body';
}).concat(pathParameters);
if (localParameters.length > 0) {
Expand All @@ -49,9 +50,9 @@ function init(swaggerPath, options) {

/**
* The middleware - should be called for each express route
* @param {any} req
* @param {any} res
* @param {any} next
* @param {any} req
* @param {any} res
* @param {any} next
* @returns In case of an error will call `next` with `InputValidationError`
*/
function validate(req, res, next) {
Expand Down
22 changes: 22 additions & 0 deletions test/pet-store-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ paths:
description: unexpected error
schema:
$ref: '#/definitions/Error'
/heartbeat:
get:
summary: Info for current system status
operationId: getHearbeat
responses:
"200":
description: Expected response to a valid request
schema:
$ref: '#/definitions/StatusReport'
default:
description: unexpected error
schema:
$ref: '#/definitions/Error'
definitions:
Pet:
required:
Expand Down Expand Up @@ -172,6 +185,15 @@ definitions:
# format: int32
message:
type: string
StatusReport:
required:
- text
- code
properties:
text:
type: string
code:
type: string
parameters:
ApiVersion:
name: 'api-version'
Expand Down

0 comments on commit cd52a32

Please sign in to comment.