Skip to content

Commit cfc9037

Browse files
committed
Decode all integral value types in GeoJson decoders
JAVA-3646
1 parent 6f097e1 commit cfc9037

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

driver-core/src/main/com/mongodb/client/model/geojson/codecs/GeometryDecoderHelper.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,7 @@ private static Position decodePosition(final BsonReader reader) {
392392
reader.readStartArray();
393393
List<Double> values = new ArrayList<Double>();
394394
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
395-
if (reader.getCurrentBsonType() != BsonType.DOUBLE) {
396-
throw new CodecConfigurationException("Invalid position");
397-
}
398-
values.add(reader.readDouble());
395+
values.add(readAsDouble(reader));
399396
}
400397
reader.readEndArray();
401398

@@ -406,6 +403,19 @@ private static Position decodePosition(final BsonReader reader) {
406403
}
407404
}
408405

406+
private static double readAsDouble(final BsonReader reader) {
407+
if (reader.getCurrentBsonType() == BsonType.DOUBLE) {
408+
return reader.readDouble();
409+
} else if (reader.getCurrentBsonType() == BsonType.INT32) {
410+
return reader.readInt32();
411+
} else if (reader.getCurrentBsonType() == BsonType.INT64) {
412+
return reader.readInt64();
413+
}
414+
415+
throw new CodecConfigurationException("A GeoJSON position value must be a numerical type, but the value is of type "
416+
+ reader.getCurrentBsonType());
417+
}
418+
409419
@Nullable
410420
static CoordinateReferenceSystem decodeCoordinateReferenceSystem(final BsonReader reader) {
411421
String crsName = null;

driver-core/src/test/unit/com/mongodb/client/model/geojson/codecs/GeometryCollectionCodecSpecification.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.bson.BsonDocumentWriter
3131
import org.bson.codecs.DecoderContext
3232
import org.bson.codecs.EncoderContext
3333
import org.bson.codecs.configuration.CodecConfigurationException
34+
import org.bson.json.JsonReader
3435
import spock.lang.Specification
3536

3637
import static com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem.EPSG_4326_STRICT_WINDING
@@ -154,6 +155,19 @@ class GeometryCollectionCodecSpecification extends Specification {
154155
geometryCollection == decodedGeometryCollection
155156
}
156157

158+
def 'should decode integral value types'() {
159+
given:
160+
def jsonRepresentation = '{type: "LineString", coordinates: [ [101.0, 0], [102.0, 2147483648] ] }'
161+
def expectedGeometry = new LineString([new Position(101d, 0d), new Position(102d, 2147483648d)])
162+
def codec = registry.get(LineString)
163+
164+
when:
165+
def decodedGeometry = codec.decode(new JsonReader(jsonRepresentation), DecoderContext.builder().build())
166+
167+
then:
168+
decodedGeometry == expectedGeometry
169+
}
170+
157171
def 'should throw when decoding invalid documents'() {
158172
when:
159173
codec.decode(new BsonDocumentReader(parse(invalidJson)), DecoderContext.builder().build())

0 commit comments

Comments
 (0)