Make Either.stream() and iterator() empty for a left value - #17714
Conversation
match(url, prefix) strips the prefix with matchAgainst.replaceFirst(prefix, ""), which interprets the prefix as a regular expression. A prefix that contains regex metacharacters (for example an unmatched ( ) throws PatternSyntaxException, or silently strips the wrong text. Quote the prefix with Pattern.quote so it is matched literally.
Either.stream() returned Stream.of(right()) and iterator() returned Collections.singleton(right()).iterator() unconditionally. For a left-valued Either, right() is null, so stream() yielded a single null element (and would NPE in downstream operations) and iterator() yielded one null element instead of being empty. Return an empty stream/iterator when the Either holds a left value.
PR Summary by QodoFix Either left-side stream/iterator and treat UrlTemplate prefixes literally Description
Diagram
High-Level Assessment
Files changed (4)
|
Code Review by Qodo
Context used✅ Compliance rules (platform):
18 rules 1.
|
|
Code review by qodo was updated up to the latest commit 6e84dee |
diemol
left a comment
There was a problem hiding this comment.
Thanks for the fix. The Either changes are correct, and the tests are well-written.
But I believe these tests are not running in our CI, I could not find a BUILD.bazel that has them configured. Would it be possible for you to add a new BUILD.bazel or to add the tests to the BUILD.bazel that lives in the parent directory?
java/test/org/openqa/selenium/internal had no BUILD.bazel, so EitherTest (and the existing MapsTest/MultimapTest/SetsTest) were never run in CI. Add a small java_test_suite that globs the *Test.java files in this package.
|
Thanks @diemol! Added |
|
Code review by qodo was updated up to the latest commit c93811d |
|
Code review by qodo was updated up to the latest commit f6410d8 |
… compile level Stream.toList() is Java 16+; the test build target compiles below that, so it failed to build once wired into Bazel. Use collect(Collectors.toList()) instead.
|
Code review by qodo was updated up to the latest commit 266131d |
|
Code review by qodo was updated up to the latest commit fb8ed8b |
Problem
Either.stream()returnedStream.of(right())andEither.iterator()returnedCollections.singleton(right()).iterator()unconditionally, regardless of which side theEitherholds. For a left-valuedEither,right()isnull, so:stream()yields a singlenullelement (and throwsNullPointerExceptionin downstream operations such astoList()on some pipelines), anditerator()yields onenullelement instead of being empty.A left-valued
Eitherhas no right value, so both should be empty — consistent with treatingEitheras a (possibly empty) collection of the right value.Fix
Return an empty stream / iterator when the
Eitherholds a left value.Test
Adds
EitherTestcoveringstream()anditerator()for both left and right values. The left-value cases fail ontrunk(non-empty, containingnull) and pass with this change.