From f9453e594ada356445672f6c9a0693bb31441f8f Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Tue, 9 Feb 2016 12:24:08 -0800 Subject: [PATCH] hide null-valued metadata fields from WriteableIngestDocument#toXContent --- .../ingest/WriteableIngestDocument.java | 4 ++- .../ingest/WriteableIngestDocumentTests.java | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/action/ingest/WriteableIngestDocument.java b/core/src/main/java/org/elasticsearch/action/ingest/WriteableIngestDocument.java index 342e4bd3a30..253fa73e8f2 100644 --- a/core/src/main/java/org/elasticsearch/action/ingest/WriteableIngestDocument.java +++ b/core/src/main/java/org/elasticsearch/action/ingest/WriteableIngestDocument.java @@ -69,7 +69,9 @@ final class WriteableIngestDocument implements Writeable metadataMap = ingestDocument.extractMetadata(); for (Map.Entry metadata : metadataMap.entrySet()) { - builder.field(metadata.getKey().getFieldName(), metadata.getValue()); + if (metadata.getValue() != null) { + builder.field(metadata.getKey().getFieldName(), metadata.getValue()); + } } builder.field("_source", ingestDocument.getSourceAndMetadata()); builder.startObject("_ingest"); diff --git a/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java b/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java index 8d3c812f4ce..828c36e7a3e 100644 --- a/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java +++ b/core/src/test/java/org/elasticsearch/action/ingest/WriteableIngestDocumentTests.java @@ -24,14 +24,19 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.ingest.RandomDocumentPicks; import org.elasticsearch.ingest.core.IngestDocument; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.XContentTestUtils; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import static org.elasticsearch.test.XContentTestUtils.convertToMap; +import static org.elasticsearch.test.XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; public class WriteableIngestDocumentTests extends ESTestCase { @@ -111,4 +116,30 @@ public class WriteableIngestDocumentTests extends ESTestCase { WriteableIngestDocument otherWriteableIngestDocument = new WriteableIngestDocument(streamInput); assertThat(otherWriteableIngestDocument, equalTo(writeableIngestDocument)); } + + @SuppressWarnings("unchecked") + public void testToXContent() throws IOException { + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); + WriteableIngestDocument writeableIngestDocument = new WriteableIngestDocument(new IngestDocument(ingestDocument)); + + Map toXContentMap = convertToMap(writeableIngestDocument); + Map toXContentDoc = (Map) toXContentMap.get("doc"); + Map toXContentSource = (Map) toXContentDoc.get("_source"); + Map toXContentIngestMetadata = (Map) toXContentDoc.get("_ingest"); + + Map metadataMap = ingestDocument.extractMetadata(); + for (Map.Entry metadata : metadataMap.entrySet()) { + String fieldName = metadata.getKey().getFieldName(); + if (metadata.getValue() == null) { + assertThat(toXContentDoc.containsKey(fieldName), is(false)); + } else { + assertThat(toXContentDoc.get(fieldName), equalTo(metadata.getValue())); + } + } + + String sourceDiff = differenceBetweenMapsIgnoringArrayOrder(toXContentSource, ingestDocument.getSourceAndMetadata()); + assertThat(sourceDiff, is(nullValue())); + + assertThat(toXContentIngestMetadata, equalTo(ingestDocument.getIngestMetadata())); + } }