Skip to content

Not all Json null case is handled #1691

@bat-bold

Description

@bat-bold

Describe the bug

select 'null'::jsonb

Starting from v5.3.0, in some case, it does not Scan as Nil .
v5.2.0 works.

To Reproduce

func TestJSONBCodecUnmarshalSQLNull(t *testing.T) {
	defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
		// Slices are nilified
		slice := []string{"foo", "bar", "baz"}
		err := conn.QueryRow(ctx, "select null::jsonb").Scan(&slice)
		require.NoError(t, err)
		require.Nil(t, slice)

                sliceJsonNull := []string{"foo1", "bar1", "baz1"}
		err = conn.QueryRow(ctx, "select 'null'::jsonb").Scan(&sliceJsonNull)
		require.NoError(t, err)
/*Pass*/	require.Nil(t, sliceJsonNull)  //// Pass

		// Maps are nilified
		m := map[string]any{"foo": "bar"}
		err = conn.QueryRow(ctx, "select null::jsonb").Scan(&m)
		require.NoError(t, err)
		require.Nil(t, m)

                mJsonNull := map[string]any{"foo": "bar"}
		err = conn.QueryRow(ctx, "select 'null'::jsonb").Scan(&mJsonNull)
		require.NoError(t, err)
/*Pass*/	require.Nil(t, mJsonNull)  //// Pass

		m = map[string]interface{}{"foo": "bar"}
		err = conn.QueryRow(ctx, "select null::jsonb").Scan(&m)
		require.NoError(t, err)
		require.Nil(t, m)

                mJsonNull = map[string]interface{}{"foo": "bar"}
		err = conn.QueryRow(ctx, "select 'null'::jsonb").Scan(&m)
		require.NoError(t, err)
/*Pass*/	require.Nil(t, m)  //// Pass

		// Pointer to pointer are nilified
		n := 42
		p := &n
		err = conn.QueryRow(ctx, "select null::jsonb").Scan(&p)
		require.NoError(t, err)
		require.Nil(t, p)

                n1 := 44
		p1 := &n1
		err = conn.QueryRow(ctx, "select 'null'::jsonb").Scan(&p1)
		require.NoError(t, err)
/*Fail*/	require.Nil(t, p1)  // *p1 == 0

                type ImageSize struct {
			Height int    `json:"height"`
			Width  int    `json:"width"`
			Str    string `json:"str"`
		}
		is1 := &ImageSize{Height: 100, Width: 100, Str: "str"}
		err = conn.QueryRow(ctx, `select null::jsonb`).Scan(&is1)
		require.NoError(t, err)
		require.Nil(t, is1)  //// Pass

                is2 := &ImageSize{Height: 100, Width: 100, Str: "str"}
		err = conn.QueryRow(ctx, `select 'null'::jsonb`).Scan(&is2)
		require.NoError(t, err)
/*Fail*/	require.Nil(t, is2) //// ImageSize{Height: 0, Width: 0, Str: ""}

		// A string cannot scan a NULL.
		str := "foobar"
		err = conn.QueryRow(ctx, "select null::jsonb").Scan(&str)
		require.EqualError(t, err, "can't scan into dest[0]: cannot scan NULL into *string")

		// A non-string cannot scan a NULL.
		err = conn.QueryRow(ctx, "select null::jsonb").Scan(&n)
		require.EqualError(t, err, "can't scan into dest[0]: cannot scan NULL into *int")
	})
}

Version

  • go version go1.20.6 darwin/amd64
  • PostgreSQL 14.6 on aarch64-apple-darwin20.6.0, compiled by Apple clang version 12.0.5 (clang-1205.0.22.9), 64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions