You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@struts.apache.org by "lukaszlenart (via GitHub)" <gi...@apache.org> on 2026/10/23 05:42:25 UTC

[PR] WW-5790 fix(rest): copy only what the body handler changed in the two-phase copy [struts]

lukaszlenart opened a new pull request, #2051:
URL: https://github.com/apache/struts/pull/2051

   Fixes [WW-5790](https://issues.apache.org/jira/browse/WW-5790)
   
   ## What was wrong
   
   With `struts.parameters.requireAnnotations=true`, a content-type handler that does not implement `AuthorizationAwareContentTypeHandler` goes through the two-phase copy in `ContentTypeInterceptor`: a fresh instance is built with the no-arg constructor, the handler fills it, and every non-null authorized property is copied onto the real target. The copy could not tell a value the body supplied from a value the constructor assigned, so every property the body omitted but the constructor initialised was written over what `params` had bound or `prepare()` had loaded. Scalars, constructor-initialised collections (an empty list replaced a populated one) and constructor-initialised nested beans (the recursion reset their leaves) all suffered. Follow-up to #2050, which covered the handlers that never read the body; this one covers `XStreamHandler` and application-provided handlers.
   
   ## Change
   
   - `applyTwoPhaseDeserialize` builds a second, untouched fresh instance as a baseline.
   - `copyAuthorizedProperties` takes the baseline and skips a property whose filled value `Objects.deepEquals` the baseline value. The baseline is threaded through the nested-bean recursion: the baseline's own nested value, else a fresh instance of the nested class, else null, which keeps today's copy-non-null rule for that subtree. Collection, map and array elements are new objects already, so their recursion passes no baseline.
   - The authorization check runs before any copy, unchanged. The copy now writes a subset of what it wrote before; nothing new becomes bindable.
   - No public API or configuration change.
   
   ## Documented limits
   
   - A body value equal to the constructor default, sent while the target holds a different value, is not applied. Mirrors the null ambiguity accepted in WW-5624.
   - The target's constructor runs twice per request on this path.
   - Non-deterministic defaults (`UUID.randomUUID()`, `Instant.now()`) always differ from the baseline and are still copied, as before.
   
   Behaviour changes only when all three hold: `requireAnnotations=true`, a non-aware handler, and a property with a non-null constructor default. The default handlers are unaffected.
   
   ## Tests
   
   Eleven tests in `ContentTypeInterceptorIntegrationTest` on a ModelDriven fixture driven by a scripted non-aware handler: omitted scalar default kept, set scalar applied, omitted collection default kept, omitted nested default kept, partially set nested bean applies only the set leaf, nested type without a no-arg constructor still applies the set leaf, the documented limits pinned by name (body value equal to the default, primitive zero value, empty collection), a nested subtype whose own leaf is applied, and a nested bean whose id-only `equals` does not hide a changed leaf. Five failed before the change with the constructor value in place of the pre-existing one; the subtype and partial-equals tests came out of the review and failed before their fix.
   
   ## Verification
   
   - `plugins/rest` suite with core compiled from the branch: 237 tests, 0 failures.
   - Security review of the diff: no findings. The copy writes a subset of what it wrote before; the authorization check in front of every copy is unchanged.
   - Docs: apache/struts-site#362.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@struts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WW-5790 fix(rest): copy only what the body handler changed in the two-phase copy [struts]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #2051:
URL: https://github.com/apache/struts/pull/2051#issuecomment-6360362656

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_struts&pullRequest=2051) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_struts&pullRequest=2051&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_struts&pullRequest=2051&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_struts&pullRequest=2051&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [89.3% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_struts&pullRequest=2051&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_struts&pullRequest=2051&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_struts&pullRequest=2051)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@struts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WW-5790 fix(rest): copy only what the body handler changed in the two-phase copy [struts]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2051:
URL: https://github.com/apache/struts/pull/2051#discussion_r4356737137


##########
plugins/rest/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java:
##########
@@ -236,15 +241,21 @@ private void copyAuthorizedProperties(
             }
 
             Object sourceValue = readMethod.invoke(source);
+            Object baselineValue = baseline == null ? null : readMethod.invoke(baseline);

Review Comment:
   `baselineValue` is computed (via reflective getter invocation) even when `sourceValue` is null and the method immediately `continue`s. You can avoid that extra reflective call by moving the `baselineValue` computation to after the `sourceValue == null` check (and only computing it when it’s actually needed for comparisons / nested-baseline derivation).



