Visitar URL original
PlaceholderDifferenceEvaluator: capturing groups in custom delimiter regexes break placeholder parsing · Issue #350 · xmlunit/xmlunit · GitHub
Skip to content

PlaceholderDifferenceEvaluator: capturing groups in custom delimiter regexes break placeholder parsing #350

Open

Description

@ishwarjha97

PlaceholderDifferenceEvaluator builds its placeholder and argument patterns by concatenating the user-supplied delimiter regexes with its own capturing groups, then reads the captured parts with hardcoded group indices (group(2) and group(3)). When a custom delimiter regex itself contains a capturing group, those indices shift and parsing breaks.

For example, an args opening delimiter regex of (\\() is functionally identical to the default \\( (it just wraps the escaped parenthesis in a capturing group), but:

Diff diff = DiffBuilder.compare("<a>${xmlunit.matchesRegex([a-z]+)}</a>").withTest("<a>abc</a>")
    .withDifferenceEvaluator(new PlaceholderDifferenceEvaluator(null, null, "(\\()", "(\\))", null))
    .build();

reads "(" as the argument list instead of "[a-z]+" and fails with an XMLUnitException caused by a PatternSyntaxException ("Unclosed group").

Likewise, a placeholder opening delimiter regex of (\\$\\{) stops ${xmlunit.ignore} from being recognised at all, so the comparison reports a difference.

This was noticed downstream in WireMock's equalToXml placeholder support (wiremock/wiremock#3094).

The fix I have ready: give the captured parts of the combined patterns named groups (placeholderContent, placeholderKeyword, placeholderArgs) and read them by name, so custom delimiter regexes may contain any number of capturing groups without affecting parsing. Includes regression tests. Happy to open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions