Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PG intersects expression to avoid NPE #1693

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public Intersects(TargetingCategory category, List<T> values) {

@Override
public boolean matches(RequestContext context) {
return !Collections.disjoint(values, lookupActualValues(context));
final List<T> actualValues = lookupActualValues(context);
return actualValues != null && !Collections.disjoint(values, actualValues);
}

protected abstract List<T> lookupActualValues(RequestContext context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public IntersectsStrings(TargetingCategory category, List<String> values) {

@Override
public List<String> lookupActualValues(RequestContext context) {
return toLowerCase(context.lookupStrings(category));
final List<String> values = context.lookupStrings(category);
return values != null ? toLowerCase(values) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not return Collections.emptyList() here and leave previous implementation of matches?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NULL is more optimized. If look forward in org.prebid.server.deals.targeting.interpret.Intersects#matches method - the call of Collections.disjoint(..) is not required. Another though - lookupActualValues(..) method returns values in request, so empty list can real value.

}

private static List<String> toLowerCase(List<String> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@ public void matchesShouldReturnFalseWhenActualValueIsMissing() {
// when and then
assertThat(expression.matches(context)).isFalse();
}

@Test
public void matchesShouldReturnFalseWhenActualValueIsNotDefined() {
// given
willReturn(null).given(context).lookupIntegers(any());

// when and then
assertThat(expression.matches(context)).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ public void matchesShouldReturnFalseWhenActualValueIsMissing() {
// when and then
assertThat(expression.matches(context)).isFalse();
}

@Test
public void matchesShouldReturnFalseWhenActualValueIsNotDefined() {
// given
willReturn(null).given(context).lookupSizes(any());

// when and then
assertThat(expression.matches(context)).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@ public void matchesShouldReturnFalseWhenActualValueIsMissing() {
// when and then
assertThat(expression.matches(context)).isFalse();
}

@Test
public void matchesShouldReturnFalseWhenActualValueIsNotDefined() {
// given
willReturn(null).given(context).lookupStrings(any());

// when and then
assertThat(expression.matches(context)).isFalse();
}
}