Skip to content

Commit c80da1c

Browse files
author
Nikita Glukhov
committed
Fix jsonb_populate_record() behavior on non-objects
1 parent d6b881a commit c80da1c

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/backend/utils/adt/jsonfuncs.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2681,7 +2681,19 @@ JsValueToJsObject(JsValue *jsv, JsObject *jso)
26812681
JsonContainerIsObject(jbv->val.binary.data))
26822682
jso->val.jsonb_cont = jbv->val.binary.data;
26832683
else
2684+
{
2685+
bool is_scalar = IsAJsonbScalar(jbv) ||
2686+
(jbv->type == jbvBinary &&
2687+
JsonContainerIsScalar(jbv->val.binary.data));
2688+
2689+
ereport(ERROR,
2690+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2691+
errmsg(is_scalar ? "cannot call %s on a scalar" :
2692+
"cannot call %s on an array",
2693+
"populate_composite")));
2694+
26842695
jso->val.jsonb_cont = NULL;
2696+
}
26852697
}
26862698
}
26872699

src/test/regress/expected/jsonb.out

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,17 +2276,9 @@ SELECT jsa FROM jsonb_populate_record(NULL::jsbrec, '{"jsa": ["aaa", null, [1, 2
22762276
(1 row)
22772277

22782278
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": 123}') q;
2279-
rec
2280-
------
2281-
(,,)
2282-
(1 row)
2283-
2279+
ERROR: cannot call populate_composite on a scalar
22842280
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": [1, 2]}') q;
2285-
rec
2286-
------
2287-
(,,)
2288-
(1 row)
2289-
2281+
ERROR: cannot call populate_composite on an array
22902282
SELECT rec FROM jsonb_populate_record(NULL::jsbrec, '{"rec": {"a": "abc", "c": "01.02.2003", "x": 43.2}}') q;
22912283
rec
22922284
-----------------------------------
@@ -2303,11 +2295,7 @@ SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": 123}') q;
23032295
ERROR: expected json array
23042296
HINT: see the value of key "reca"
23052297
SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [1, 2]}') q;
2306-
reca
2307-
-----------------
2308-
{"(,,)","(,,)"}
2309-
(1 row)
2310-
2298+
ERROR: cannot call populate_composite on a scalar
23112299
SELECT reca FROM jsonb_populate_record(NULL::jsbrec, '{"reca": [{"a": "abc", "b": 456}, null, {"c": "01.02.2003", "x": 43.2}]}') q;
23122300
reca
23132301
--------------------------------------------------------

0 commit comments

Comments
 (0)