Skip to content

Commit

Permalink
fix(package): fix a bug in $input.json
Browse files Browse the repository at this point in the history
- Don't parse twice.
- Don't parse as JSON if payload starts with { or [ or "
  • Loading branch information
ToQoz committed Dec 18, 2015
1 parent a1a8bf6 commit fde5289
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ module.exports = function(parameters) {
return jsonpath(obj, path);
},
json: function(path) {
var obj = JSON.parse(payload);
if (typeof obj === 'string') {
// re-parse when parsed payload is string.
// because of
// - https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a669d28c
// - https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-1b8d22cd
obj = JSON.parse(obj);
var obj;
// if payload starts with `{` or `[` or `"`, treat as JSON
if (/^\s*(?:{|\[|")/.test(this._payload)) {
obj = JSON.parse(this._payload);
} else {
// treat as string
obj = this._payload;
}

return JSON.stringify(jsonpath(obj, path));
Expand Down
6 changes: 3 additions & 3 deletions misc/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module.exports = [
headers: {},
},
{
template: '"$input.json(\'$\')"',
template: '$input.json(\'$\')',
payload: "a=b",
headers: {},
},
{
template: '"$input.json(\'$\')"',
template: '$input.json(\'$\')',
payload: '"a=b"',
headers: {},
},
Expand All @@ -30,7 +30,7 @@ module.exports = [
headers: {},
},
{
template: '"$input.json(\'$\')"',
template: '$input.json(\'$\')',
payload: "{}",
headers: {},
},
Expand Down
28 changes: 16 additions & 12 deletions test/_.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ describe('$input.path|$input.json', function() {
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a669d28c
describe('H=`{}` P=`a=b` ===> T=`"$input.json(\'$\')"`', function() {
it('throw error', function() {
assert.throws(function() { mappingTemplate({template: "\"$input.json('$')\"", payload: "a=b"}); });
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-0dce6fa3
describe('H=`{}` P=`a=b` ===> T=`$input.json(\'$\')`', function() {
it('return a=b', function() {
var expected = "a=b";
var actual = JSON.parse(mappingTemplate({template: "$input.json('$')", payload: "a=b"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-0ce08526
describe('H=`{}` P=`"a=b"` ===> T=`"$input.json(\'$\')"`', function() {
it('throw error', function() {
assert.throws(function() { mappingTemplate({template: "\"$input.json('$')\"", payload: "\"a=b\""}); });
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-dbd6cf1c
describe('H=`{}` P=`"a=b"` ===> T=`$input.json(\'$\')`', function() {
it('return "a=b"', function() {
var expected = "a=b";
var actual = JSON.parse(mappingTemplate({template: "$input.json('$')", payload: "\"a=b\""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-03be1e25
Expand All @@ -48,11 +52,11 @@ describe('$input.path|$input.json', function() {
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-c1b4a9a5
describe('H=`{}` P=`{}` ===> T=`"$input.json(\'$\')"`', function() {
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-b9f18c27
describe('H=`{}` P=`{}` ===> T=`$input.json(\'$\')`', function() {
it('return {}', function() {
var expected = "{}";
var actual = JSON.parse(mappingTemplate({template: "\"$input.json('$')\"", payload: "{}"}));
var expected = {};
var actual = JSON.parse(mappingTemplate({template: "$input.json('$')", payload: "{}"}));
assert.deepEqual(expected, actual);
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/_.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Template|Header|Payload|Status code|Result
--------|------|-------|-----------|------
`"$input.path('$')"`|`None`|`"a=b"`|`200`|`"a=b"`

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

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

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

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

## example-25c6993c
Template|Header|Payload|Status code|Result
Expand Down

0 comments on commit fde5289

Please sign in to comment.