##########
plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeInterceptorIntegrationTest.java:
##########
@@ -421,10 +512,124 @@ public static class MergedPropertyAction extends ActionSupport {
      */
     public static class DefaultedModel {
         private String status = "new";
+        private String name;
+        private List<String> tags = new ArrayList<>();
+        private DefaultedAddress address = new DefaultedAddress();
+        private PinnedAddress pinned;
+        private IdEqualAddress billing = new IdEqualAddress();
+        private boolean enabled;
 
         public String getStatus() { return status; }
 
+        public boolean isEnabled() { return enabled; }
+
+        public void setEnabled(boolean enabled) { this.enabled = enabled; }
+
+        public IdEqualAddress getBilling() { return billing; }
+
+        public void setBilling(IdEqualAddress billing) { this.billing = billing; }
+
         public void setStatus(String status) { this.status = status; }
+
+        public String getName() { return name; }
+
+        public void setName(String name) { this.name = name; }
+
+        public List<String> getTags() { return tags; }
+
+        public void setTags(List<String> tags) { this.tags = tags; }
+
+        public DefaultedAddress getAddress() { return address; }
+
+        public void setAddress(DefaultedAddress address) { this.address = address; }
+
+        public PinnedAddress getPinned() { return pinned; }
+
+        public void setPinned(PinnedAddress pinned) { this.pinned = pinned; }
+    }
+
+    public static class DefaultedAddress {
+        private String country = "PL";
+        private String city;
+
+        public String getCountry() { return country; }
+
+        public void setCountry(String country) { this.country = country; }
+
+        public String getCity() { return city; }
+
+        public void setCity(String city) { this.city = city; }
+    }
+
+    public static class HomeAddress extends DefaultedAddress {
+        private String floor;
+
+        public String getFloor() { return floor; }
+
+        public void setFloor(String floor) { this.floor = floor; }
+    }
+
+    /**
+     * WW-5790: equals by id only, the shape of a JPA entity; a leaf-blind equals must not hide a changed leaf.
+     */
+    public static class IdEqualAddress {
+        private Long id;
+        private String city;
+
+        public Long getId() { return id; }
+
+        public void setId(Long id) { this.id = id; }
+
+        public String getCity() { return city; }
+
+        public void setCity(String city) { this.city = city; }
+
+        @Override
+        public boolean equals(Object other) {
+            return other instanceof IdEqualAddress that && java.util.Objects.equals(id, that.id);
+        }
+
+        @Override
+        public int hashCode() { return java.util.Objects.hashCode(id); }
+    }
+
+    /**
+     * WW-5790: no no-arg constructor, so the copy cannot build a baseline for this subtree.
+     */
+    public static class PinnedAddress {
+        private String country = "PL";
+        private String city;
+
+        public PinnedAddress(String city) { this.city = city; }
+
+        public String getCountry() { return country; }
+
+        public void setCountry(String country) { this.country = country; }
+
+        public String getCity() { return city; }
+
+        public void setCity(String city) { this.city = city; }
+    }
+
+    /**
+     * WW-5790: a handler without the authorization-aware marker; what it sets stands in for the body.
+     */
+    public static class ScriptedHandler implements ContentTypeHandler {
+        private final Consumer<DefaultedModel> body;
+
+        ScriptedHandler(Consumer<DefaultedModel> body) { this.body = body; }
+
+        @Override
+        public void toObject(ActionInvocation invocation, Reader in, Object target) { body.accept((DefaultedModel) target); }
+
+        @Override
+        public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) { return null; }

Review Comment:
   Returning null from `ContentTypeHandler#fromObject` can cause fragile tests (and potential NPEs) if the execution path ever calls serialization/result-code logic. Prefer returning `resultCode` (pass-through) or a concrete result code (e.g., `Action.SUCCESS`) to keep the handler behavior well-defined even when response generation is exercised.



##########
docs/superpowers/plans/2026-10-23-WW-5790-two-phase-copy-baseline.md:
##########
@@ -0,0 +1,235 @@
+# WW-5790 Two-Phase Copy Baseline Implementation Plan

Review Comment:
   This PR introduces a large internal implementation plan document under `docs/superpowers/plans/`. The PR description focuses on the functional fix + tests + an external struts-site docs PR, but doesn’t mention adding this planning artifact. If this repository doesn’t intend to version internal agent execution plans, consider dropping this file from the PR (or relocating it to the appropriate contributor/process location if such docs are expected here).



##########
plugins/rest/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java:
##########
@@ -236,15 +241,21 @@ private void copyAuthorizedProperties(
             }
 
             Object sourceValue = readMethod.invoke(source);
+            Object baselineValue = baseline == null ? null : readMethod.invoke(baseline);
+            // A null, or a leaf or collection equal to the baseline's constructor default, reads as "not in
+            // the body": copying it would overwrite what params or prepare() put on the target. The cost is
+            // that an explicit null, a primitive at its zero value, an empty collection, or any body value
+            // equal to the constructor default is not applied either. A nested bean is compared leaf by leaf
+            // below, never through its own equals.
             if (sourceValue == null) {
-                // Intentionally skip null values: in two-phase deserialization, properties NOT present in the
-                // request body will be null in the fresh instance. Copying null would clear pre-initialized
-                // fields on the target. This is the safer default — an explicit JSON null and a missing field
-                // are indistinguishable after deserialization into a fresh POJO.
+                continue;
+            }

Review Comment:
   `baselineValue` is computed (via reflective getter invocation) even when `sourceValue` is null and the method immediately `continue`s. You can avoid that extra reflective call by moving the `baselineValue` computation to after the `sourceValue == null` check (and only computing it when it’s actually needed for comparisons / nested-baseline derivation).



##########
plugins/rest/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java:
##########
@@ -236,15 +241,21 @@ private void copyAuthorizedProperties(
             }
 
             Object sourceValue = readMethod.invoke(source);
+            Object baselineValue = baseline == null ? null : readMethod.invoke(baseline);
+            // A null, or a leaf or collection equal to the baseline's constructor default, reads as "not in
+            // the body": copying it would overwrite what params or prepare() put on the target. The cost is
+            // that an explicit null, a primitive at its zero value, an empty collection, or any body value
+            // equal to the constructor default is not applied either. A nested bean is compared leaf by leaf
+            // below, never through its own equals.

Review Comment:
   The skip-rule comment says \"leaf or collection\" but the `Objects.deepEquals` check is also applied to maps and arrays (both are non-nested types and will flow through this branch). To keep the comment accurate, please update it to explicitly include maps/arrays (or refer more generally to \"non-nested values\", including collections/maps/arrays).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@struts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WW-5790 fix(rest): copy only what the body handler changed in the two-phase copy [struts]

Posted by "lukaszlenart (via GitHub)" <gi...@apache.org>.
lukaszlenart commented on code in PR #2051:
URL: https://github.com/apache/struts/pull/2051#discussion_r4356758061


##########
docs/superpowers/plans/2026-10-23-WW-5790-two-phase-copy-baseline.md:
##########
@@ -0,0 +1,235 @@
+# WW-5790 Two-Phase Copy Baseline Implementation Plan

Review Comment:
   Plan files are versioned here on purpose: docs/superpowers/plans/ holds the plan of every ticket that went through this workflow, committed in the same PR (for example #2042 for WW-3576 and #2009 for WW-5770). Keeping it.



##########
plugins/rest/src/test/java/org/apache/struts2/rest/ContentTypeInterceptorIntegrationTest.java:
##########
@@ -421,10 +512,124 @@ public static class MergedPropertyAction extends ActionSupport {
      */
     public static class DefaultedModel {
         private String status = "new";
+        private String name;
+        private List<String> tags = new ArrayList<>();
+        private DefaultedAddress address = new DefaultedAddress();
+        private PinnedAddress pinned;
+        private IdEqualAddress billing = new IdEqualAddress();
+        private boolean enabled;
 
         public String getStatus() { return status; }
 
+        public boolean isEnabled() { return enabled; }
+
+        public void setEnabled(boolean enabled) { this.enabled = enabled; }
+
+        public IdEqualAddress getBilling() { return billing; }
+
+        public void setBilling(IdEqualAddress billing) { this.billing = billing; }
+
         public void setStatus(String status) { this.status = status; }
+
+        public String getName() { return name; }
+
+        public void setName(String name) { this.name = name; }
+
+        public List<String> getTags() { return tags; }
+
+        public void setTags(List<String> tags) { this.tags = tags; }
+
+        public DefaultedAddress getAddress() { return address; }
+
+        public void setAddress(DefaultedAddress address) { this.address = address; }
+
+        public PinnedAddress getPinned() { return pinned; }
+
+        public void setPinned(PinnedAddress pinned) { this.pinned = pinned; }
+    }
+
+    public static class DefaultedAddress {
+        private String country = "PL";
+        private String city;
+
+        public String getCountry() { return country; }
+
+        public void setCountry(String country) { this.country = country; }
+
+        public String getCity() { return city; }
+
+        public void setCity(String city) { this.city = city; }
+    }
+
+    public static class HomeAddress extends DefaultedAddress {
+        private String floor;
+
+        public String getFloor() { return floor; }
+
+        public void setFloor(String floor) { this.floor = floor; }
+    }
+
+    /**
+     * WW-5790: equals by id only, the shape of a JPA entity; a leaf-blind equals must not hide a changed leaf.
+     */
+    public static class IdEqualAddress {
+        private Long id;
+        private String city;
+
+        public Long getId() { return id; }
+
+        public void setId(Long id) { this.id = id; }
+
+        public String getCity() { return city; }
+
+        public void setCity(String city) { this.city = city; }
+
+        @Override
+        public boolean equals(Object other) {
+            return other instanceof IdEqualAddress that && java.util.Objects.equals(id, that.id);
+        }
+
+        @Override
+        public int hashCode() { return java.util.Objects.hashCode(id); }
+    }
+
+    /**
+     * WW-5790: no no-arg constructor, so the copy cannot build a baseline for this subtree.
+     */
+    public static class PinnedAddress {
+        private String country = "PL";
+        private String city;
+
+        public PinnedAddress(String city) { this.city = city; }
+
+        public String getCountry() { return country; }
+
+        public void setCountry(String country) { this.country = country; }
+
+        public String getCity() { return city; }
+
+        public void setCity(String city) { this.city = city; }
+    }
+
+    /**
+     * WW-5790: a handler without the authorization-aware marker; what it sets stands in for the body.
+     */
+    public static class ScriptedHandler implements ContentTypeHandler {
+        private final Consumer<DefaultedModel> body;
+
+        ScriptedHandler(Consumer<DefaultedModel> body) { this.body = body; }
+
+        @Override
+        public void toObject(ActionInvocation invocation, Reader in, Object target) { body.accept((DefaultedModel) target); }
+
+        @Override
+        public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) { return null; }

Review Comment:
   The interceptor under test never calls fromObject; that is the result side, driven by RestActionInvocation. The existing StatePlantingHandler fixture in this class returns null for the same reason, so this one matches it.



##########
plugins/rest/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java:
##########
@@ -236,15 +241,21 @@ private void copyAuthorizedProperties(
             }
 
             Object sourceValue = readMethod.invoke(source);
+            Object baselineValue = baseline == null ? null : readMethod.invoke(baseline);

Review Comment:
   Moved below the null check in 022f1373c; the baseline getter now runs only when a comparison or a nested baseline needs it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@struts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WW-5790 fix(rest): copy only what the body handler changed in the two-phase copy [struts]

Posted by "lukaszlenart (via GitHub)" <gi...@apache.org>.
lukaszlenart commented on code in PR #2051:
URL: https://github.com/apache/struts/pull/2051#discussion_r4356758699


##########
plugins/rest/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java:
##########
@@ -236,15 +241,21 @@ private void copyAuthorizedProperties(
             }
 
             Object sourceValue = readMethod.invoke(source);
+            Object baselineValue = baseline == null ? null : readMethod.invoke(baseline);
+            // A null, or a leaf or collection equal to the baseline's constructor default, reads as "not in
+            // the body": copying it would overwrite what params or prepare() put on the target. The cost is
+            // that an explicit null, a primitive at its zero value, an empty collection, or any body value
+            // equal to the constructor default is not applied either. A nested bean is compared leaf by leaf
+            // below, never through its own equals.

Review Comment:
   Reworded in 022f1373c to "non-nested value (leaf, collection, map, array)".



##########
plugins/rest/src/main/java/org/apache/struts2/rest/ContentTypeInterceptor.java:
##########
@@ -236,15 +241,21 @@ private void copyAuthorizedProperties(
             }
 
             Object sourceValue = readMethod.invoke(source);
+            Object baselineValue = baseline == null ? null : readMethod.invoke(baseline);
+            // A null, or a leaf or collection equal to the baseline's constructor default, reads as "not in
+            // the body": copying it would overwrite what params or prepare() put on the target. The cost is
+            // that an explicit null, a primitive at its zero value, an empty collection, or any body value
+            // equal to the constructor default is not applied either. A nested bean is compared leaf by leaf
+            // below, never through its own equals.
             if (sourceValue == null) {
-                // Intentionally skip null values: in two-phase deserialization, properties NOT present in the
-                // request body will be null in the fresh instance. Copying null would clear pre-initialized
-                // fields on the target. This is the safer default — an explicit JSON null and a missing field
-                // are indistinguishable after deserialization into a fresh POJO.
+                continue;
+            }

Review Comment:
   Same as the thread above: fixed in 022f1373c.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@struts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WW-5790 fix(rest): copy only what the body handler changed in the two-phase copy [struts]

Posted by "lukaszlenart (via GitHub)" <gi...@apache.org>.
lukaszlenart merged PR #2051:
URL: https://github.com/apache/struts/pull/2051


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@struts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org