-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check value type before Jackson getJsonBigDecimal (#2057)
Also avoid re-parsing BigDecimal/BigInteger values in fromJson Signed-off-by: Michael Edgar <michael@xlate.io>
- Loading branch information
Showing
4 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
core/src/test/java/io/smallrye/openapi/runtime/io/JacksonJsonIOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
core/src/test/java/io/smallrye/openapi/runtime/io/JakartaJsonIOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
core/src/test/java/io/smallrye/openapi/runtime/io/JsonIOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |