You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2026/08/23 18:55:12 UTC

(struts) branch WW-5688-rest-root-namespace-6x created (now ba7773e0f)

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

lukaszlenart pushed a change to branch WW-5688-rest-root-namespace-6x
in repository https://gitbox.apache.org/repos/asf/struts.git


      at ba7773e0f WW-5688 fix(rest): resolve id-bearing URIs into the root namespace when declared

This branch includes the following new commits:

     new ba7773e0f WW-5688 fix(rest): resolve id-bearing URIs into the root namespace when declared

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(struts) 01/01: WW-5688 fix(rest): resolve id-bearing URIs into the root namespace when declared

Posted by lu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch WW-5688-rest-root-namespace-6x
in repository https://gitbox.apache.org/repos/asf/struts.git

commit ba7773e0f0222fdba6eacabb9a069eef697f88c0
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Sun Aug 23 20:48:04 2026 +0200

    WW-5688 fix(rest): resolve id-bearing URIs into the root namespace when declared
    
    RestActionMapper mapped a URI carrying an id into the default namespace while
    mapping the same action without an id into "/". Since the configuration only
    fails over from "/" to "" and never the other way round, an action declared in
    a package with namespace="/" resolved for index but 404'd for show, update and
    destroy.
    
    DefaultActionMapper already handles this: WW-2461 added a rootAvailable check
    in June 2008, three months before WW-2820 reported the REST symptom, but the
    fix was never ported to the mapper the REST plugin had forked earlier. Port it,
    keeping the ordering that computes the action name while the namespace is still
    empty, since the name is relative to it.
    
    The promotion only fires when a package explicitly declares namespace="/" and
    nothing more specific matched. Convention derives "" or "/sub" and never "/",
    so applications that do not opt in are unaffected, and because a "/" lookup
    already falls back to "", the set of resolvable actions is a superset of the
    previous one.
    
    Co-Authored-By: Claude Opus 5 <no...@anthropic.com>
---
 .../org/apache/struts2/rest/RestActionMapper.java  | 11 +++
 .../rest/RestActionMapperRootNamespaceTest.java    | 85 ++++++++++++++++++++++
 .../apache/struts2/rest/RestActionMapperTest.java  | 17 +++++
 plugins/rest/src/test/resources/ww-5688.xml        | 31 ++++++++
 4 files changed, 144 insertions(+)

diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionMapper.java b/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionMapper.java
index eec8bf37e..1678fd9a6 100644
--- a/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionMapper.java
+++ b/plugins/rest/src/main/java/org/apache/struts2/rest/RestActionMapper.java
@@ -351,6 +351,7 @@ public class RestActionMapper extends DefaultActionMapper {
             Configuration config = configManager.getConfiguration();
             String prefix = uri.substring(0, lastSlash);
             namespace = "";
+            boolean rootAvailable = false;
             // Find the longest matching namespace, defaulting to the default
             for (Object o : config.getPackageConfigs().values()) {
                 String ns = ((PackageConfig) o).getNamespace();
@@ -359,9 +360,19 @@ public class RestActionMapper extends DefaultActionMapper {
                         namespace = ns;
                     }
                 }
+                if ("/".equals(ns)) {
+                    rootAvailable = true;
+                }
             }
 
+            // must be read before the root namespace is selected below, as it is relative to ""
             name = uri.substring(namespace.length() + 1);
+
+            // WW-5688, WW-2461: still none found, use the root namespace if it is declared, so that
+            // an id-bearing uri lands in the same namespace as the one without an id
+            if (rootAvailable && namespace.isEmpty()) {
+                namespace = "/";
+            }
         }
 
         mapping.setNamespace(cleanupNamespaceName(namespace));
diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionMapperRootNamespaceTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionMapperRootNamespaceTest.java
new file mode 100644
index 000000000..b722b4d6c
--- /dev/null
+++ b/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionMapperRootNamespaceTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.struts2.rest;
+
+import com.opensymphony.xwork2.XWorkTestCase;
+import org.apache.struts2.config.StrutsXmlConfigurationProvider;
+import org.apache.struts2.dispatcher.mapper.ActionMapping;
+import org.springframework.mock.web.MockHttpServletRequest;
+
+/**
+ * WW-5688: an action declared in the root namespace has to stay reachable whether or not the
+ * request URI carries an id, so that {@code index} and {@code show} resolve to the same action.
+ *
+ * <p>These assertions go all the way to the {@code ActionConfig} rather than stopping at the
+ * mapping, because the reported symptom is a 404 - the mapper handing back a namespace that
+ * the configuration cannot resolve.</p>
+ */
+public class RestActionMapperRootNamespaceTest extends XWorkTestCase {
+
+    private RestActionMapper mapper;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        loadConfigurationProviders(new StrutsXmlConfigurationProvider("ww-5688.xml"));
+        mapper = new RestActionMapper();
+    }
+
+    private ActionMapping map(String servletPath, String httpMethod) {
+        MockHttpServletRequest request = new MockHttpServletRequest();
+        request.setContextPath("/myapp");
+        request.setMethod(httpMethod);
+        request.setRequestURI("/myapp" + servletPath);
+        request.setServletPath(servletPath);
+        return mapper.getMapping(request, configurationManager);
+    }
+
+    private void assertResolves(String servletPath, String httpMethod, String expectedMethod) {
+        ActionMapping mapping = map(servletPath, httpMethod);
+        assertNotNull(httpMethod + " " + servletPath + " produced no mapping", mapping);
+        assertEquals("dog", mapping.getName());
+        assertEquals(expectedMethod, mapping.getMethod());
+        assertNotNull(httpMethod + " " + servletPath + " must resolve to the action declared in the root namespace,"
+                        + " but namespace '" + mapping.getNamespace() + "' does not hold it",
+                configurationManager.getConfiguration().getRuntimeConfiguration()
+                        .getActionConfig(mapping.getNamespace(), mapping.getName()));
+    }
+
+    public void testIndexResolvesInRootNamespace() {
+        assertResolves("/dog", "GET", "index");
+    }
+
+    public void testShowResolvesInRootNamespace() {
+        assertResolves("/dog/1", "GET", "show");
+    }
+
+    public void testUpdateResolvesInRootNamespace() {
+        assertResolves("/dog/1", "PUT", "update");
+    }
+
+    public void testDestroyResolvesInRootNamespace() {
+        assertResolves("/dog/1", "DELETE", "destroy");
+    }
+
+    public void testIdIsStillExtracted() {
+        ActionMapping mapping = map("/dog/1", "GET");
+        assertEquals("1", ((String[]) mapping.getParams().get("id"))[0]);
+    }
+}
diff --git a/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java b/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java
index 12e04b896..dd5262a81 100644
--- a/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java
+++ b/plugins/rest/src/test/java/org/apache/struts2/rest/RestActionMapperTest.java
@@ -261,6 +261,23 @@ public class RestActionMapperTest extends TestCase {
         tryUri("/my/foo/23;edit", "/my", "foo/23;edit");
     }
 
+    /**
+     * WW-5688: when a package declares the root namespace, a uri that matches no more specific
+     * namespace belongs to that root rather than to the default namespace, so that "/foo" and
+     * "/foo/23" agree on where the action lives. Without a root package the default namespace
+     * still wins - see {@link #testParseNameAndNamespace()}.
+     */
+    public void testParseNameAndNamespaceWithRootPackage() {
+        config.addPackageConfig("root", new PackageConfig.Builder("root").namespace("/").build());
+
+        tryUri("/foo/23", "/", "foo/23");
+        tryUri("/foo/", "/", "foo/");
+        tryUri("/", "/", "");
+
+        // a longer declared namespace still outranks the root
+        tryUri("/my/foo/23", "/my", "foo/23");
+    }
+
     public void testShouldAllowExclamation() throws Exception {
         req.setRequestURI("/myapp/animals/dog/fido!edit");
         req.setServletPath("/animals/dog/fido!edit");
diff --git a/plugins/rest/src/test/resources/ww-5688.xml b/plugins/rest/src/test/resources/ww-5688.xml
new file mode 100644
index 000000000..1b553347c
--- /dev/null
+++ b/plugins/rest/src/test/resources/ww-5688.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+/*
+ * 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.
+ */
+-->
+<!DOCTYPE struts PUBLIC
+        "-//Apache Software Foundation//DTD Struts Configuration 6.0//EN"
+        "https://struts.apache.org/dtds/struts-6.0.dtd">
+
+<!-- WW-5688: the only declared package sits at the root namespace -->
+<struts>
+    <package name="ww-5688-root" namespace="/">
+        <action name="dog" class="org.apache.struts2.ActionSupport"/>
+    </package>
+</struts>