openrewrite / rewrite-jackson

OpenRewrite recipes for Jackson.
Apache License 2.0
1 stars 0 forks source link

Remove `@JsonProperty` annotation from record arguments when only value attribute matches argument name #7

Open samuelfac opened 2 weeks ago

samuelfac commented 2 weeks ago

What problem are you trying to solve?

Remove the annotation @JsonProperty when is no necessary, like:

What precondition(s) should be checked before applying this recipe?

Describe the situation before applying the recipe

public record CategorieRequest(
        @JsonProperty(value = "name", required = true) String name,
        @JsonProperty("color") String color,
        @JsonProperty("parent_id") Long parentId
) {
}

Describe the situation after applying the recipe

public record CategorieRequest(
        @JsonProperty(required = true) String name,
        String color,
        @JsonProperty("parent_id") Long parentId
) {
}
timtebeek commented 2 weeks ago

Hi @samuelfac ; thanks for the suggestion! I've moved the issue to rewrite-jackson, as it seemed to fit in there. Do please let me know if you had intended a different framework!

This seems like a fairly straightforward recipe to me; create a recipe with a visitor that visits records and their constructor arguments, and then remove the annotation if it only has an explicit or implicit value attribute, whose textual representation matches the argument.

Would you be willing to help create such a recipe? We'll gladly help guide such a recipe implementation, typically starting with a draft PR containing just the tests to capture which cases to (not) match & convert.