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

asf-gitbox-commits pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 3a5c7add9b642c7ff28a4d97873dafdfb10d9811
Author: jsorel <[email protected]>
AuthorDate: Fri Nov 13 10:39:25 2026 +0100

    fix(Geojson): fix type written twice
---
 .../main/module-info.java                          |  2 +
 .../org/apache/sis/storage/geojson/GeoJSON.java    | 19 ++++-
 .../sis/storage/geojson/binding/GeoJSONObject.java | 18 ++++
 .../apache/sis/storage/geojson/GeoJSONTest.java    | 99 ++++++++++++++++++++++
 .../sis/storage/geojson/binding/BindingTest.java   | 85 ++++++++++++++++++-
 5 files changed, 217 insertions(+), 6 deletions(-)

diff --git a/incubator/src/org.apache.sis.storage.json/main/module-info.java 
b/incubator/src/org.apache.sis.storage.json/main/module-info.java
index 7f0f15692c..3cbbbbf8a5 100644
--- a/incubator/src/org.apache.sis.storage.json/main/module-info.java
+++ b/incubator/src/org.apache.sis.storage.json/main/module-info.java
@@ -44,6 +44,8 @@ module org.apache.sis.storage.json {
     exports org.apache.sis.storage.coveragejson;
     exports org.apache.sis.storage.coveragejson.binding;
     exports org.apache.sis.storage.geopose;
+    exports org.apache.sis.storage.geojson.binding;
+    exports org.apache.sis.storage.ogcjson;
     exports org.apache.sis.storage.geopose.binding;
     exports org.apache.sis.storage.ubjson;
 }
diff --git 
a/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/GeoJSON.java
 
b/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/GeoJSON.java
index 236030a34f..48d1762fc0 100644
--- 
a/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/GeoJSON.java
+++ 
b/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/GeoJSON.java
@@ -17,6 +17,7 @@
 package org.apache.sis.storage.geojson;
 
 import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.CoordinateXY;
@@ -66,6 +67,9 @@ public final class GeoJSON {
             geom = fromGeoJSON(jsongeom);
         }
 
