Skip to content

Commit

Permalink
Ingest: RandomDocumentPicks.randomExistingFieldName doesn't return _v…
Browse files Browse the repository at this point in the history
…ersion (elastic#88183)

When `RandomDocumentPicks.randomExistingFieldName` selects a field, the
caller expects to be able to manipulate that field in any manner.  `_version` has
special validation, must fit in an `long` and may not be `null`.

This change no longer returns `_version` from `randomExistingFieldName` unless it is the only field in the document.

Related to change: elastic#88102
  • Loading branch information
stu-elastic authored Jun 29, 2022
1 parent cd0d5c8 commit 17305fc
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;

public final class RandomDocumentPicks {
Expand Down Expand Up @@ -57,11 +58,12 @@ public static String randomLeafFieldName(Random random) {

/**
* Returns a randomly selected existing field name out of the fields that are contained
* in the document provided as an argument.
* in the document provided as an argument. Does not return the _version field unless it is the only
* field.
*/
public static String randomExistingFieldName(Random random, IngestDocument ingestDocument) {
Map<String, Object> source = new TreeMap<>(ingestDocument.getSourceAndMetadata());
Map.Entry<String, Object> randomEntry = RandomPicks.randomFrom(random, source.entrySet());
Map.Entry<String, Object> randomEntry = getRandomEntry(random, source.entrySet());
String key = randomEntry.getKey();
while (randomEntry.getValue() instanceof Map) {
@SuppressWarnings("unchecked")
Expand All @@ -74,6 +76,20 @@ public static String randomExistingFieldName(Random random, IngestDocument inges
return key;
}

/**
* Return a random entry from a set as long as the entry is not _version. Returns _version only if it is the only entry.
* Since _verison has special validation, tests should test it explicitly rather than randomly
*/
static Map.Entry<String, Object> getRandomEntry(Random random, Set<Map.Entry<String, Object>> entrySet) {
Map.Entry<String, Object> randomEntry = RandomPicks.randomFrom(random, entrySet);
String key = randomEntry.getKey();
while (IngestDocument.Metadata.VERSION.getFieldName().equals(key) && entrySet.size() > 1) {
randomEntry = RandomPicks.randomFrom(random, entrySet);
key = randomEntry.getKey();
}
return randomEntry;
}

/**
* Adds a random non existing field to the provided document and associates it
* with the provided value. The field will be added at a random position within the document,
Expand Down

0 comments on commit 17305fc

Please sign in to comment.