spring-projects / spring-hateoas

Spring HATEOAS - Library to support implementing representations for hyper-text driven REST web services.
https://spring.io/projects/spring-hateoas
Apache License 2.0
1.03k stars 475 forks source link

Consider @JsonProperty-defined property name for affordance metadata #1563

Closed talentedasian closed 3 years ago

talentedasian commented 3 years ago

Make the default name or value of the field name of the DefaultMetadataProperty to the value that is present in the @JsonProperty annotation else, use the default field name.

odrotbohm commented 3 years ago

Would you mind taking a step back and describing the actual problem to be solved?

talentedasian commented 3 years ago

image The image above is the desired output. Even though the field is named politicalParty, a @JsonProperty annotation is present with the value of political_party. This should be the default behavior for names of Hal+Form _templates property name value(_templates.default.properties[0].name) because the @JsonProperty values are the one that are expected in the @RequestBody.

For example, I have a DTO that is used in a @RequestBody:

public class AddPoliticianDTORequest {

        @JsonProperty("first_name")
    String firstName;

        @JsonProperty("last_name")  
    String lastName;

Spring MVC expects the body of the POST method to have json values of first_name and last_name. If I were to make an affordance for this in a Hal+Form mediatype, the resulting data would be:

"templates":{
   "default":{
      "method":"post",
      "properties":[
         {
            "name":"firstName",
             .......
         },
        {
            "name":"lastName",
             .......
         }
      ],
      "target":"http://localhost:8080/api/politician/politicians"
   }
}

The response would ultimately break your clients that use your api since you expect first_name and last_name instead of "firstName" and "lastName" all because of the @JsonProperty. The name of the property values should always be the value of a @JsonProperty annotation when it is present and if not, use the default field name.

odrotbohm commented 3 years ago

Awesome, thanks for the additional context.