Skip to content

🐛 fix for invalid polygons returned by the server #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/main/java/com/mindee/parsing/generated/GeneratedObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,26 @@ public Polygon getPolygon() {
* @return A {@link Polygon}.
*/
public Polygon getAsPolygon(String key) {
if (this.containsKey(key)) {
return PolygonUtils.getFrom((List<List<Double>>) this.get(key));
if (!this.containsKey(key)) {
return null;
}
return null;
Object rawPolygon = this.get(key);
// a valid polygon must have at least 4 points
if (!(rawPolygon instanceof List && ((List<?>) rawPolygon).size() >= 4)) {
return null;
}
// a valid point must have exactly 2 coordinates
for (Object point : (List<?>) rawPolygon) {
if (!(point instanceof List) || ((List<?>) point).size() != 2) {
return null;
}
for (Object coord : (List<?>) point) {
if (!(coord instanceof Double)) {
return null;
}
}
}
return PolygonUtils.getFrom((List<List<Double>>) rawPolygon);
}

/**
Expand Down
22 changes: 19 additions & 3 deletions src/test/java/com/mindee/product/generated/GeneratedV1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected AsyncPredictResponse<GeneratedV1> getAsyncPrediction(String name) thro
GeneratedV1.class
);
return objectMapper.readValue(
new File("src/test/resources/products/generated/response_v1/" + name + "_international_id_v1.json"),
new File("src/test/resources/products/generated/response_v1/" + name + ".json"),
type
);
}
Expand Down Expand Up @@ -62,9 +62,25 @@ protected PredictResponse<GeneratedV1> getSyncPrediction(String name) throws IOE
);
}

@Test
void whenAsyncBadPolygonsDeserialized_mustHaveValidProperties() throws IOException {
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("bad_polygons_driver_license_v1");
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();

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

// invalid polygon returned by the server
StringField categoryField = features.get("category").asStringField();
Assertions.assertNull(categoryField.getPolygon());

// valid polygon
StringField mrzField = features.get("mrz").asStringField();
Assertions.assertNotNull(mrzField.getPolygon());
}

@Test
void whenAsyncCompleteDeserialized_mustHaveValidProperties() throws IOException {
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("complete");
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("complete_international_id_v1");
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();

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

@Test
void whenAsyncEmptyDeserialized_mustHaveValidProperties() throws IOException {
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("empty");
AsyncPredictResponse<GeneratedV1> response = getAsyncPrediction("empty_international_id_v1");
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();

Map<String, GeneratedFeature> features = docPrediction.getFields();
Expand Down
Loading