Skip to content

Commit 467acc2

Browse files
author
Alexey Botchkov
committed
MDEV-13324 JSON_SET returns NULL instead of object.
Superfluous ',' was added to the JSON_SET result so it became invalid.
1 parent 49878be commit 467acc2

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

mysql-test/r/func_json.result

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,6 @@ JSON_OBJECT('foo', '`')
680680
SELECT JSON_OBJECT("foo", "bar`bar");
681681
JSON_OBJECT("foo", "bar`bar")
682682
{"foo": "bar`bar"}
683+
SELECT JSON_SET('{}', '$.age', 87);
684+
JSON_SET('{}', '$.age', 87)
685+
{"age": 87}

mysql-test/suite/json/r/json_no_table.result

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,10 +2109,9 @@ ERROR 42000: Incorrect parameter count in the call to native function 'json_set'
21092109
error ER_INVALID_JSON_TEXT_IN_PARAM
21102110
SELECT JSON_SET('{}', '$.name', JSON_EXTRACT('', '$'));
21112111
JSON_SET('{}', '$.name', JSON_EXTRACT('', '$'))
2112-
NULL
2112+
{"name": null}
21132113
Warnings:
21142114
Warning 4037 Unexpected end of JSON text in argument 1 to function 'json_extract'
2115-
Warning 4038 Syntax error in JSON text in argument 1 to function 'json_set' at position 2
21162115
select json_set('[1,2,3]', '$[2]', 4);
21172116
json_set('[1,2,3]', '$[2]', 4)
21182117
[1, 2, 4]

mysql-test/t/func_json.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,8 @@ select JSON_EXTRACT('{"foo": "bar" foobar foo invalid ', '$.foo');
328328
SELECT JSON_OBJECT('foo', '`');
329329
SELECT JSON_OBJECT("foo", "bar`bar");
330330

331+
#
332+
# MDEV-13324 JSON_SET returns NULL instead of object.
333+
#
334+
SELECT JSON_SET('{}', '$.age', 87);
335+

sql/item_jsonfunc.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,8 @@ String *Item_func_json_insert::val_str(String *str)
24502450
}
24512451
else /*JSON_PATH_KEY*/
24522452
{
2453+
uint n_key= 0;
2454+
24532455
if (je.value_type != JSON_VALUE_OBJECT)
24542456
continue;
24552457

@@ -2461,6 +2463,7 @@ String *Item_func_json_insert::val_str(String *str)
24612463
json_string_set_str(&key_name, lp->key, lp->key_end);
24622464
if (json_key_matches(&je, &key_name))
24632465
goto v_found;
2466+
n_key++;
24642467
if (json_skip_key(&je))
24652468
goto js_error;
24662469
break;
@@ -2478,7 +2481,8 @@ String *Item_func_json_insert::val_str(String *str)
24782481
v_to= (const char *) (je.s.c_str - je.sav_c_len);
24792482
str->length(0);
24802483
if (append_simple(str, js->ptr(), v_to - js->ptr()) ||
2481-
str->append(", \"", 3) ||
2484+
(n_key > 0 && str->append(", ", 2)) ||
2485+
str->append("\"", 1) ||
24822486
append_simple(str, lp->key, lp->key_end - lp->key) ||
24832487
str->append("\":", 2) ||
24842488
append_json_value(str, args[n_arg+1], &tmp_val) ||

0 commit comments

Comments
 (0)