Skip to content

Commit fde5289

Browse files
committed
fix(package): fix a bug in $input.json
- Don't parse twice. - Don't parse as JSON if payload starts with { or [ or "
1 parent a1a8bf6 commit fde5289

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ module.exports = function(parameters) {
3535
return jsonpath(obj, path);
3636
},
3737
json: function(path) {
38-
var obj = JSON.parse(payload);
39-
if (typeof obj === 'string') {
40-
// re-parse when parsed payload is string.
41-
// because of
42-
// - https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a669d28c
43-
// - https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-1b8d22cd
44-
obj = JSON.parse(obj);
38+
var obj;
39+
// if payload starts with `{` or `[` or `"`, treat as JSON
40+
if (/^\s*(?:{|\[|")/.test(this._payload)) {
41+
obj = JSON.parse(this._payload);
42+
} else {
43+
// treat as string
44+
obj = this._payload;
4545
}
4646

4747
return JSON.stringify(jsonpath(obj, path));

misc/examples.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ module.exports = [
1010
headers: {},
1111
},
1212
{
13-
template: '"$input.json(\'$\')"',
13+
template: '$input.json(\'$\')',
1414
payload: "a=b",
1515
headers: {},
1616
},
1717
{
18-
template: '"$input.json(\'$\')"',
18+
template: '$input.json(\'$\')',
1919
payload: '"a=b"',
2020
headers: {},
2121
},
@@ -30,7 +30,7 @@ module.exports = [
3030
headers: {},
3131
},
3232
{
33-
template: '"$input.json(\'$\')"',
33+
template: '$input.json(\'$\')',
3434
payload: "{}",
3535
headers: {},
3636
},

test/_.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@ describe('$input.path|$input.json', function() {
2020
assert.deepEqual(expected, actual);
2121
});
2222
});
23-
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a669d28c
24-
describe('H=`{}` P=`a=b` ===> T=`"$input.json(\'$\')"`', function() {
25-
it('throw error', function() {
26-
assert.throws(function() { mappingTemplate({template: "\"$input.json('$')\"", payload: "a=b"}); });
23+
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-0dce6fa3
24+
describe('H=`{}` P=`a=b` ===> T=`$input.json(\'$\')`', function() {
25+
it('return a=b', function() {
26+
var expected = "a=b";
27+
var actual = JSON.parse(mappingTemplate({template: "$input.json('$')", payload: "a=b"}));
28+
assert.deepEqual(expected, actual);
2729
});
2830
});
29-
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-0ce08526
30-
describe('H=`{}` P=`"a=b"` ===> T=`"$input.json(\'$\')"`', function() {
31-
it('throw error', function() {
32-
assert.throws(function() { mappingTemplate({template: "\"$input.json('$')\"", payload: "\"a=b\""}); });
31+
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-dbd6cf1c
32+
describe('H=`{}` P=`"a=b"` ===> T=`$input.json(\'$\')`', function() {
33+
it('return "a=b"', function() {
34+
var expected = "a=b";
35+
var actual = JSON.parse(mappingTemplate({template: "$input.json('$')", payload: "\"a=b\""}));
36+
assert.deepEqual(expected, actual);
3337
});
3438
});
3539
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-03be1e25
@@ -48,11 +52,11 @@ describe('$input.path|$input.json', function() {
4852
assert.deepEqual(expected, actual);
4953
});
5054
});
51-
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-c1b4a9a5
52-
describe('H=`{}` P=`{}` ===> T=`"$input.json(\'$\')"`', function() {
55+
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-b9f18c27
56+
describe('H=`{}` P=`{}` ===> T=`$input.json(\'$\')`', function() {
5357
it('return {}', function() {
54-
var expected = "{}";
55-
var actual = JSON.parse(mappingTemplate({template: "\"$input.json('$')\"", payload: "{}"}));
58+
var expected = {};
59+
var actual = JSON.parse(mappingTemplate({template: "$input.json('$')", payload: "{}"}));
5660
assert.deepEqual(expected, actual);
5761
});
5862
});

test/_.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ Template|Header|Payload|Status code|Result
88
--------|------|-------|-----------|------
99
`"$input.path('$')"`|`None`|`"a=b"`|`200`|`"a=b"`
1010

11-
## example-a669d28c
11+
## example-0dce6fa3
1212
Template|Header|Payload|Status code|Result
1313
--------|------|-------|-----------|------
14-
`"$input.json('$')"`|`None`|`a=b`|`200`|`{errorMessage=Unable to parse input as json: Unexpected token a, errorType=SyntaxError}`
14+
`$input.json('$')`|`None`|`a=b`|`200`|`"a=b"`
1515

16-
## example-0ce08526
16+
## example-dbd6cf1c
1717
Template|Header|Payload|Status code|Result
1818
--------|------|-------|-----------|------
19-
`"$input.json('$')"`|`None`|`"a=b"`|`200`|`{errorMessage=Unable to parse input as json: Unexpected token a, errorType=SyntaxError}`
19+
`$input.json('$')`|`None`|`"a=b"`|`200`|`"a=b"`
2020

2121
## example-03be1e25
2222
Template|Header|Payload|Status code|Result
@@ -28,10 +28,10 @@ Template|Header|Payload|Status code|Result
2828
--------|------|-------|-----------|------
2929
`"$input.path('$')"`|`None`|`"{}"`|`200`|`"{}"`
3030

31-
## example-c1b4a9a5
31+
## example-b9f18c27
3232
Template|Header|Payload|Status code|Result
3333
--------|------|-------|-----------|------
34-
`"$input.json('$')"`|`None`|`{}`|`200`|`"{}"`
34+
`$input.json('$')`|`None`|`{}`|`200`|`{}`
3535

3636
## example-25c6993c
3737
Template|Header|Payload|Status code|Result

0 commit comments

Comments
 (0)