Skip to content

Commit 4140993

Browse files
committed
🐛 fix for invalid polygons returned by the server
1 parent 503570d commit 4140993

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/main/java/com/mindee/parsing/generated/GeneratedObject.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,25 @@ public Polygon getPolygon() {
104104
* @return A {@link Polygon}.
105105
*/
106106
public Polygon getAsPolygon(String key) {
107-
if (this.containsKey(key)) {
108-
return PolygonUtils.getFrom((List<List<Double>>) this.get(key));
107+
if (!this.containsKey(key)) {
108+
return null;
109+
}
110+
Object rawPolygon = this.get(key);
111+
if (!(rawPolygon instanceof List && ((List<?>) rawPolygon).size() >= 4)) {
112+
return null;
113+
}
114+
for (Object point : (List<?>) rawPolygon) {
115+
if (!(point instanceof List) || ((List<?>) point).size() != 2) {
116+
return null;
117+
}
118+
for (Object coord : (List<?>) point) {
119+
if (!(coord instanceof Double)) {
120+
return null;
121+
}
122+
}
123+
}
124+
return PolygonUtils.getFrom((List<List<Double>>) rawPolygon);
109125
}
110-
return null;
111-
}
112126

113127
/**
114128
* Get the page ID, if present.

src/test/java/com/mindee/product/generated/GeneratedV1Test.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected AsyncPredictResponse<GeneratedV1> getAsyncPrediction(String name) thro
2929
GeneratedV1.class
3030
);
3131
return objectMapper.readValue(
32-
new File("src/test/resources/products/generated/response_v1/" + name + "_international_id_v1.json"),
32+
new File("src/test/resources/products/generated/response_v1/" + name + ".json"),
3333
type
3434
);
3535
}
@@ -62,9 +62,25 @@ protected PredictResponse<GeneratedV1> getSyncPrediction(String name) throws IOE
6262
);
6363
}
6464

65+
@Test
66+
void whenAsyncBadPolygonsDeserialized_mustHaveValidProperties() throws IOException {
67+
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("bad_polygons_driver_license_v1");
68+
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();
69+
70+
Map<String, GeneratedFeature> features = docPrediction.getFields();
71+
72+
// invalid polygon returned by the server
73+
StringField categoryField = features.get("category").asStringField();
74+
Assertions.assertNull(categoryField.getPolygon());
75+
76+
// valid polygon
77+
StringField mrzField = features.get("mrz").asStringField();
78+
Assertions.assertNotNull(mrzField.getPolygon());
79+
}
80+
6581
@Test
6682
void whenAsyncCompleteDeserialized_mustHaveValidProperties() throws IOException {
67-
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("complete");
83+
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("complete_international_id_v1");
6884
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();
6985

7086
Map<String, GeneratedFeature> features = docPrediction.getFields();
@@ -114,7 +130,7 @@ void whenAsyncCompleteDeserialized_mustHaveValidProperties() throws IOException
114130

115131
@Test
116132
void whenAsyncEmptyDeserialized_mustHaveValidProperties() throws IOException {
117-
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("empty");
133+
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("empty_international_id_v1");
118134
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();
119135

120136
Map<String, GeneratedFeature> features = docPrediction.getFields();

0 commit comments

Comments
 (0)