When writing a REST-API in Java, the PATCH method requests that a "set of changes" described in the request entity be applied to the resource identified by the Request-URI. (see: https://datatracker.ietf.org/doc/html/rfc5789)
Given following resource:
{ productId: 7, name: "container", contactPerson: "peter", price: 12}
With PATCH i should be able to set the "contactPerson" to NULL while not touching the other fields, like this:
{ productId: 7, contactPerson: null}
Within the Java Ecosystem how can i implement this in a good way, considering that the most relevant libraries here need to have full support for it:
- Jackson (JSON Deserialization)
- Hibernate Validator (Bean Validation)
- Microprofile OpenAPI (API-Definition)
Java Optional
Among others i tried out Java-Optional, and while it initially seemed to work it is the wrong tool for this, as an Optional should not contain a NULL-Value, but it should also be possible to set a value to NULL explicitly within a PATCH (instead of omitting it).
- but i did get very far with using Optional. The only problem was that Hibernate Validator did not supported it in the way i needed it.
For Example, the following looked good initially (here for the name-field which is required = false and nullable = false):
@Schema(required = false, nullable = false)Optional<@NotNull @Size(max = 255) String> name = Optional.empty();
Hibernate validator considered the @NotNull Annotation to be violated when the name field was missing in the request, which i found strange because the annotation is within the Optional-Generic-Declaration (not directly on the Optional)
JsonNullable
Jackson provides a JsonNullable annotation. While this is the right tool for the task and is seemingly supported for openapi, it does not seem to be supported for hibernate validator, at least i could not find something about it.
Validation
Mostly the problem seems to be during validation of Bean Validation Annotations, which are important to have for an API.
- When using annotations like
@Size(255)
,@Email
etc. there is the requirement that...- ...those validations must be validated by the Hibernate Validator when the field is given/set in the PATCH
- ...those validations must not be validated when the field is omitted (missing) in the PATCH.
--
Is the REST-PATCH covered by those major libraries of the Java Ecosystem, and if yes, what is the recommended way to support it?