Skip to content

Commit b39a335

Browse files
authored
🐛 fix for server not properly formatting floats (#215)
1 parent cd74401 commit b39a335

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,19 @@ public StringField asStringField() {
3232
* Represent the object as a standard {@link AmountField}.
3333
*/
3434
public AmountField asAmountField() {
35+
Double value;
36+
Object rawValue = this.get("value");
37+
if (rawValue instanceof Integer) {
38+
value = ((Integer) rawValue).doubleValue();
39+
}
40+
else if (rawValue instanceof Double) {
41+
value = (Double) rawValue;
42+
}
43+
else {
44+
throw new ClassCastException("Cannot cast " + rawValue + " to Double");
45+
}
3546
return new AmountField(
36-
(Double) this.get("value"),
47+
value,
3748
this.getConfidence(),
3849
this.getPolygon(),
3950
this.getPageId()

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

+17
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,21 @@ else if (featureValue.isList()) {
243243
}
244244
}
245245
}
246+
247+
@Test
248+
void whenAmountDeserialized_mustCastToDouble() {
249+
GeneratedObject intObject = new GeneratedObject();
250+
intObject.put("value", 5);
251+
AmountField fieldFromInt = intObject.asAmountField();
252+
Assertions.assertEquals(5.0, fieldFromInt.getValue());
253+
254+
GeneratedObject doubleObject = new GeneratedObject();
255+
doubleObject.put("value", 5.0);
256+
AmountField fieldFromDouble = doubleObject.asAmountField();
257+
Assertions.assertEquals(5.0, fieldFromDouble.getValue());
258+
259+
GeneratedObject badStringObject = new GeneratedObject();
260+
doubleObject.put("value", "I will fail miserably");
261+
Assertions.assertThrows(ClassCastException.class, badStringObject::asAmountField);
262+
}
246263
}

0 commit comments

Comments
 (0)