Skip to content

Commit cd52a32

Browse files
authored
Merge pull request #6 from kibertoad/master
refs #5 Fix crash on endpoints without any parameters.
2 parents f355c00 + 99e7ac2 commit cd52a32

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/middleware.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ function init(swaggerPath, options) {
2828
.forEach(function (currentMethod) {
2929
schemas[parsedPath][currentMethod.toLowerCase()] = {};
3030

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

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

5051
/**
5152
* The middleware - should be called for each express route
52-
* @param {any} req
53-
* @param {any} res
54-
* @param {any} next
53+
* @param {any} req
54+
* @param {any} res
55+
* @param {any} next
5556
* @returns In case of an error will call `next` with `InputValidationError`
5657
*/
5758
function validate(req, res, next) {

test/pet-store-swagger.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ paths:
145145
description: unexpected error
146146
schema:
147147
$ref: '#/definitions/Error'
148+
/heartbeat:
149+
get:
150+
summary: Info for current system status
151+
operationId: getHearbeat
152+
responses:
153+
"200":
154+
description: Expected response to a valid request
155+
schema:
156+
$ref: '#/definitions/StatusReport'
157+
default:
158+
description: unexpected error
159+
schema:
160+
$ref: '#/definitions/Error'
148161
definitions:
149162
Pet:
150163
required:
@@ -172,6 +185,15 @@ definitions:
172185
# format: int32
173186
message:
174187
type: string
188+
StatusReport:
189+
required:
190+
- text
191+
- code
192+
properties:
193+
text:
194+
type: string
195+
code:
196+
type: string
175197
parameters:
176198
ApiVersion:
177199
name: 'api-version'

0 commit comments

Comments
 (0)