This is an automated email from the ASF dual-hosted git repository.

zeroshade pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 7a7e1a748e ARROW-16456: [Go] Fix RecordBuilder UnmarshalJSON when 
extra fields are present
7a7e1a748e is described below

commit 7a7e1a748eed50d7ac43ab0c158422da8c078841
Author: Phillip LeBlanc <[email protected]>
AuthorDate: Wed May 4 12:38:26 2022 -0400

    ARROW-16456: [Go] Fix RecordBuilder UnmarshalJSON when extra fields are 
present
    
    If RecordBuilder.UnmarshalJSON encountered an unknown field in the JSON 
document; it wouldn't consume the next JSON token representing the value.
    
    Fix this by manually calling `dec.Token()` and allowing the next call to 
`dec.Token()` to return the following key.
    
    Note that this only handles the case where the ignored value is a simple 
type, and not a nested array or object. I'm not sure what the correct fix for 
that would be.
    
    Closes #13065 from phillipleblanc/phillip/unmarshal-json-bug
    
    Authored-by: Phillip LeBlanc <[email protected]>
    Signed-off-by: Matthew Topol <[email protected]>
---
 go/arrow/array/json_reader_test.go | 24 ++++++++++++++++++++++++
 go/arrow/array/record.go           |  4 ++++
 2 files changed, 28 insertions(+)

diff --git a/go/arrow/array/json_reader_test.go 
b/go/arrow/array/json_reader_test.go
index 01524ab671..14e8ca0052 100644
--- a/go/arrow/array/json_reader_test.go
+++ b/go/arrow/array/json_reader_test.go
@@ -115,3 +115,27 @@ func TestJSONReaderChunked(t *testing.T) {
        assert.Equal(t, 4, n)
        assert.NoError(t, rdr.Err())
 }
+
+func TestUnmarshalJSON(t *testing.T) {
+       schema := arrow.NewSchema([]arrow.Field{
+               {Name: "region", Type: arrow.BinaryTypes.String, Nullable: 
true},
+               {Name: "model", Type: arrow.BinaryTypes.String},
+               {Name: "sales", Type: arrow.PrimitiveTypes.Float64, Nullable: 
true},
+       }, nil)
+
+       mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
+       defer mem.AssertSize(t, 0)
+
+       recordBuilder := array.NewRecordBuilder(mem, schema)
+       defer recordBuilder.Release()
+
+       jsondata := `{"region": "NY", "model": "3", "sales": 742.0, "extra": 
1234}`
+
+       err := recordBuilder.UnmarshalJSON([]byte(jsondata))
+       assert.NoError(t, err)
+
+       record := recordBuilder.NewRecord()
+       defer record.Release()
+
+       assert.NotNil(t, record)
+}
diff --git a/go/arrow/array/record.go b/go/arrow/array/record.go
index 0203e812d7..2bf14de131 100644
--- a/go/arrow/array/record.go
+++ b/go/arrow/array/record.go
@@ -353,6 +353,10 @@ func (b *RecordBuilder) UnmarshalJSON(data []byte) error {
 
                indices := b.schema.FieldIndices(key)
                if len(indices) == 0 {
+                       _, err = dec.Token()
+                       if err != nil {
+                               return err
+                       }
                        continue
                }
 

Reply via email to