Skip to content

[main] Add @Required and validation to StringMatchFilter text attribute (#3158 port)#4153

Open
vpelikh wants to merge 5 commits into
apache:mainfrom
vpelikh:GH-3158-port-to-main
Open

[main] Add @Required and validation to StringMatchFilter text attribute (#3158 port)#4153
vpelikh wants to merge 5 commits into
apache:mainfrom
vpelikh:GH-3158-port-to-main

Conversation

@vpelikh

@vpelikh vpelikh commented Jun 26, 2026

Copy link
Copy Markdown

Ports the StringMatchFilter validation work from the 2.x branch (#3158), originally started by @JWT007 in #3510 (his commit authorship is preserved), and resolves the earlier review feedback.

Changes

  • StringMatchFilter.java: The text builder attribute is now marked @Required(message = "No text provided for StringMatchFilter") so a missing attribute fails configuration validation. The constructor validates with Assert.requireNonEmpty(...). The builder's setText(@NonNull String) throws NullPointerException for a null argument and IllegalArgumentException for an empty string. build() returns null (logged error) when text is missing, and a new getText() accessor was added.
  • StringMatchFilterTest.java: Added tests covering valid construction, empty text rejection, null text rejection (NullPointerException), missing-attribute rejection via config, and the post-stop() guard.
  • Test configs: Added log4j2-stringmatchfilter-3153-ok.xml and log4j2-stringmatchfilter-3153-nok.xml fixtures. The nok config uses a correctly named <StringMatchFilter/> element with no text attribute (the earlier version used a misspelled plugin name and was replaced).

Why

An empty or missing text attribute on StringMatchFilter previously resulted in either a cryptic NullPointerException at runtime (after stop()) or silently matching the empty string against every event. This change provides clear validation at configuration time and graceful degradation.

Notes

@vpelikh
vpelikh force-pushed the GH-3158-port-to-main branch 4 times, most recently from 340a851 to fd62c22 Compare June 26, 2026 08:20
@vpelikh vpelikh changed the title [main] Add @Required and validation to StringMatchFilter text attribute (#3158) [main] Add @Required and validation to StringMatchFilter text attribute (#3158 port) Jun 26, 2026
@vpelikh vpelikh mentioned this pull request Jun 26, 2026
27 tasks
@vpelikh
vpelikh force-pushed the GH-3158-port-to-main branch from fd62c22 to 829c17c Compare July 16, 2026 07:21
@ramanathan1504

Copy link
Copy Markdown
Contributor

@vpelikh

It turns out the original author (@JWT007) already started porting this to main over in PR #3510, but that PR stalled and still has some unresolved review comments from @vy and @ppkarwasz .

Since he already did half the work, could you pull his commits into this PR (to preserve his authorship), and then address the review feedback that @vy and @ppkarwasz left on #3510?

If you can resolve those review comments here in this PR, I will close the old PR and we can focus on getting yours reviewed and merged!

JWT007 added 4 commits July 16, 2026 12:52
* Guard against NPEs
* added private constructor to builder
* added getText() accessor
* add JVerify nullability annotations
* added detailed javadoc with implementation details
* optimized AbstractLifeCycle and AbstractFilter "equalsImpl" implementations
* added changelog
@vpelikh
vpelikh force-pushed the GH-3158-port-to-main branch from 829c17c to 8b15430 Compare July 16, 2026 10:04
@vpelikh

vpelikh commented Jul 16, 2026

Copy link
Copy Markdown
Author

Thanks @ramanathan1504 — I've pulled @JWT007's original port commits from #3510 into this PR (authorship preserved) and addressed the open review feedback that @vy and @ppkarwasz left there:

@ppkarwaszsetText(null) should throw NPE (not IllegalArgumentException)

  • setText(@NonNull final String text) now calls Objects.requireNonNull(text, ...) first, so a null argument throws NullPointerException (the @NonNull contract violation).
  • An empty string still throws IllegalArgumentException via Assert.requireNonEmpty, keeping the two validation levels distinct (null → NPE, empty → IAE). The corresponding test now asserts NullPointerException.

@ppkarwasz — the misspelled-plugin-name test didn't belong here

  • The nok fixture (StringMatchFfilter, a typo) is replaced with a correct <StringMatchFilter/> element that omits the required text attribute, so the test now exercises StringMatchFilter's own @Required validation (the plugin is skipped → getFilter() is null) rather than PluginBuilder name resolution.

@vy / @ppkarwasz — avoid cosmetic changes on existing code

  • Reverted the method reordering and the rewritten filter() javadocs back to the original main form; only the validation behavior was added. The filter() method bodies are unchanged.
  • Dropped the out-of-scope equalsImpl refactors in AbstractFilter/AbstractLifeCycle and the Assert tweaks into a separate follow-up change, keeping this PR focused on StringMatchFilter validation.

- Preserve @JWT007's original port commits (authorship retained).
- Make setText(null) throw NullPointerException and setText("") throw IllegalArgumentException, differentiating the @nonnull and non-empty validation levels (review by ppkarwasz).
- Replace the misspelled-plugin-name configuration test with a correct StringMatchFilter element lacking the required 'text' attribute, so the test exercises the filter's own @required validation rather than PluginBuilder name resolution (review by ppkarwasz).
- Revert cosmetic method reordering and filter() javadoc rewrites; keep the existing method order and minimal documentation (review by vy/ppkarwasz).
- Drop the out-of-scope equalsImpl/Assert cosmetic refactors into a separate follow-up change (review by vy).
@vpelikh
vpelikh force-pushed the GH-3158-port-to-main branch from 8b15430 to c0f92de Compare July 16, 2026 10:06
@vpelikh
vpelikh marked this pull request as draft July 16, 2026 12:46
@vpelikh
vpelikh marked this pull request as ready for review July 16, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

3 participants