diff options
| author | Andrew Dunstan | 2015-10-04 17:28:16 +0000 |
|---|---|---|
| committer | Andrew Dunstan | 2015-10-04 17:28:16 +0000 |
| commit | 1edd4ec831458e10b524d1473a7de5791aa8753e (patch) | |
| tree | 1b2f229c94932b0b92421e1b258b44f4f802cddf /src/test | |
| parent | 6390c8c654d07c08686adbbc595a13d76b573653 (diff) | |
Disallow invalid path elements in jsonb_set
Null path elements and, where the object is an array, invalid integer
elements now cause an error.
Incorrect behaviour noted by Thom Brown, patch from Dmitry Dolgov.
Backpatch to 9.5 where jsonb_set was introduced
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/jsonb.out | 24 | ||||
| -rw-r--r-- | src/test/regress/expected/jsonb_1.out | 24 | ||||
| -rw-r--r-- | src/test/regress/sql/jsonb.sql | 3 |
3 files changed, 21 insertions, 30 deletions
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index 82d1b69bfaa..6da5a151d72 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -3127,11 +3127,7 @@ select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::j (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]'); - jsonb_set ---------------------------------------------------------------------- - {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} -(1 row) - +ERROR: path element at the position 2 is NULL select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}'); jsonb_set ------------------------------------------------------------------------- @@ -3151,11 +3147,7 @@ select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::j (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}'); - jsonb_set ---------------------------------------------------------------------- - {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} -(1 row) - +ERROR: path element at the position 2 is NULL select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"'); jsonb_set -------------------------------------------------------------------------- @@ -3199,11 +3191,7 @@ select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{ (1 row) select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript - ?column? ---------------------------------------------------------------------- - {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} -(1 row) - +ERROR: path element at the position 2 is not an integer select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}'; ?column? ------------------------------------------------------------------ @@ -3331,3 +3319,9 @@ select jsonb_set('[]','{-99}','{"foo":123}'); [{"foo": 123}] (1 row) +select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"'); +ERROR: path element at the position 2 is not an integer +select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"'); +ERROR: path element at the position 3 is not an integer +select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"'); +ERROR: path element at the position 3 is NULL diff --git a/src/test/regress/expected/jsonb_1.out b/src/test/regress/expected/jsonb_1.out index 2cbc0c79bba..39e7b1ff570 100644 --- a/src/test/regress/expected/jsonb_1.out +++ b/src/test/regress/expected/jsonb_1.out @@ -3127,11 +3127,7 @@ select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::j (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]'); - jsonb_set ---------------------------------------------------------------------- - {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} -(1 row) - +ERROR: path element at the position 2 is NULL select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}'); jsonb_set ------------------------------------------------------------------------- @@ -3151,11 +3147,7 @@ select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::j (1 row) select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}'); - jsonb_set ---------------------------------------------------------------------- - {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} -(1 row) - +ERROR: path element at the position 2 is NULL select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"'); jsonb_set -------------------------------------------------------------------------- @@ -3199,11 +3191,7 @@ select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{ (1 row) select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript - ?column? ---------------------------------------------------------------------- - {"a": 1, "b": [1, 2], "c": {"1": 2}, "d": {"1": [2, 3]}, "n": null} -(1 row) - +ERROR: path element at the position 2 is not an integer select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}'; ?column? ------------------------------------------------------------------ @@ -3331,3 +3319,9 @@ select jsonb_set('[]','{-99}','{"foo":123}'); [{"foo": 123}] (1 row) +select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"'); +ERROR: path element at the position 2 is not an integer +select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"'); +ERROR: path element at the position 3 is not an integer +select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"'); +ERROR: path element at the position 3 is NULL diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql index cb03ada1731..b1a0764cfaa 100644 --- a/src/test/regress/sql/jsonb.sql +++ b/src/test/regress/sql/jsonb.sql @@ -816,3 +816,6 @@ select jsonb_set('{}','{x}','{"foo":123}'); select jsonb_set('[]','{0}','{"foo":123}'); select jsonb_set('[]','{99}','{"foo":123}'); select jsonb_set('[]','{-99}','{"foo":123}'); +select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"'); +select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"'); +select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"'); |
