Skip to content

Commit

Permalink
[DHUB-3346] - Add validation for geojson request parameter (#917)
Browse files Browse the repository at this point in the history
* [DHUB-3346] Add validation for Geometry

Added a validation to check if coordinates are not null in the request
Replaced $body with args in jexl-expression, $body isn't used anymore
  • Loading branch information
kad-switzc committed Jan 16, 2024
1 parent 3fe8f71 commit 3299f52
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public EncodedParameter encode(Object value, int dataType) {
Assert.requireType(value, Geometry.class, "value must be Geometry type");
var geometry = (Geometry) value;

Assert.requireNonNull(geometry.getCoordinate(), "Encoding of geometry failed, coordinate can't be empty.");

var outputDimension = Double.isNaN(geometry.getCoordinate()
.getZ()) ? 2 : 3;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ void encode_returnsEncodedParameter_forGeometry() {
assertThat(actual, notNullValue());
}

@Test
void encode_throwsException_forInvalidGeometry() {
var geometry = geometryFactory.createPoint();

var thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> geometryCodec.encode(geometry));
assertThat(thrown.getMessage(), equalTo("Encoding of geometry failed, coordinate can't be empty."));
}

@Test
void decode_returnsGeometry_forByteBufTextFormatted() {
var geometry = geometryFactory.createPoint(new Coordinate(1, 2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,16 @@ paths:
x-dws-expr: "$header.'content-crs' == 'epsg:28992' ? 7415 : 7931"
contains:
fromGeoJSON:
x-dws-expr: "json:asString($body._geo.contains)!"
x-dws-expr: "json:asString(args._geo.contains)!"
intersects:
fromGeoJSON:
x-dws-expr: "json:asString($body._geo.intersects)!"
x-dws-expr: "json:asString(args._geo.intersects)!"
touches:
fromGeoJSON:
x-dws-expr: "json:asString($body._geo.touches)!"
x-dws-expr: "json:asString(args._geo.touches)!"
within:
fromGeoJSON:
x-dws-expr: "json:asString($body._geo.within)!"
x-dws-expr: "json:asString(args._geo.within)!"
parameters:
- name: content-crs
in: header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
Expand Down Expand Up @@ -72,8 +73,16 @@ private static Geometry getGeometryFromWkb(String wkb) {

private static Geometry getGeometryFromGeoJson(String geoJson) {
var geoJsonReader = new GeoJsonReader();

try {
return geoJsonReader.read(geoJson);
var geometry = geoJsonReader.read(geoJson);

// Type is validated in het locationtech library.
if (Objects.isNull(geometry.getCoordinate())) {
throw requestValidationException("Coordinates can't be null!");
}

return geometry;
} catch (ParseException e) {
throw requestValidationException("The filter input GeoJSON is invalid!", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,20 @@ void readGeometry_forWkb_returnsGeometry() {

@Test
void readGeometry_forGeoJson_returnsGeometry() {
var geoJson = "{\n" + " \"type\": \"Point\",\n" + " \"coordinates\": [\n" + " 194936.73,\n" + " 470973.96\n"
+ " ],\n" + " \"crs\": {\n" + " \"type\": \"name\",\n" + " \"properties\": {\n"
+ " \"name\": \"EPSG:28992\"\n" + " }\n" + " }\n" + "}";
var geoJson = """
{
"type": "Point",
"coordinates": [
194936.73,
470973.96
],
"crs": {
"type": "name",
"properties": {
"name": "EPSG:28992"
}
}
}""";
Map<String, Object> data = Map.of(FROM_GEOJSON, geoJson);

Geometry geometry = GeometryReader.readGeometry(data);
Expand All @@ -86,6 +97,25 @@ void readGeometry_forGeoJson_returnsGeometry() {
.getZ()), is(true));
}

@Test
void getGeometryFromGeoJson_throwsException_whenCoordinatesAreNull() {
var geoJson = """
{
"type": "Point",
"crs": {
"type": "name",
"properties": {
"name": "EPSG:28992"
}
}
}""";

Map<String, Object> data = Map.of(FROM_GEOJSON, geoJson);

var exception = assertThrows(RequestValidationException.class, () -> GeometryReader.readGeometry(data));
assertThat(exception.getMessage(), is("Coordinates can't be null!"));
}

@Test
void readGeometry_forIllegalWkt_returnsException() {
var wkt = "POINT (194936.73 470973.96";
Expand Down

0 comments on commit 3299f52

Please sign in to comment.