+        // RFC 7946 allows a null "properties" member.
+        final Map<String,Object> properties = json.getProperties();
+
         if (type == null) {
             final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
             ftb.setName("geojson");
@@ -76,8 +80,13 @@ public final class GeoJSON {
             if (geom != null) {
                 
ftb.addAttribute(geom.getClass()).setName(AttributeConvention.GEOMETRY_PROPERTY).addRole(AttributeRole.DEFAULT_GEOMETRY);
             }
-            for (Entry<String,Object> entry : json.getProperties().entrySet()) 
{
-                
ftb.addAttribute(entry.getValue().getClass()).setName(entry.getKey());
+            if (properties != null) {
+                for (Entry<String,Object> entry : properties.entrySet()) {
+                    // A null value gives no information about the value type.
+                    final Object value = entry.getValue();
+                    final Class<?> valueClass = (value != null) ? 
value.getClass() : Object.class;
+                    ftb.addAttribute(valueClass).setName(entry.getKey());
+                }
             }
             type = ftb.build();
         }
@@ -92,8 +101,10 @@ public final class GeoJSON {
             feature.setPropertyValue(AttributeConvention.GEOMETRY, geom);
         }
 
-        for (Entry<String,Object> entry : json.getProperties().entrySet()) {
-            feature.setPropertyValue(entry.getKey(), entry.getValue());
+        if (properties != null) {
+            for (Entry<String,Object> entry : properties.entrySet()) {
+                feature.setPropertyValue(entry.getKey(), entry.getValue());
+            }
         }
         return feature;
     }
diff --git 
a/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/binding/GeoJSONObject.java
 
b/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/binding/GeoJSONObject.java
index f7e0312e2f..4e30153951 100644
--- 
a/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/binding/GeoJSONObject.java
+++ 
b/incubator/src/org.apache.sis.storage.json/main/org/apache/sis/storage/geojson/binding/GeoJSONObject.java
@@ -65,6 +65,24 @@ public abstract class GeoJSONObject extends 
DataTransferObject {
     @JsonInclude(value = JsonInclude.Include.ALWAYS)
     public abstract String getType();
 
+    /**
+     * Verifies the value of the {@code type} member read from JSON.
+     * The type is fixed by the class, so this method only checks that the 
given value is the type of this object.
+     *
+     * <p>This setter also prevents the {@code type} member from being 
collected as an unknown field,
+     * which would cause it to be written twice.</p>
+     *
+     * @param  type  value of the {@code type} member.
+     * @throws IllegalArgumentException if the given type is not the type of 
this object.
+     */
+    @JsonProperty(PROPERTY_TYPE)
+    public void setType(final String type) {
+        final String expected = getType();
+        if (!expected.equals(type)) {
+            throw new IllegalArgumentException("Expected a GeoJSON object of 
type \"" + expected + "\" but found \"" + type + "\".");
+        }
+    }
+
     /**
      * Get bbox
      *
diff --git 
a/incubator/src/org.apache.sis.storage.json/test/org/apache/sis/storage/geojson/GeoJSONTest.java
 
b/incubator/src/org.apache.sis.storage.json/test/org/apache/sis/storage/geojson/GeoJSONTest.java
new file mode 100644
index 0000000000..4a4fbd609f
--- /dev/null
+++ 
b/incubator/src/org.apache.sis.storage.json/test/org/apache/sis/storage/geojson/GeoJSONTest.java
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.storage.geojson;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.locationtech.jts.geom.Point;
+import org.apache.sis.feature.internal.shared.AttributeConvention;
+import org.apache.sis.storage.geojson.binding.GeoJSONFeature;
+
+// Test dependencies
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+// Specific to the geoapi-3.1 and geoapi-4.0 branches:
+import org.opengis.feature.AttributeType;
+import org.opengis.feature.Feature;
+import org.opengis.feature.FeatureType;
+
+
+/**
+ * Tests {@link GeoJSON}.
+ *
+ * @author Johann Sorel (Geomatys)
+ */
+public final class GeoJSONTest {
+
+    private final ObjectMapper mapper = new ObjectMapper();
+
+    /**
+     * Parses the given JSON text as a GeoJSON feature.
+     */
+    private GeoJSONFeature parse(final String json) throws Exception {
+        return mapper.readValue(json, GeoJSONFeature.class);
+    }
+
+    /**
+     * Returns the number of properties which are not SIS conventions.
+     */
+    private static long countUserProperties(final FeatureType type) {
+        return type.getProperties(true).stream().filter((p) -> 
!AttributeConvention.contains(p.getName())).count();
+    }
+
+    /**
+     * Tests a feature with a null {@code properties} member, as allowed by 
RFC 7946 section 3.2.
+     *
+     * @throws Exception if an error occurred while reading the feature.
+     */
+    @Test
+    public void testNullProperties() throws Exception {
+        final GeoJSONFeature json = parse(
+                
"{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1,2]},\"properties\":null}");
+        assertNull(json.getProperties());
+
+        final Feature feature = GeoJSON.fromGeoJSON(json, null);
+        assertInstanceOf(Point.class, 
feature.getPropertyValue(AttributeConvention.GEOMETRY));
+        assertEquals(0, countUserProperties(feature.getType()));
+
+        // Same test with a feature type given by the caller.
+        final Feature other = GeoJSON.fromGeoJSON(json, feature.getType());
+        assertInstanceOf(Point.class, 
other.getPropertyValue(AttributeConvention.GEOMETRY));
+    }
+
+    /**
+     * Tests a feature with a property having a null value.
+     *
+     * @throws Exception if an error occurred while reading the feature.
+     */
+    @Test
+    public void testNullPropertyValue() throws Exception {
+        final GeoJSONFeature json = parse(
+                
"{\"type\":\"Feature\",\"geometry\":null,\"properties\":{\"a\":null,\"b\":\"text\"}}");
+
+        final Feature feature = GeoJSON.fromGeoJSON(json, null);
+        assertNull(feature.getPropertyValue("a"));
+        assertEquals("text", feature.getPropertyValue("b"));
+        assertEquals(Object.class, ((AttributeType<?>) 
feature.getType().getProperty("a")).getValueClass());
+        assertEquals(String.class, ((AttributeType<?>) 
feature.getType().getProperty("b")).getValueClass());
+        assertEquals(2, countUserProperties(feature.getType()));
+
+        // Same test with a feature type given by the caller.
+        final Feature other = GeoJSON.fromGeoJSON(json, feature.getType());
+        assertNull(other.getPropertyValue("a"));
+        assertEquals("text", other.getPropertyValue("b"));
+    }
+}
diff --git 
a/incubator/src/org.apache.sis.storage.json/test/org/apache/sis/storage/geojson/binding/BindingTest.java
 
b/incubator/src/org.apache.sis.storage.json/test/org/apache/sis/storage/geojson/binding/BindingTest.java
index 872200d2aa..80fa0e829f 100644
--- 
a/incubator/src/org.apache.sis.storage.json/test/org/apache/sis/storage/geojson/binding/BindingTest.java
+++ 
b/incubator/src/org.apache.sis.storage.json/test/org/apache/sis/storage/geojson/binding/BindingTest.java
@@ -9,15 +9,25 @@
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on anz "AS IS" BASIS,
+ * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.sis.storage.geojson.binding;
 
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import com.fasterxml.jackson.core.StreamReadFeature;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.json.JsonMapper;
+
 // Test dependencies
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestInstance;
+import static org.junit.jupiter.api.Assertions.*;
 import org.apache.sis.storage.json.AbstractBindingTest;
 
 
@@ -29,5 +39,76 @@ import org.apache.sis.storage.json.AbstractBindingTest;
 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
 public class BindingTest extends AbstractBindingTest {
 
-    //TODO
+    /**
+     * A mapper which rejects JSON objects having the same member twice.
+     */
+    private final ObjectMapper strict = JsonMapper.builder()
+            .enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION)
+            .build();
+
+    /**
+     * Writes the given object and verifies that the result does not contain 
duplicated members.
+     */
+    private void assertNoDuplicatedMember(final Object object) throws 
IOException {
+        final String json = mapper.writeValueAsString(object);
+        assertDoesNotThrow(() -> strict.readTree(json), json);
+    }
+
+    /**
+     * Tests reading and writing a feature.
+     * The {@code type} member shall not be collected as an unknown field, and 
shall be written only once.
+     *
+     * @throws IOException if an error occurred while reading or writing the 
JSON.
+     */
+    @Test
+    public void testFeature() throws IOException {
+        final GeoJSONPoint point = new GeoJSONPoint();
+        point.setCoordinates(List.of(102.0, 0.5));
+        final GeoJSONFeature expected = new GeoJSONFeature();
+        expected.setGeometry(point);
+        expected.setProperties(Map.of("prop0", "value0"));
+        compare("geojson_feature.json", expected);
+
+        final GeoJSONFeature feature = 
mapper.readValue(readResource("geojson_feature.json"), GeoJSONFeature.class);
+        assertTrue(feature.otherFields().isEmpty(), "Unexpected unknown 
fields: " + feature.otherFields());
+        assertNoDuplicatedMember(feature);
+    }
+
+    /**
+     * Tests reading and writing a feature collection.
+     * The {@code type} member shall be written only once, both on the 
collection and on its features.
+     *
+     * @throws IOException if an error occurred while reading or writing the 
JSON.
+     */
+    @Test
+    public void testFeatureCollection() throws IOException {
+        final GeoJSONFeatureCollection collection = mapper.readValue(
+                readResource("geojson_featurecollection.json"), 
GeoJSONFeatureCollection.class);
+        assertTrue(collection.otherFields().isEmpty(), "Unexpected unknown 
fields: " + collection.otherFields());
+        assertEquals(3, collection.getFeatures().size());
+        for (final GeoJSONFeature feature : collection.getFeatures()) {
+            assertTrue(feature.otherFields().isEmpty(), "Unexpected unknown 
fields: " + feature.otherFields());
+        }
+        assertNoDuplicatedMember(collection);
+    }
+
+    /**
+     * Tests that an object with a {@code type} member different than the 
expected type is rejected.
+     */
+    @Test
+    public void testWrongType() {
+        JsonMappingException e;
+        e = assertThrows(JsonMappingException.class, () -> mapper.readValue(
+                "{\"type\":\"FeatureCollection\",\"features\":[]}", 
GeoJSONFeature.class));
+        assertTrue(e.getMessage().contains("FeatureCollection"), 
e.getMessage());
+
+        e = assertThrows(JsonMappingException.class, () -> mapper.readValue(
+                
"{\"type\":\"Feature\",\"geometry\":null,\"properties\":null}", 
GeoJSONFeatureCollection.class));
+        assertTrue(e.getMessage().contains("Feature"), e.getMessage());
+
+        // A geometry where a feature is expected in the "features" array.
+        assertThrows(JsonMappingException.class, () -> mapper.readValue(
+                
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Point\",\"coordinates\":[1,2]}]}",
+                GeoJSONFeatureCollection.class));
+    }
 }

Reply via email to