You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "jadhavgaurav (via GitHub)" <gi...@apache.org> on 2026/09/22 07:22:07 UTC

[PR] [LANG-1249] Propagate excludeFieldNames to recursive toString rendering [commons-lang]

jadhavgaurav opened a new pull request, #1798:
URL: https://github.com/apache/commons-lang/pull/1798

   https://issues.apache.org/jira/browse/LANG-1249
   
   ## What
   
   `RecursiveToStringStyle.appendDetail()` rendered nested objects by calling
   the static `ReflectionToStringBuilder.toString(value, style)` convenience
   method. That method builds a brand-new `ReflectionToStringBuilder` with no
   exclusions, so a field name excluded on the outer builder via
   `setExcludeFieldNames()` only applied to the top-level object's own
   fields; the exclusion silently stopped applying as soon as traversal
   entered a nested object.
   
   ## How
   
   `ReflectionToStringBuilder.toString()` now pushes its own
   `excludeFieldNames` onto a per-thread stack kept on the style, when the
   style is a `RecursiveToStringStyle`, before rendering, and pops it
   afterward in a `finally` block. `RecursiveToStringStyle.appendDetail()`
   reads the current top of that stack and applies it to each nested
   `ReflectionToStringBuilder` it constructs, so the same exclusions now
   propagate through every level of recursion.
   
   A stack, rather than a single field, correctly restores the enclosing
   value if the style is reentered on the same thread with a different
   exclusion list. It is backed by a `LinkedList` rather than an
   `ArrayDeque` because the common case, no exclusions configured, pushes a
   `null` element, which `ArrayDeque` rejects (this bit me during testing:
   switching from `ArrayDeque` to `LinkedList` was needed to avoid an NPE on
   every call that doesn't set exclusions).
   
   ## Why
   
   Reported behavior in the ticket: excluding a field that only exists on a
   nested object (e.g. `Bar.b` when the top-level object is `Foo` containing
   a `Bar bar` field) has no effect, even though excluding a top-level field
   (`Foo.bar`) works correctly. This fixes that asymmetry.
   
   ## Testing
   
   Added `RecursiveToStringStyleTest#testExcludeFieldNamesAppliesToNestedObjects`,
   which fails against the prior behavior (the nested field stays in the
   output) and passes with this change. Verified the regression test fails
   with only the source fix reverted (test kept) and passes with the fix
   reapplied.
   
   Ran the full test suite (`mvn test`): 98254 tests, 0 failures, 0 errors,
   69 skipped (pre-existing, environment-related, unrelated to this change).
   Ran `mvn checkstyle:check`: clean.


-- 
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: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1249] Propagate excludeFieldNames to recursive toString rendering [commons-lang]

Posted by "jadhavgaurav (via GitHub)" <gi...@apache.org>.
jadhavgaurav closed pull request #1798: [LANG-1249] Propagate excludeFieldNames to recursive toString rendering
URL: https://github.com/apache/commons-lang/pull/1798


-- 
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: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1249] Propagate excludeFieldNames to recursive toString rendering [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #1798:
URL: https://github.com/apache/commons-lang/pull/1798#issuecomment-5775670565

   Set to draft status. I'm not sure we actually need since, the Jira is over ten years old without further requests for implementation.


-- 
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: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1249] Propagate excludeFieldNames to recursive toString rendering [commons-lang]

Posted by "jadhavgaurav (via GitHub)" <gi...@apache.org>.
jadhavgaurav commented on PR #1798:
URL: https://github.com/apache/commons-lang/pull/1798#issuecomment-5889248536

   Thanks for looking, and understood that the ticket is old. Here is the concrete behavior the patch changes, so you can judge whether it is worth having.
   
   With a `ReflectionToStringBuilder` using `RecursiveToStringStyle`, `setExcludeFieldNames` currently applies only to the top-level object:
   
   ```java
   Person p = new Person();
   p.job = new Job();
   p.job.title = "Manager";
   ReflectionToStringBuilder b = new ReflectionToStringBuilder(p, new RecursiveToStringStyle());
   b.setExcludeFieldNames("title");
   b.toString();
   ```
   
   Before: `...job=Job[title=Manager]...` (the excluded field is still printed)
   After: `...job=Job[]...`
   
   The cause is that `RecursiveToStringStyle.appendDetail()` renders a nested object through the static `ReflectionToStringBuilder.toString(value, style)`, which builds a fresh builder that knows nothing about the outer builder's exclusions. So an exclusion meant to keep a field out of the output (a password field, for example) is honored on the outer object and silently ignored on nested ones.
   
   `RecursiveToStringStyleTest#testExcludeFieldNamesAppliesToNestedObjects` fails without the change and passes with it.
   
   If you would rather not change this behavior, I understand and am fine with the PR being closed. If it is wanted, I can also simplify the approach (for example, avoid the per-thread stack) if you prefer something smaller.
   


-- 
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: issues-unsubscribe@commons.apache.org

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


Re: [PR] [LANG-1249] Propagate excludeFieldNames to recursive toString rendering [commons-lang]

Posted by "jadhavgaurav (via GitHub)" <gi...@apache.org>.
jadhavgaurav commented on PR #1798:
URL: https://github.com/apache/commons-lang/pull/1798#issuecomment-6041152324

   Closing this one. Thanks for taking a look, and for the clear feedback that the ticket may not be needed after all. Happy to reopen if it turns out to be wanted.


-- 
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: issues-unsubscribe@commons.apache.org

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