Skip to content

Commit

Permalink
fix: check value type before Jackson getJsonBigDecimal (#2057)
Browse files Browse the repository at this point in the history
Also avoid re-parsing BigDecimal/BigInteger values in fromJson

Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar authored Nov 7, 2024
1 parent 42db59d commit c176d39
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public Boolean getJsonBoolean(ObjectNode object, String key) {
@Override
public BigDecimal getJsonBigDecimal(ObjectNode object, String key) {
JsonNode value = object.get(key);
return value != null ? new BigDecimal(value.asText()) : null;
return value != null && value.isNumber() ? value.decimalValue() : null;
}

@Override
Expand Down Expand Up @@ -300,10 +300,10 @@ public Object fromJson(JsonNode value) {
return null;
}
if (value.isBigDecimal()) {
return new BigDecimal(value.asText());
return value.decimalValue();
}
if (value.isBigInteger()) {
return new BigInteger(value.asText());
return value.bigIntegerValue();
}
if (value.isBoolean()) {
return value.asBoolean();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.smallrye.openapi.runtime.io;

import org.junit.jupiter.api.BeforeEach;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

class JacksonJsonIOTest extends JsonIOTest<JsonNode, ArrayNode, ObjectNode, ArrayNode, ObjectNode> {

@BeforeEach
void setup() {
super.target = new JacksonJsonIO(new ObjectMapper()
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.smallrye.openapi.runtime.io;

import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonValue;

import org.junit.jupiter.api.BeforeEach;

class JakartaJsonIOTest extends JsonIOTest<JsonValue, JsonArray, JsonObject, JsonArrayBuilder, JsonObjectBuilder> {

@BeforeEach
void setup() {
super.target = new JakartaJsonIO();
}

}
37 changes: 37 additions & 0 deletions core/src/test/java/io/smallrye/openapi/runtime/io/JsonIOTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.smallrye.openapi.runtime.io;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.math.BigDecimal;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

abstract class JsonIOTest<V, A extends V, O extends V, AB, OB> {

protected JsonIO<V, A, O, AB, OB> target;

@ParameterizedTest
@CsvSource({
"{ \"key\": 3.1415 }, 3.1415",
"{ \"key\": \"3.1415\" }, ",
"{ \"key\": [ 3.1415 ] }, ",
})
void testGetJsonBigDecimal(String input, BigDecimal expected) {
@SuppressWarnings("unchecked")
O value = (O) target.fromString(input, Format.JSON);
assertEquals(expected, target.getJsonBigDecimal(value, "key"));
}

@ParameterizedTest
@CsvSource({
"{ \"key\": 3.141592653589793238462643383279 }, 3.141592653589793238462643383279",
})
void testFromJsonBigDecimal(String input, String expected) {
@SuppressWarnings("unchecked")
O value = (O) target.fromString(input, Format.JSON);
V jsonValue = target.getValue(value, "key");
Object javaValue = target.fromJson(jsonValue);
assertEquals(new BigDecimal(expected), javaValue);
}
}

0 comments on commit c176d39

Please sign in to comment.