Skip to content

Commit f00656a

Browse files
authored
Merge pull request #1079 from extempl/master
Add `operation not found` exception
2 parents 7da5755 + 77ce275 commit f00656a

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/execute.js

100644100755
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import btoa from 'btoa'
66
import url from 'url'
77
import http, {mergeInQueryOrForm} from './http'
88
import {getOperationRaw, idFromPathMethod, legacyIdFromPathMethod} from './helpers'
9+
import createError from './specmap/lib/create-error'
910

1011
const arrayOrEmpty = (ar) => {
1112
return Array.isArray(ar) ? ar : []
1213
}
1314

15+
const OperationNotFoundError = createError('OperationNotFoundError', function(message, extra, oriError) {
16+
this.originalError = oriError
17+
Object.assign(this, extra || {})
18+
})
19+
1420
// For stubbing in tests
1521
export const self = {
1622
buildRequest
@@ -87,20 +93,26 @@ export function buildRequest({
8793
return req
8894
}
8995

90-
const {operation = {}, method, pathName} = getOperationRaw(spec, operationId)
96+
const operationRaw = getOperationRaw(spec, operationId)
97+
if (!operationRaw) {
98+
throw new OperationNotFoundError(`Operation ${operationId} not found`)
99+
}
100+
101+
const {operation = {}, method, pathName} = operationRaw
91102

92103
req.url += pathName // Have not yet replaced the path parameters
93104
req.method = (`${method}`).toUpperCase()
94105

95106
parameters = parameters || {}
107+
const path = spec.paths[pathName] || {}
96108

97109
if (responseContentType) {
98110
req.headers.accept = responseContentType
99111
}
100112

101113
// Add values to request
102114
arrayOrEmpty(operation.parameters) // operation parameters
103-
.concat(arrayOrEmpty(spec.paths[pathName].parameters)) // path parameters
115+
.concat(arrayOrEmpty(path.parameters)) // path parameters
104116
.forEach((parameter) => {
105117
const builder = parameterBuilders[parameter.in]
106118
let value
@@ -219,7 +231,7 @@ export function baseUrl({spec, scheme, contextUrl = ''}) {
219231
// Add security values, to operations - that declare their need on them
220232
export function applySecurities({request, securities = {}, operation = {}, spec}) {
221233
const result = assign({}, request)
222-
const {authorized = {}, specSecurity = {}} = securities
234+
const {authorized = {}, specSecurity = []} = securities
223235
const security = operation.security || specSecurity
224236
const isAuthorized = authorized && !!Object.keys(authorized).length
225237
const securityDef = spec.securityDefinitions

src/helpers.js

100644100755
File mode changed.

test/execute.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ describe('execute', () => {
271271
expect(() => buildRequest({spec, operationId: 'getMe'})).toThrow('Required parameter petId is not provided')
272272
})
273273

274+
it('should throw error if operation was not found', function () {
275+
// Given
276+
const spec = {
277+
host: 'swagger.io',
278+
basePath: '/v1',
279+
paths: {
280+
'/one': {
281+
get: {
282+
operationId: 'getMe'
283+
}
284+
}
285+
}
286+
}
287+
288+
expect(() => buildRequest({spec, operationId: 'nonExistingOperationId'})).toThrow('Operation nonExistingOperationId not found')
289+
})
290+
274291
describe('formData', function () {
275292
it('should add an empty query param if the value is empty and allowEmptyValue: true', function () {
276293
// Given

0 commit comments

Comments
 (0)