Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit fb81a06

Browse files
committed
Fixes #157
Parse and add optional path parameters to APIGW Swagger
1 parent 31b0013 commit fb81a06

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

compile/apigw/index.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,42 @@ class OpenWhiskCompileHttpEvents {
256256
action: httpEvent.action, namespace: httpEvent.namespace,
257257
package: httpEvent.pkge, url: webaction_url
258258
}
259-
259+
260260
const swaggerPath = { operationId, responses, "x-openwhisk": x_ow }
261+
const pathParameters = this.parsePathParameters(httpEvent.relpath)
262+
263+
if (pathParameters.length) {
264+
swaggerPath.parameters = pathParameters.map(this.createPathParameter)
265+
}
266+
261267
return swaggerPath
262268
}
263269

270+
parsePathParameters (path) {
271+
const regex = /{([^}]+)\}/g
272+
const findAllParams = p => {
273+
const ids = []
274+
let id = regex.exec(p)
275+
while (id) {
276+
ids.push(id[1])
277+
id = regex.exec(p)
278+
}
279+
return ids
280+
}
281+
282+
return path.split('/')
283+
.map(findAllParams)
284+
.reduce((sum, el) => sum.concat(el), [])
285+
}
286+
287+
createPathParameter (name) {
288+
return {
289+
name: name, in: 'path',
290+
description: `Default description for '${name}'`,
291+
required: true, type: 'string'
292+
}
293+
}
294+
264295
compileSwaggerCaseSwitch(httpEvent, host) {
265296
const webaction_url = this.webActionUrl(httpEvent, host)
266297
const operationId = this.operationId(httpEvent)

compile/apigw/tests/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,40 @@ describe('OpenWhiskCompileHttpEvents', () => {
253253

254254
return expect(result).to.deep.equal(expectedResult)
255255
});
256+
257+
it('should define swagger definition with path parameters', () => {
258+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
259+
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
260+
261+
const httpEvent = {
262+
relpath: '/api/foo/{id}', operation: 'GET', secure_key: 'auth-token',
263+
action: 'action-name', namespace: '[email protected]_space', pkge: 'default', responsetype: 'json'
264+
}
265+
266+
const host = 'openwhisk.somewhere.com'
267+
const result = openwhiskCompileHttpEvents.compileSwaggerPath(httpEvent, host);
268+
269+
const expectedResult = {
270+
operationId: "get-/api/foo/{id}",
271+
parameters: [{
272+
name: "id", in: "path",
273+
description: "Default description for 'id'",
274+
required: true, type: "string"
275+
}],
276+
responses: {
277+
"200": { description: "A successful invocation response" }
278+
},
279+
"x-openwhisk": {
280+
action: "action-name",
281+
namespace: "[email protected]_space",
282+
package: "default",
283+
url: "https://openwhisk.somewhere.com/api/v1/web/[email protected]_space/default/action-name.json"
284+
}
285+
}
286+
287+
return expect(result).to.deep.equal(expectedResult)
288+
});
289+
256290
});
257291

258292
describe('#compileSwaggerCaseSwitch()', () => {

0 commit comments

Comments
 (0)