Skip to content

Custom Comparators do not behave as expected when dealing with certain regular expressions that contain JSON Arrays #161

@jfox13-nd

Description

@jfox13-nd

When using a CustomComparator based on a RegularExpressionValueMatcher to match values of the type foo[*].bar.baz the CustomComparator will be disregarded in the NON_EXTENSIBLE and LENIENT modes.

Here is a minimal example that will throw two AssertionErrors:

        String expected = "{\"addresses\": [ {\"address\": {\"num\": \"not_a_number\"} } ] }";
        String actual =   "{\"addresses\": [ {\"address\": {\"num\": \"not_a_number\"} } ] }";

        // Correctly checks that there are number values at addresses[*].address.num
        JSONAssert.assertNotEquals(expected, actual,
                new CustomComparator(
                        JSONCompareMode.STRICT,
                        new Customization("addresses[*].address.num",
                                new RegularExpressionValueMatcher<Object>("\\d"))));

        // Fails to check that a number value exists at addresses[*].address.num
        JSONAssert.assertNotEquals(expected, actual,
                new CustomComparator(
                        JSONCompareMode.NON_EXTENSIBLE,
                        new Customization("addresses[*].address.num",
                                new RegularExpressionValueMatcher<Object>("\\d"))));

        // Fails to check that a number value exists at addresses[*].address.num
        JSONAssert.assertNotEquals(expected, actual,
                new CustomComparator(
                        JSONCompareMode.LENIENT,
                        new Customization("addresses[*].address.num",
                                new RegularExpressionValueMatcher<Object>("\\d"))));

I have created a PR that fixes this edge case:
#160

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions