From 0caac27ce53f5c37b56c46bdb724415d6ab0a632 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 30 Mar 2022 19:38:14 +0200 Subject: [PATCH] Fix serialization of NaN/Infinite/Min/Max values (#223) (#225) Co-authored-by: Sylvain Wallez --- .../co/elastic/clients/json/JsonpUtils.java | 6 +-- .../elasticsearch/json/JsonpUtilsTest.java | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java b/java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java index 7a72e505e..6e53a2aef 100644 --- a/java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java +++ b/java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java @@ -217,8 +217,7 @@ public static String toString(JsonValue value) { } public static void serializeDoubleOrNull(JsonGenerator generator, double value, double defaultValue) { - // Only output null if the default value isn't finite, which cannot be represented as JSON - if (value == defaultValue && !Double.isFinite(defaultValue)) { + if (!Double.isFinite(value)) { generator.writeNull(); } else { generator.write(value); @@ -226,8 +225,7 @@ public static void serializeDoubleOrNull(JsonGenerator generator, double value, } public static void serializeIntOrNull(JsonGenerator generator, int value, int defaultValue) { - // Only output null if the default value isn't finite, which cannot be represented as JSON - if (value == defaultValue && defaultValue == Integer.MAX_VALUE || defaultValue == Integer.MIN_VALUE) { + if (value == defaultValue && (defaultValue == Integer.MAX_VALUE || defaultValue == Integer.MIN_VALUE)) { generator.writeNull(); } else { generator.write(value); diff --git a/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java b/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java index fe69b1e3a..95263b287 100644 --- a/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java +++ b/java-client/src/test/java/co/elastic/clients/elasticsearch/json/JsonpUtilsTest.java @@ -23,12 +23,15 @@ import co.elastic.clients.util.AllowForbiddenApis; import jakarta.json.JsonException; import jakarta.json.spi.JsonProvider; +import jakarta.json.stream.JsonGenerator; import org.junit.Assert; import org.junit.Test; +import java.io.StringWriter; import java.net.URL; import java.util.Collections; import java.util.Enumeration; +import java.util.function.Consumer; public class JsonpUtilsTest extends Assert { @@ -60,4 +63,52 @@ public Enumeration getResources(String name) { Thread.currentThread().setContextClassLoader(savedLoader); } } + + @Test + public void testSerializeDoubleOrNull() { + // ---- Double values + assertEquals("{\"a\":null}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.NaN, Double.NaN))); + assertEquals("{\"a\":1.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 1.0, Double.NaN))); + + assertEquals("{\"a\":null}", + orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY))); + assertEquals("{\"a\":1.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 1.0, Double.POSITIVE_INFINITY))); + + assertEquals("{\"a\":null}", + orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY))); + assertEquals("{\"a\":1.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 1.0, Double.NEGATIVE_INFINITY))); + + assertEquals("{\"a\":null}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, Double.NaN, 0.0))); + + // Serialize defined default values + assertEquals("{\"a\":0.0}", orNullHelper(g -> JsonpUtils.serializeDoubleOrNull(g, 0.0, 0.0))); + + } + + @Test + public void testSerializeIntOrNull() { + assertEquals("{\"a\":null}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, Integer.MAX_VALUE, Integer.MAX_VALUE))); + assertEquals("{\"a\":1}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, 1, Integer.MAX_VALUE))); + assertEquals("{\"a\":1}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, 1, 0))); + + // Integer.MAX_VALUE is valid if not the default value + assertEquals("{\"a\":2147483647}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, Integer.MAX_VALUE, 0))); + assertEquals("{\"a\":2147483647}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, Integer.MAX_VALUE, Integer.MIN_VALUE))); + + // Serialize non infinite default values + assertEquals("{\"a\":0}", orNullHelper(g -> JsonpUtils.serializeIntOrNull(g, 0, 0))); + } + + private static String orNullHelper(Consumer c) { + StringWriter sw = new StringWriter(); + JsonGenerator generator = JsonpUtils.provider().createGenerator(sw); + + generator.writeStartObject(); + generator.writeKey("a"); + c.accept(generator); + generator.writeEnd(); + generator.close(); + + return sw.toString(); + } }