Browse Source

Fix DoubleBounds null serialization (#59475)

Fixes null serialization in DoubleBounds
Igor Motov 5 years ago
parent
commit
8feb12832e

+ 12 - 4
server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DoubleBounds.java

@@ -24,8 +24,10 @@ import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
 import org.elasticsearch.common.io.stream.Writeable;
 import org.elasticsearch.common.xcontent.InstantiatingObjectParser;
+import org.elasticsearch.common.xcontent.ObjectParser;
 import org.elasticsearch.common.xcontent.ToXContentFragment;
 import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.common.xcontent.XContentParser;
 
 import java.io.IOException;
 import java.util.Objects;
@@ -47,8 +49,10 @@ public class DoubleBounds implements ToXContentFragment, Writeable {
     static {
         InstantiatingObjectParser.Builder<DoubleBounds, Void> parser =
             InstantiatingObjectParser.builder("double_bounds", false, DoubleBounds.class);
-        parser.declareDouble(optionalConstructorArg(), MIN_FIELD);
-        parser.declareDouble(optionalConstructorArg(), MAX_FIELD);
+        parser.declareField(optionalConstructorArg(), p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.doubleValue(),
+            MIN_FIELD, ObjectParser.ValueType.DOUBLE_OR_NULL);
+        parser.declareField(optionalConstructorArg(), p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.doubleValue(),
+            MAX_FIELD, ObjectParser.ValueType.DOUBLE_OR_NULL);
         PARSER = parser.build();
     }
 
@@ -86,8 +90,12 @@ public class DoubleBounds implements ToXContentFragment, Writeable {
 
     @Override
     public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
-        builder.field(MIN_FIELD.getPreferredName(), min);
-        builder.field(MAX_FIELD.getPreferredName(), max);
+        if (min != null) {
+            builder.field(MIN_FIELD.getPreferredName(), min);
+        }
+        if (max != null) {
+            builder.field(MAX_FIELD.getPreferredName(), max);
+        }
         return builder;
     }
 

+ 0 - 1
server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DoubleBoundsTests.java

@@ -79,7 +79,6 @@ public class DoubleBoundsTests extends ESTestCase {
         assertEquals(origBytes, readBytes);
     }
 
-    @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/59475")
     public void testXContentRoundTrip() throws Exception {
         DoubleBounds orig = randomBounds();