diff --git a/core/src/main/java/io/smallrye/openapi/runtime/io/JacksonJsonIO.java b/core/src/main/java/io/smallrye/openapi/runtime/io/JacksonJsonIO.java index 7969fa9ff..12e72c5ee 100644 --- a/core/src/main/java/io/smallrye/openapi/runtime/io/JacksonJsonIO.java +++ b/core/src/main/java/io/smallrye/openapi/runtime/io/JacksonJsonIO.java @@ -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 @@ -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(); diff --git a/core/src/test/java/io/smallrye/openapi/runtime/io/JacksonJsonIOTest.java b/core/src/test/java/io/smallrye/openapi/runtime/io/JacksonJsonIOTest.java new file mode 100644 index 000000000..69db47552 --- /dev/null +++ b/core/src/test/java/io/smallrye/openapi/runtime/io/JacksonJsonIOTest.java @@ -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 { + + @BeforeEach + void setup() { + super.target = new JacksonJsonIO(new ObjectMapper() + .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)); + } + +} diff --git a/core/src/test/java/io/smallrye/openapi/runtime/io/JakartaJsonIOTest.java b/core/src/test/java/io/smallrye/openapi/runtime/io/JakartaJsonIOTest.java new file mode 100644 index 000000000..1077edb7c --- /dev/null +++ b/core/src/test/java/io/smallrye/openapi/runtime/io/JakartaJsonIOTest.java @@ -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 { + + @BeforeEach + void setup() { + super.target = new JakartaJsonIO(); + } + +} diff --git a/core/src/test/java/io/smallrye/openapi/runtime/io/JsonIOTest.java b/core/src/test/java/io/smallrye/openapi/runtime/io/JsonIOTest.java new file mode 100644 index 000000000..2039d05d0 --- /dev/null +++ b/core/src/test/java/io/smallrye/openapi/runtime/io/JsonIOTest.java @@ -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 { + + protected JsonIO 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); + } +}