Skip to content

Commit

Permalink
[engine] Fix NPE while collecting variables data containing variable …
Browse files Browse the repository at this point in the history
…with `null` value (#4994)
  • Loading branch information
valfirst authored Apr 24, 2024
1 parent 4872436 commit 3ee9ef3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 6 additions & 5 deletions vividus-engine/src/main/java/org/vividus/variable/Variables.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,12 +21,10 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.reflect.FieldUtils;
Expand Down Expand Up @@ -72,9 +70,12 @@ private Stream<Map<String, Object>> concatedVariables()

public Map<String, Object> getVariables()
{
// Do not use Collectors#toMap, it results in NPE, it's OpenJDK bug:
// https://stackoverflow.com/questions/24630963/nullpointerexception-in-collectors-tomap-with-null-entry-values
// https://bugs.openjdk.java.net/browse/JDK-8148463
return concatedVariables().map(Map::entrySet)
.flatMap(Set::stream)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (k1, k2) -> k2));
.flatMap(Set::stream)
.collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
}

private Optional<Object> getVariable(Map<String, Object> variables, VariableKey variableKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -185,7 +186,14 @@ void shouldReturnMergedVariables()
variables.putStoryVariable(KEY2, STORY);
variables.putStoryVariable(KEY3, STORY);
variables.putStoryVariable(KEY3, STEP);
assertEquals(Map.of(KEY1, SCENARIO, KEY2, STORY, KEY3, STEP), variables.getVariables());
var keyWithNullValue = "key-with-null-value";
variables.putStoryVariable(keyWithNullValue, null);
Map<String, String> expected = new HashMap<>();
expected.put(KEY1, SCENARIO);
expected.put(KEY2, STORY);
expected.put(KEY3, STEP);
expected.put(keyWithNullValue, null);
assertEquals(expected, variables.getVariables());
}

private record Pojo(String name)
Expand Down

0 comments on commit 3ee9ef3

Please # to comment.