Skip to content

Commit

Permalink
geojson: don't error on nil geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Jan 16, 2021
1 parent 3cb1eb0 commit c6d2869
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions geojson/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,20 @@ func (f *Feature) UnmarshalJSON(data []byte) error {
return fmt.Errorf("geojson: not a feature: type=%s", jf.Type)
}

if jf.Geometry == nil || (jf.Geometry.Coordinates == nil && jf.Geometry.Geometries == nil) {
return ErrInvalidGeometry
var g orb.Geometry
if jf.Geometry != nil {
if jf.Geometry.Coordinates == nil && jf.Geometry.Geometries == nil {
return ErrInvalidGeometry
}
g = jf.Geometry.Geometry()
}

*f = Feature{
ID: jf.ID,
Type: jf.Type,
Properties: jf.Properties,
BBox: jf.BBox,
Geometry: jf.Geometry.Geometry(),
Geometry: g,
}

return nil
Expand Down
10 changes: 7 additions & 3 deletions geojson/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,13 @@ func TestUnmarshalFeature_missingGeometry(t *testing.T) {
t.Run("missing geometry", func(t *testing.T) {
rawJSON := `{ "type": "Feature" }`

_, err := UnmarshalFeature([]byte(rawJSON))
if err != ErrInvalidGeometry {
t.Fatalf("incorrect unmarshal error: %v", err)
f, err := UnmarshalFeature([]byte(rawJSON))
if err != nil {
t.Fatalf("should not error: %v", err)
}

if f == nil {
t.Fatalf("feature should not be nil")
}
})
}
Expand Down

0 comments on commit c6d2869

Please sign in to comment.