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

Commit 31b8d8a

Browse files
committed
Path parameters fail to deploy due to incorrect Swagger document.
1 parent 2ff8a95 commit 31b8d8a

File tree

6 files changed

+198
-7
lines changed

6 files changed

+198
-7
lines changed

compile/apigw/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ class OpenWhiskCompileHttpEvents {
249249

250250
compileSwaggerPath(httpEvent, host) {
251251
const operationId = this.operationId(httpEvent)
252+
const pathParameters = this.parsePathParameters(httpEvent.relpath)
252253
const responses = { "200": { description: "A successful invocation response" } }
253254
const webaction_url = this.webActionUrl(httpEvent, host)
254255

@@ -258,7 +259,6 @@ class OpenWhiskCompileHttpEvents {
258259
}
259260

260261
const swaggerPath = { operationId, responses, "x-openwhisk": x_ow }
261-
const pathParameters = this.parsePathParameters(httpEvent.relpath)
262262

263263
if (pathParameters.length) {
264264
swaggerPath.parameters = pathParameters.map(this.createPathParameter)
@@ -293,7 +293,8 @@ class OpenWhiskCompileHttpEvents {
293293
}
294294

295295
compileSwaggerCaseSwitch(httpEvent, host) {
296-
const webaction_url = this.webActionUrl(httpEvent, host)
296+
const pathParameters = this.parsePathParameters(httpEvent.relpath)
297+
const webaction_url = this.webActionUrl(httpEvent, host, !!pathParameters.length)
297298
const operationId = this.operationId(httpEvent)
298299

299300
const header = {
@@ -311,8 +312,10 @@ class OpenWhiskCompileHttpEvents {
311312
return swaggerCaseSwitch
312313
}
313314

314-
webActionUrl(httpEvent, host) {
315-
return `https://${host}/api/v1/web/${httpEvent.namespace}/${httpEvent.pkge}/${httpEvent.action}.${httpEvent.responsetype}`
315+
webActionUrl(httpEvent, host, has_path_params) {
316+
const url = `https://${host}/api/v1/web/${httpEvent.namespace}/${httpEvent.pkge}/${httpEvent.action}.${httpEvent.responsetype}${has_path_params ? '$(request.path)': ''}`
317+
318+
return url
316319
}
317320

318321
operationId(httpEvent) {

compile/apigw/tests/index.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
260260

261261
const httpEvent = {
262262
relpath: '/api/foo/{id}', operation: 'GET', secure_key: 'auth-token',
263-
action: 'action-name', namespace: '[email protected]_space', pkge: 'default', responsetype: 'json'
263+
action: 'action-name', namespace: '[email protected]_space', pkge: 'default', responsetype: 'http'
264264
}
265265

266266
const host = 'openwhisk.somewhere.com'
@@ -280,7 +280,7 @@ describe('OpenWhiskCompileHttpEvents', () => {
280280
action: "action-name",
281281
namespace: "[email protected]_space",
282282
package: "default",
283-
url: "https://openwhisk.somewhere.com/api/v1/web/[email protected]_space/default/action-name.json"
283+
url: "https://openwhisk.somewhere.com/api/v1/web/[email protected]_space/default/action-name.http"
284284
}
285285
}
286286

@@ -323,6 +323,40 @@ describe('OpenWhiskCompileHttpEvents', () => {
323323

324324
return expect(result).to.deep.equal(expectedResult)
325325
});
326+
327+
it('should define swagger case statement from http events with path parameters', () => {
328+
openwhiskCompileHttpEvents.serverless.service.service = 'my-service'
329+
openwhiskCompileHttpEvents.serverless.service.provider = {namespace: "sample_ns"};
330+
331+
const httpEvent = {
332+
relpath: '/api/foo/{id}', operation: 'GET', secure_key: 'auth-token',
333+
action: 'action-name', namespace: '[email protected]_space', pkge: 'default', responsetype: 'http'
334+
}
335+
336+
const host = 'openwhisk.somewhere.com'
337+
const result = openwhiskCompileHttpEvents.compileSwaggerCaseSwitch(httpEvent, host);
338+
339+
const expectedResult = {
340+
execute: [{
341+
"set-variable": {
342+
actions: [{
343+
set: "message.headers.X-Require-Whisk-Auth",
344+
value: "auth-token"
345+
}]
346+
}
347+
},
348+
{
349+
invoke: {
350+
"target-url": "https://openwhisk.somewhere.com/api/v1/web/[email protected]_space/default/action-name.http$(request.path)",
351+
"verb": "keep"
352+
}
353+
}
354+
],
355+
operations: [ "get-/api/foo/{id}" ]
356+
}
357+
358+
return expect(result).to.deep.equal(expectedResult)
359+
});
326360
});
327361

328362
describe('#generateSwagger()', () => {

deploy/lib/deployApiGw.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module.exports = {
4040
const id = operation.operationId
4141
const stmts = swagger["x-ibm-configuration"].assembly.execute[0]['operation-switch'].case
4242
const stmt = stmts.find(stmt => stmt.operations[0] === id)
43-
stmt.execute[stmt.execute.length -1].invoke['target-url'] = swaggerAction.url
43+
const invoke = stmt.execute[stmt.execute.length -1].invoke
44+
invoke['target-url'] = invoke['target-url'].replace(/web\/_/, `web/${action.namespace}`)
4445
}
4546
}
4647
}

deploy/tests/deployApiGw.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,27 @@ describe('deployHttpEvents', () => {
7171
let result = await openwhiskDeploy.replaceDefaultNamespace(source)
7272
expect(result).to.be.deep.equal(converted)
7373
})
74+
75+
it('should return same swagger doc including path params', async () => {
76+
const without_default_ns = fs.readFileSync('./deploy/tests/resources/swagger_ns_paths.json', 'utf-8')
77+
const with_default_ns = fs.readFileSync('./deploy/tests/resources/swagger_paths.json', 'utf-8')
78+
const source = JSON.parse(with_default_ns)
79+
const converted = JSON.parse(without_default_ns)
80+
81+
const actions = [{"name":"hello","namespace":"[email protected]_dev"}]
82+
83+
sandbox.stub(openwhiskDeploy.provider, 'client', () => {
84+
const list = params => {
85+
return Promise.resolve(actions);
86+
};
87+
88+
return Promise.resolve({ actions: { list } });
89+
});
90+
91+
let result = await openwhiskDeploy.replaceDefaultNamespace(source)
92+
expect(result).to.be.deep.equal(converted)
93+
94+
})
7495
})
7596

7697
/**
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"swagger": "2.0",
3+
"basePath": "/testing",
4+
"info": {
5+
"title": "testing",
6+
"version": "1.0"
7+
},
8+
"paths": {
9+
"/testing": {
10+
"get": {
11+
"operationId": "getTesting",
12+
"responses": {
13+
"200": {
14+
"description": "A successful invocation response"
15+
}
16+
},
17+
"x-openwhisk": {
18+
"action": "hello",
19+
"namespace": "[email protected]_dev",
20+
"package": "default",
21+
"url": "https://openwhisk/api/v1/web/[email protected]_dev/default/hello.http"
22+
}
23+
}
24+
}
25+
},
26+
"x-ibm-configuration": {
27+
"assembly": {
28+
"execute": [
29+
{
30+
"operation-switch": {
31+
"case": [
32+
{
33+
"execute": [
34+
{
35+
"set-variable": {
36+
"actions": [
37+
{
38+
"set": "message.headers.X-Require-Whisk-Auth",
39+
"value": "3b08f67e-c7fa-4998-9096-ffa355932a3d"
40+
}
41+
]
42+
}
43+
},
44+
{
45+
"invoke": {
46+
"target-url": "https://openwhisk/api/v1/web/[email protected]_dev/default/hello.http$(request.path)",
47+
"verb": "keep"
48+
}
49+
}
50+
],
51+
"operations": [
52+
"getTesting"
53+
]
54+
}
55+
],
56+
"otherwise": [],
57+
"title": "whisk-invoke"
58+
}
59+
}
60+
]
61+
},
62+
"cors": {
63+
"enabled": true
64+
}
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"swagger": "2.0",
3+
"basePath": "/testing",
4+
"info": {
5+
"title": "testing",
6+
"version": "1.0"
7+
},
8+
"paths": {
9+
"/testing": {
10+
"get": {
11+
"operationId": "getTesting",
12+
"responses": {
13+
"200": {
14+
"description": "A successful invocation response"
15+
}
16+
},
17+
"x-openwhisk": {
18+
"action": "hello",
19+
"namespace": "_",
20+
"package": "default",
21+
"url": "https://openwhisk/api/v1/web/_/default/hello.http"
22+
}
23+
}
24+
}
25+
},
26+
"x-ibm-configuration": {
27+
"assembly": {
28+
"execute": [
29+
{
30+
"operation-switch": {
31+
"case": [
32+
{
33+
"execute": [
34+
{
35+
"set-variable": {
36+
"actions": [
37+
{
38+
"set": "message.headers.X-Require-Whisk-Auth",
39+
"value": "3b08f67e-c7fa-4998-9096-ffa355932a3d"
40+
}
41+
]
42+
}
43+
},
44+
{
45+
"invoke": {
46+
"target-url": "https://openwhisk/api/v1/web/_/default/hello.http$(request.path)",
47+
"verb": "keep"
48+
}
49+
}
50+
],
51+
"operations": [
52+
"getTesting"
53+
]
54+
}
55+
],
56+
"otherwise": [],
57+
"title": "whisk-invoke"
58+
}
59+
}
60+
]
61+
},
62+
"cors": {
63+
"enabled": true
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)