This is an automated email from the ASF dual-hosted git repository.

ffang pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.6.x-fixes by this push:
     new 47845aa89d3 CXF-7457: Remove the query in 
ContainerRequestContext.setRequestUri when the new URI has none (#3665)
47845aa89d3 is described below

commit 47845aa89d36ac0acf82c87b59f56ef75ede497f
Author: Freeman(Yue) Fang <[email protected]>
AuthorDate: Mon Nov 2 16:22:16 2026 -0500

    CXF-7457: Remove the query in ContainerRequestContext.setRequestUri when 
the new URI has none (#3665)
    
    doSetRequestUri() only replaced Message.QUERY_STRING when the new request 
URI
    had a query. So a pre-matching filter that removed every query parameter, by
    setting a request URI without a query, left the original query in place, and
    the resource still saw the original query parameters.
    
    Remove Message.QUERY_STRING when the new URI has no query. An empty query
    (as in "/path?") is treated the same as no query.
    
    (cherry picked from commit 795aa4d0c56a0d0ec94f886676cc0ed699e5943d)
    (cherry picked from commit 30d354291167fdef8368a9c1a0aec608caecccd3)
---
 .../jaxrs/impl/ContainerRequestContextImpl.java    |  5 +-
 .../impl/ContainerRequestContextImplTest.java      | 75 ++++++++++++++++++++++
 2 files changed, 79 insertions(+), 1 deletion(-)

diff --git 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
index b3a65bae974..601815328c6 100644
--- 
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
+++ 
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
@@ -137,8 +137,11 @@ public class ContainerRequestContextImpl extends 
AbstractRequestContextImpl
         // changing to toString() seems to have adverse effects downstream. 
Needs more investigation.
         HttpUtils.resetRequestURI(m, requestUri.getRawPath());
         String query = requestUri.getRawQuery();
-        if (query != null) {
+        if (!StringUtils.isEmpty(query)) {
             m.put(Message.QUERY_STRING, query);
+        } else {
+            // the new URI has no or an empty query, so the query of the old 
one must not stay
+            m.remove(Message.QUERY_STRING);
         }
     }
 
diff --git 
a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImplTest.java
 
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImplTest.java
new file mode 100644
index 00000000000..5c944b13a72
--- /dev/null
+++ 
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImplTest.java
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.jaxrs.impl;
+
+import java.net.URI;
+
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class ContainerRequestContextImplTest {
+
+    @Test
+    public void testSetRequestUriReplacesQuery() {
+        Message m = createMessage("/echo", "a=1&b=2");
+        new ContainerRequestContextImpl(m, true, 
false).setRequestUri(URI.create("/echo?a=3"));
+
+        MultivaluedMap<String, String> query = new 
UriInfoImpl(m).getQueryParameters();
+        assertEquals(1, query.size());
+        assertEquals("3", query.getFirst("a"));
+    }
+
+    @Test
+    public void testSetRequestUriWithoutQueryRemovesQuery() {
+        Message m = createMessage("/echo", "a=1&b=2");
+        new ContainerRequestContextImpl(m, true, 
false).setRequestUri(URI.create("/echo"));
+
+        assertNull(m.get(Message.QUERY_STRING));
+        assertTrue(new UriInfoImpl(m).getQueryParameters().isEmpty());
+    }
+
+    @Test
+    public void testSetRequestUriWithEmptyQueryRemovesQuery() {
+        Message m = createMessage("/echo", "a=1&b=2");
+        new ContainerRequestContextImpl(m, true, 
false).setRequestUri(URI.create("/echo?"));
+
+        assertNull(m.get(Message.QUERY_STRING));
+        assertTrue(new UriInfoImpl(m).getQueryParameters().isEmpty());
+    }
+
+    private static Message createMessage(String requestUri, String query) {
+        Message m = new MessageImpl();
+        Exchange e = new ExchangeImpl();
+        m.setExchange(e);
+        e.setInMessage(m);
+        m.put(Message.REQUEST_URI, requestUri);
+        m.put(Message.QUERY_STRING, query);
+        return m;
+    }
+}

Reply via email to