Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances
Date
Msg-id 26373.1456967099@sss.pgh.pa.us
Whole thread Raw
In response to BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances  (johann@visagie.za.net)
Responses Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances
List pgsql-bugs
johann@visagie.za.net writes:
> SELECT t.*
> FROM json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
> AS t(a int, b json, c text, x int);

> However, if (as in this example) another key in the JSON object - `b` in
> this case - refers to a nested JSON object which *does* contain a key `c`,
> the function returns a malformed JSON string as (text)  value for column
> `c`:

>  a |       b        |    c    | x
> ---+----------------+---------+---
>  1 | {"c":16,"d":2} | {"c":16 | 8
> (1 row)


AFAICT this is a simple thinko in the hash_object_field_end() callback,
as per attached patch that fixes this and doesn't break any existing
regression test cases.  Andrew, do you concur that this is correct,
or is there something I'm missing about the tracking of lex_level?

            regards, tom lane

diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 88225aa..363afa7 100644
*** a/src/backend/utils/adt/jsonfuncs.c
--- b/src/backend/utils/adt/jsonfuncs.c
*************** hash_object_field_end(void *state, char
*** 2438,2444 ****
      /*
       * Ignore nested fields.
       */
!     if (_state->lex->lex_level > 2)
          return;

      /*
--- 2438,2444 ----
      /*
       * Ignore nested fields.
       */
!     if (_state->lex->lex_level > 1)
          return;

      /*

pgsql-bugs by date:

Previous
From: Michael Paquier
Date:
Subject: Re: BUG #13770: Extending recovery_min_apply_delay on Standby causes it to be unavailable for a while
Next
From: Andrew Dunstan
Date:
Subject: Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances