Skip to content

Commit b4a4652

Browse files
authored
CORE-585 fix number format exception (#1026)
fix number format exception
1 parent 31fb4fc commit b4a4652

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

service/src/main/java/org/databiosphere/workspacedataservice/service/DataTypeInferer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@
4747
import org.databiosphere.workspacedataservice.shared.model.RecordAttributes;
4848
import org.databiosphere.workspacedataservice.shared.model.RecordType;
4949
import org.databiosphere.workspacedataservice.shared.model.attributes.JsonAttribute;
50+
import org.slf4j.Logger;
51+
import org.slf4j.LoggerFactory;
5052
import org.springframework.util.CollectionUtils;
5153

5254
public class DataTypeInferer {
5355

56+
private static final Logger logger = LoggerFactory.getLogger(DataTypeInferer.class);
5457
private final ObjectMapper objectMapper;
5558

5659
public DataTypeInferer(ObjectMapper mapper) {
@@ -204,7 +207,13 @@ private JsonNode parseToJsonNode(String val) {
204207
// boolean values
205208
// as booleans - e.g. `TRUE`, `tRUe`, or `true` ---> `true`
206209
return objectMapper.readTree(val.toLowerCase());
207-
} catch (JsonProcessingException e) {
210+
} catch (JsonProcessingException | NumberFormatException e) {
211+
// number format exceptions can occur if the input is scientific notation but too big
212+
// e.g. 9623e89508858559
213+
return null;
214+
} catch (Exception e) {
215+
// Catch-all for any other exceptions that may occur during parsing
216+
logger.info("unexpected exception while parsing json, proceeding as string", e);
208217
return null;
209218
}
210219
}

service/src/test/java/org/databiosphere/workspacedataservice/service/DataTypeInfererTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ void isValidJson() {
9292
assertThat(inferer.tryJsonObject(Boolean.TRUE.toString())).isEmpty();
9393
assertThat(inferer.tryJsonObject("True")).isEmpty();
9494
assertThat(inferer.tryJsonObject("{\"foo\":\"bar\"}")).isPresent();
95+
96+
// scientific notation
97+
assertThat(inferer.tryJsonObject("3e4")).isEmpty();
98+
// scientific notation that's too big
99+
assertThat(inferer.tryJsonObject("9623e89508858559")).isEmpty();
95100
}
96101

97102
@Test

0 commit comments

Comments
 (0)