From 21f91cd3a712a88da31485d3fe56e756c0ace415 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 16 Mar 2017 10:18:30 +0900 Subject: [PATCH] ElasticChangeIndex: Fix reading ref state fields The ref_states and ref_state_patterns fields are being set only when they exist in the index. In the case where the fields are requested, but are not in the index, this results null values exceptions when attempting to populate the change data. Set the fields when they are requested. If the field does not exist in the index, set it to an empty list. Bug: Issue 5783 Change-Id: I35326bfe175cdd94b43c30f2875e83417be253ba --- .../elasticsearch/ElasticChangeIndex.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/gerrit-elasticsearch/src/main/java/com/google/gerrit/elasticsearch/ElasticChangeIndex.java b/gerrit-elasticsearch/src/main/java/com/google/gerrit/elasticsearch/ElasticChangeIndex.java index 870b88ac80..77cae9bbef 100644 --- a/gerrit-elasticsearch/src/main/java/com/google/gerrit/elasticsearch/ElasticChangeIndex.java +++ b/gerrit-elasticsearch/src/main/java/com/google/gerrit/elasticsearch/ElasticChangeIndex.java @@ -354,20 +354,23 @@ class ElasticChangeIndex extends AbstractElasticIndex cd); decodeUnresolvedCommentCount(source, ChangeField.UNRESOLVED_COMMENT_COUNT.getName(), cd); - if (source.get(ChangeField.REF_STATE.getName()) != null) { - JsonArray refStates = source.get(ChangeField.REF_STATE.getName()).getAsJsonArray(); - cd.setRefStates(Iterables.transform(refStates, e -> Base64.decodeBase64(e.getAsString()))); + if (fields.contains(ChangeField.REF_STATE.getName())) { + cd.setRefStates(getByteArray(source, ChangeField.REF_STATE.getName())); } - if (source.get(ChangeField.REF_STATE_PATTERN.getName()) != null) { - JsonArray refStatePatterns = - source.get(ChangeField.REF_STATE_PATTERN.getName()).getAsJsonArray(); - cd.setRefStatePatterns( - Iterables.transform(refStatePatterns, e -> Base64.decodeBase64(e.getAsString()))); + if (fields.contains(ChangeField.REF_STATE_PATTERN.getName())) { + cd.setRefStatePatterns(getByteArray(source, ChangeField.REF_STATE_PATTERN.getName())); } return cd; } + private Iterable getByteArray(JsonObject source, String name) { + JsonElement element = source.get(name); + return element != null + ? Iterables.transform(element.getAsJsonArray(), e -> Base64.decodeBase64(e.getAsString())) + : Collections.emptyList(); + } + private void decodeSubmitRecords( JsonObject doc, String fieldName, SubmitRuleOptions opts, ChangeData out) { JsonArray records = doc.getAsJsonArray(fieldName);