Skip to content

Commit 4da1226

Browse files
🐛 fix missing boolean field accessor for generated objects (#219)
1 parent 8234633 commit 4da1226

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mindee.MindeeException;
44
import com.mindee.parsing.standard.AmountField;
5+
import com.mindee.parsing.standard.BooleanField;
56
import com.mindee.parsing.standard.ClassificationField;
67
import com.mindee.parsing.standard.DateField;
78
import com.mindee.parsing.standard.StringField;
@@ -34,6 +35,7 @@ public class GeneratedFeature extends ArrayList<GeneratedObject> {
3435

3536
/**
3637
* Whether the original feature is a list.
38+
* @param isList Whether the feature is a list.
3739
*/
3840
public GeneratedFeature(boolean isList) {
3941
this.isList = isList;
@@ -42,6 +44,7 @@ public GeneratedFeature(boolean isList) {
4244
/**
4345
* Represent the feature as a standard {@link StringField}.
4446
* Only works for non-list features.
47+
* @return An instance of a {@link StringField}.
4548
*/
4649
public StringField asStringField() {
4750
if (this.isList) {
@@ -50,9 +53,20 @@ public StringField asStringField() {
5053
return this.get(0).asStringField();
5154
}
5255

56+
/**
57+
* @return An instance of a {@link BooleanField}.
58+
*/
59+
public BooleanField asBooleanField() {
60+
if (this.isList) {
61+
throw new MindeeException("Cannot convert a list feature into a BooleanField.");
62+
}
63+
return this.get(0).asBooleanField();
64+
}
65+
5366
/**
5467
* Represent the feature as a standard {@link AmountField}.
5568
* Only works for non-list features.
69+
* @return An instance of a {@link AmountField}.
5670
*/
5771
public AmountField asAmountField() {
5872
if (this.isList) {
@@ -64,6 +78,7 @@ public AmountField asAmountField() {
6478
/**
6579
* Represent the feature as a standard {@link DateField}.
6680
* Only works for non-list features.
81+
* @return An instance of a {@link DateField}.
6782
*/
6883
public DateField asDateField() {
6984
if (this.isList) {
@@ -75,6 +90,7 @@ public DateField asDateField() {
7590
/**
7691
* Represent the feature as a standard {@link ClassificationField}.
7792
* Only works for non-list features.
93+
* @return An instance of a {@link ClassificationField}.
7894
*/
7995
public ClassificationField asClassificationField() {
8096
if (this.isList) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.mindee.geometry.Polygon;
44
import com.mindee.geometry.PolygonUtils;
55
import com.mindee.parsing.standard.AmountField;
6+
import com.mindee.parsing.standard.BooleanField;
67
import com.mindee.parsing.standard.ClassificationField;
78
import com.mindee.parsing.standard.DateField;
89
import com.mindee.parsing.standard.StringField;
@@ -17,6 +18,7 @@ public class GeneratedObject extends HashMap<String, Object> {
1718

1819
/**
1920
* Represent the object as a standard {@link StringField}.
21+
* @return A {@link StringField} containing a string value.
2022
*/
2123
public StringField asStringField() {
2224
return new StringField(
@@ -30,6 +32,7 @@ public StringField asStringField() {
3032

3133
/**
3234
* Represent the object as a standard {@link AmountField}.
35+
* @return An {@link AmountField} containing a double value.
3336
*/
3437
public AmountField asAmountField() {
3538
Double value;
@@ -51,8 +54,21 @@ else if (rawValue instanceof Double) {
5154
);
5255
}
5356

57+
/**
58+
* Represent the object as a standard {@link BooleanField}.
59+
* @return A {@link BooleanField} containing a boolean value.
60+
*/
61+
public BooleanField asBooleanField() {
62+
Object rawValue = this.get("value");
63+
if (rawValue instanceof Boolean) {
64+
return new BooleanField((Boolean) rawValue, this.getConfidence(), this.getPolygon(), this.getPageId());
65+
}
66+
throw new ClassCastException("Cannot cast " + rawValue + " to Boolean");
67+
}
68+
5469
/**
5570
* Represent the object as a standard {@link DateField}.
71+
* @return A {@link DateField} containing a date value.
5672
*/
5773
public DateField asDateField() {
5874
return new DateField(
@@ -66,6 +82,7 @@ public DateField asDateField() {
6682

6783
/**
6884
* Represent the object as a standard {@link ClassificationField}.
85+
* @return A {@link ClassificationField} containing a string value.
6986
*/
7087
public ClassificationField asClassificationField() {
7188
return new ClassificationField(
@@ -75,13 +92,16 @@ public ClassificationField asClassificationField() {
7592

7693
/**
7794
* Get the polygon, if present.
95+
* @return A {@link Polygon}.
7896
*/
7997
public Polygon getPolygon() {
8098
return this.getAsPolygon("polygon");
8199
}
82100

83101
/**
84102
* Get the specified key as a {@link Polygon} object.
103+
* @param key Key to retrieve the polygon from.
104+
* @return A {@link Polygon}.
85105
*/
86106
public Polygon getAsPolygon(String key) {
87107
if (this.containsKey(key)) {
@@ -92,20 +112,23 @@ public Polygon getAsPolygon(String key) {
92112

93113
/**
94114
* Get the page ID, if present.
115+
* @return A page ID, as an integer.
95116
*/
96117
public Integer getPageId() {
97118
return this.get("page_id") != null ? (Integer) this.get("page_id") : null;
98119
}
99120

100121
/**
101122
* Get the confidence score, if present.
123+
* @return The confidence score, as a double.
102124
*/
103125
public Double getConfidence() {
104126
return this.get("confidence") != null ? (Double) this.get("confidence") : null;
105127
}
106128

107129
/**
108130
* Get the information on whether the date field was extracted.
131+
* @return A boolean representation of the field.
109132
*/
110133
public Boolean getIsComputed(){
111134
return (Boolean) this.get("is_computed");

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.mindee.parsing.generated.GeneratedFeature;
88
import com.mindee.parsing.generated.GeneratedObject;
99
import com.mindee.parsing.standard.AmountField;
10+
import com.mindee.parsing.standard.BooleanField;
1011
import com.mindee.parsing.standard.ClassificationField;
1112
import com.mindee.parsing.standard.DateField;
1213
import com.mindee.parsing.standard.StringField;
@@ -24,12 +25,26 @@ protected AsyncPredictResponse<GeneratedV1> getAsyncPrediction(String name) thro
2425
objectMapper.findAndRegisterModules();
2526

2627
JavaType type = objectMapper.getTypeFactory().constructParametricType(
27-
AsyncPredictResponse.class,
28-
GeneratedV1.class
28+
AsyncPredictResponse.class,
29+
GeneratedV1.class
2930
);
3031
return objectMapper.readValue(
31-
new File("src/test/resources/products/generated/response_v1/" + name + "_international_id_v1.json"),
32-
type
32+
new File("src/test/resources/products/generated/response_v1/" + name + "_international_id_v1.json"),
33+
type
34+
);
35+
}
36+
37+
protected AsyncPredictResponse<GeneratedV1> getUsMailPrediction() throws IOException {
38+
ObjectMapper objectMapper = new ObjectMapper();
39+
objectMapper.findAndRegisterModules();
40+
41+
JavaType type = objectMapper.getTypeFactory().constructParametricType(
42+
AsyncPredictResponse.class,
43+
GeneratedV1.class
44+
);
45+
return objectMapper.readValue(
46+
new File("src/test/resources/products/us_mail/response_v3/complete.json"),
47+
type
3348
);
3449
}
3550

@@ -244,6 +259,14 @@ else if (featureValue.isList()) {
244259
}
245260
}
246261

262+
@Test
263+
void whenCompleteDeserializedWithBoolean_mustHaveValidProperties() throws IOException {
264+
AsyncPredictResponse<GeneratedV1> response = getUsMailPrediction();
265+
GeneratedV1Document docPrediction = response.getDocumentObj().getInference().getPrediction();
266+
Map<String, GeneratedFeature> features = docPrediction.getFields();
267+
Assertions.assertFalse(features.get("is_return_to_sender").asBooleanField().getValue());
268+
}
269+
247270
@Test
248271
void whenAmountDeserialized_mustCastToDouble() {
249272
GeneratedObject intObject = new GeneratedObject();
@@ -260,4 +283,16 @@ void whenAmountDeserialized_mustCastToDouble() {
260283
doubleObject.put("value", "I will fail miserably");
261284
Assertions.assertThrows(ClassCastException.class, badStringObject::asAmountField);
262285
}
286+
287+
@Test
288+
void whenBooleanDeserialized_mustCastToBoolean() {
289+
GeneratedObject boolObject = new GeneratedObject();
290+
boolObject.put("value", true);
291+
BooleanField booleanField = boolObject.asBooleanField();
292+
Assertions.assertTrue(booleanField.getValue());
293+
294+
boolObject.put("value", false);
295+
booleanField = boolObject.asBooleanField();
296+
Assertions.assertFalse(booleanField.getValue());
297+
}
263298
}

0 commit comments

Comments
 (0)