This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git
The following commit(s) were added to refs/heads/master by this push:
new 6a5be1e SLING-10894 : Allow null for SlingUriBuilder.setSelectors
6a5be1e is described below
commit 6a5be1ef28becae251f13a5d2830d6a2dffe4a9c
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Thu Oct 28 06:52:29 2021 +0200
SLING-10894 : Allow null for SlingUriBuilder.setSelectors
---
.../builder/SlingHttpServletRequestBuilder.java | 5 ++---
.../builder/impl/SlingHttpServletRequestImpl.java | 10 ++++------
.../org/apache/sling/api/uri/SlingUriBuilder.java | 8 +++++---
.../java/org/apache/sling/api/uri/package-info.java | 2 +-
.../apache/sling/api/uri/SlingUriBuilderTest.java | 20 ++++++++++++++++++++
5 files changed, 32 insertions(+), 13 deletions(-)
diff --git
a/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java
b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java
index bba24e5..8b21cf2 100644
---
a/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java
+++
b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java
@@ -28,9 +28,8 @@ import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;
/**
- * Fluent helper for building a request.
- *
- * Instances of this interface are not thread-safe.
+ * <p>Fluent helper for building a request.</p>
+ * <p><strong>Note:</strong> instances of this interface are not
thread-safe.</p>
* @since 1.0 (Sling API Bundle 2.24.0)
*/
@ProviderType
diff --git
a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
index 598738b..cdfa5c5 100644
---
a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
+++
b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
@@ -307,13 +307,11 @@ public class SlingHttpServletRequestImpl extends
SlingAdaptable
this.checkLocked();
this.locked = true;
- final SlingUriBuilder builder =
SlingUriBuilder.createFrom(this.resource)
+ this.requestPathInfo = SlingUriBuilder.createFrom(this.resource)
.setExtension(this.extension)
- .setSuffix(this.suffix);
- if ( this.selectors != null ) {
- builder.setSelectors(this.selectors);
- }
- this.requestPathInfo = builder.build();
+ .setSuffix(this.suffix)
+ .setSelectors(this.selectors)
+ .build();
this.queryString = this.formatQueryString();
this.pathInfo = this.buildPathInfo();
diff --git a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
index eaa87d2..0a4f603 100644
--- a/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
+++ b/src/main/java/org/apache/sling/api/uri/SlingUriBuilder.java
@@ -427,17 +427,19 @@ public class SlingUriBuilder {
/**
* Set the selectors of the URI.
- *
+ * Passing in {@code null} has the same effect as passing in an empty
array.
* @param selectors the selectors
* @return the builder for method chaining
*/
@NotNull
- public SlingUriBuilder setSelectors(@NotNull String[] selectors) {
+ public SlingUriBuilder setSelectors(@Nullable String[] selectors) {
if (schemeSpecificPart != null || resourcePath == null) {
return this;
}
this.selectors.clear();
- Arrays.stream(selectors).forEach(this.selectors::add);
+ if ( selectors != null ) {
+ Arrays.stream(selectors).forEach(this.selectors::add);
+ }
return this;
}
diff --git a/src/main/java/org/apache/sling/api/uri/package-info.java
b/src/main/java/org/apache/sling/api/uri/package-info.java
index 4a8466a..7e1e38e 100644
--- a/src/main/java/org/apache/sling/api/uri/package-info.java
+++ b/src/main/java/org/apache/sling/api/uri/package-info.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-@Version("1.1.0")
+@Version("1.2.0")
package org.apache.sling.api.uri;
import org.osgi.annotation.versioning.Version;
diff --git a/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java
b/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java
index 5a59045..2fb984f 100644
--- a/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java
+++ b/src/test/java/org/apache/sling/api/uri/SlingUriBuilderTest.java
@@ -18,6 +18,7 @@
*/
package org.apache.sling.api.uri;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -202,4 +203,23 @@ public class SlingUriBuilderTest {
SlingUri testUriEmpty = SlingUriBuilder.create().build();
assertEquals("", testUriEmpty.toString());
}
+
+ @Test
+ public void testSetSelectors() {
+ SlingUri u1 = SlingUriBuilder.parse("/content", null).build();
+ assertEquals(0, u1.getSelectors().length);
+ assertNull(u1.getSelectorString());
+
+ u1 = SlingUriBuilder.parse("/content",
null).setSelectors(null).build();
+ assertEquals(0, u1.getSelectors().length);
+ assertNull(u1.getSelectorString());
+
+ u1 = SlingUriBuilder.parse("/content", null).setSelectors(new String[]
{"a","b"}).build();
+ assertArrayEquals(new String[] {"a", "b"}, u1.getSelectors());
+ assertEquals("a.b", u1.getSelectorString());
+
+ u1 = SlingUriBuilder.parse("/content", null).setSelectors(new String[]
{"a","b"}).setSelectors(null).build();
+ assertEquals(0, u1.getSelectors().length);
+ assertNull(u1.getSelectorString());
+ }
}