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.04k stars 478 forks source link

HAL-FORMS properties marked readOnly when @JsonCreator is available. #1078

Open marcuslange opened 5 years ago

marcuslange commented 5 years ago

In cases where command style objects are used for PUT and POST we use immutable object with a @JsonCreator annotation. I think this is something that should be available in the affordances rendering vs just marking them readOnly.

gregturn commented 5 years ago

Can you paste a simple example? I'd love to accommodate your needs.

marcuslange commented 5 years ago
@Getter
public class CreateUser {
    @JsonProperty("first_name")
    private final String firstName;
    @JsonProperty("last_name")
    private final String lastName;
    @JsonProperty("email")
    private final String email;
    @JsonProperty("home_phone")
    private final String homePhone;
    @JsonProperty("cell_phone")
    private final String cellPhone;

   @JsonCreator
    public CreateUser(@JsonProperty("first_name") String firstName,
                      @JsonProperty("last_name") String lastName,
                      @JsonProperty("email") String email,
                      @JsonProperty("home_phone") @Nullable  String homePhone,
                      @JsonProperty("cell_phone") @Nullable String cellPhone) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.homePhone = homePhone;
        this.cellPhone = cellPhone;
    }
}

I would expect for this you have HAL-FORM that is not marked readOnly and required false on the @Nullable fields.

odrotbohm commented 5 years ago

That’s a tricky one as we‘d need access to Jackson’s mapping information, which properties it considers creators etc. and currently out property metamodel is entirely driven from a static context that doesn’t know anything about Jackson.

I’ll see whether we can abstract that away as it looks like the property inspection is usually triggered from within a context in which we have access to an AnnotationIntrospector, so that we could wrap that into an implementation of an interface that all downstream components use to decide whether a property is a creator property.

reda-alaoui commented 1 year ago

@odrotbohm , I think java Record could be at least excluded from this behaviour by default. What do you think?