From 15139b8fa5bcaf9b5fedc08393e10e18baeedd74 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Thu, 5 Feb 2015 16:15:05 -0500 Subject: [PATCH] fix panic on mapping value containing null added testcase to reproduce initial issue closes #153 --- mapping_document.go | 4 ++++ mapping_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/mapping_document.go b/mapping_document.go index fc74a67f..aed87eae 100644 --- a/mapping_document.go +++ b/mapping_document.go @@ -270,6 +270,10 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string, } propertyValue := reflect.ValueOf(property) + if !propertyValue.IsValid() { + // cannot do anything with the zero value + return + } propertyType := propertyValue.Type() switch propertyType.Kind() { case reflect.String: diff --git a/mapping_test.go b/mapping_test.go index 417674ec..00029859 100644 --- a/mapping_test.go +++ b/mapping_test.go @@ -183,3 +183,35 @@ func TestMappingStructWithPointerToString(t *testing.T) { t.Errorf("expected to find 1 find, found %d", count) } } + +func TestMappingJSONWithNull(t *testing.T) { + + mapping := NewIndexMapping() + + jsonbytes := []byte(`{"name":"marty", "age": null}`) + var jsondoc interface{} + err := json.Unmarshal(jsonbytes, &jsondoc) + if err != nil { + t.Fatal(err) + } + + doc := document.NewDocument("1") + err = mapping.mapDocument(doc, jsondoc) + if err != nil { + t.Fatal(err) + } + found := false + count := 0 + for _, f := range doc.Fields { + if f.Name() == "name" { + found = true + } + count++ + } + if !found { + t.Errorf("expected to find field named 'name'") + } + if count != 1 { + t.Errorf("expected to find 1 find, found %d", count) + } +}