From 777bafdba382210e1ed42ea3b21156d5a7324cf0 Mon Sep 17 00:00:00 2001 From: Gabriel Landais Date: Wed, 26 Jan 2022 09:05:19 +0100 Subject: [PATCH] Fix for #143 : Parsing a GeoJSON feature with null geometry fails --- .../geom/json/FeatureDeserializer.java | 2 +- .../geom/json/FeatureDeserializationTest.java | 25 ++++++++++++++++--- .../geolatte/geom/json/GeoJsonStrings.java | 1 + 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/json/src/main/java/org/geolatte/geom/json/FeatureDeserializer.java b/json/src/main/java/org/geolatte/geom/json/FeatureDeserializer.java index 65a45651..f09b74b7 100644 --- a/json/src/main/java/org/geolatte/geom/json/FeatureDeserializer.java +++ b/json/src/main/java/org/geolatte/geom/json/FeatureDeserializer.java @@ -36,7 +36,7 @@ public Feature deserialize(JsonParser p, DeserializationContext ctxt) throw GeoJsonFeature readFeature(ObjectCodec oc, JsonNode root) throws JsonProcessingException { JsonNode geomNode = root.get("geometry"); - Geometry geom = null == geomNode ? null : geomParser.parseGeometry(geomNode); + Geometry geom = (null == geomNode || geomNode.isNull()) ? null : geomParser.parseGeometry(geomNode); Object id = null; JsonNode idNode = root.get("id"); diff --git a/json/src/test/java/org/geolatte/geom/json/FeatureDeserializationTest.java b/json/src/test/java/org/geolatte/geom/json/FeatureDeserializationTest.java index c6e0bf10..c1f059b9 100644 --- a/json/src/test/java/org/geolatte/geom/json/FeatureDeserializationTest.java +++ b/json/src/test/java/org/geolatte/geom/json/FeatureDeserializationTest.java @@ -14,9 +14,7 @@ import static org.geolatte.geom.builder.DSL.g; import static org.geolatte.geom.builder.DSL.point; import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84; -import static org.geolatte.geom.json.GeoJsonStrings.emptyLineString; -import static org.geolatte.geom.json.GeoJsonStrings.feature; -import static org.geolatte.geom.json.GeoJsonStrings.featureIntId; +import static org.geolatte.geom.json.GeoJsonStrings.*; import static org.geolatte.geom.json.Setting.SUPPRESS_CRS_SERIALIZATION; import static org.junit.Assert.assertEquals; @@ -32,6 +30,10 @@ public void testDeSerialize() throws IOException { map.put("a", 1); Feature feature = new GeoJsonFeature<>(point(WGS84, g(1, 2)), "1", map); assertEquals(feature,rec) ; + assertEquals(feature.getId(),rec.getId()) ; + assertEquals(feature.getType(),rec.getType()) ; + assertEquals(feature.getGeometry(),rec.getGeometry()) ; + assertEquals(feature.getProperties(),rec.getProperties()) ; } @Test @@ -41,6 +43,23 @@ public void testDeSerializeWithIntAsId() throws IOException { map.put("a", 1); Feature feature = new GeoJsonFeature<>(point(WGS84, g(1, 2)), 1L, map); assertEquals(feature,rec) ; + assertEquals(feature.getId(),rec.getId()) ; + assertEquals(feature.getType(),rec.getType()) ; + assertEquals(feature.getGeometry(),rec.getGeometry()) ; + assertEquals(feature.getProperties(),rec.getProperties()) ; + } + + @Test + public void testDeSerializeWithNullGeometry() throws IOException { + Feature rec = mapper.readValue(featureNullGeometry, Feature.class); + Map map = new HashMap<>(); + map.put("a", 1); + Feature feature = new GeoJsonFeature<>(null, "1", map); + assertEquals(feature,rec) ; + assertEquals(feature.getId(),rec.getId()) ; + assertEquals(feature.getType(),rec.getType()) ; + assertEquals(feature.getGeometry(),rec.getGeometry()) ; + assertEquals(feature.getProperties(),rec.getProperties()) ; } } diff --git a/json/src/test/java/org/geolatte/geom/json/GeoJsonStrings.java b/json/src/test/java/org/geolatte/geom/json/GeoJsonStrings.java index b28ad664..d6f4fc3a 100644 --- a/json/src/test/java/org/geolatte/geom/json/GeoJsonStrings.java +++ b/json/src/test/java/org/geolatte/geom/json/GeoJsonStrings.java @@ -58,6 +58,7 @@ public class GeoJsonStrings { static String featureIntId = "{\"type\" : \"Feature\", \"id\": 1, \"geometry\": {\"type\":\"Point\",\"coordinates\":[1.0,2.0]}, " + "\"properties\": { \"a\" : 1 }}"; + static String featureNullGeometry = "{\"geometry\":null,\"id\":\"1\",\"properties\":{\"a\":1},\"type\":\"Feature\"}"; // crs static String crswgs84 = "{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}";