Skip to content

Refactor ViolationMessagesMacro::constructMessageKeyUrl to use UrlEncoder #13490

@stoyanK7

Description

@stoyanK7

From #13423 (comment)

Method constructMessageKeyUrl in ViolationMessagesMacro does not encode the URL

    private static String constructMessageKeyUrl(Class<?> clss, String messageKey) {
        return "https://github.com/search?q="
                + "path%3Asrc%2Fmain%2Fresources%2F"
                + clss.getPackage().getName().replace(".", "%2F")
                + "%20path%3A**%2Fmessages*.properties+repo%3Acheckstyle%2F"
                + "checkstyle+%22" + messageKey + "%22";
}

UrlEncoder.encode can be used in this case (https://www.baeldung.com/java-url-encoding-decoding). However, this is not possible without creating a ton of suppressions.

A space can be transformed to + or %20. From https://www.w3schools.com/tags/ref_urlencode.ASP:

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

UrlEncoder.encode replaces spaces to + but the following test expects both %20 and +

else {
expectedUrl = "https://github.com/search?q="
+ "path%3Asrc%2Fmain%2Fresources%2F"
+ clss.getPackage().getName().replace(".", "%2F")
+ "%20path%3A**%2Fmessages*.properties+repo%3Acheckstyle%2F"
+ "checkstyle+%22" + linkText + "%22";
}

After all check templates use the violation messages macro, we can switch to UrlEncoder and edit the test to expect +

Metadata

Metadata

Assignees

No one assigned

    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