#1018 - HAL Forms properties customizable via annotations on representation model classes.

We now consider additional Jackson and JSR-303 annotations on representation models to enrich the HAL Forms template properties with additional information:

- @NotNull on a property flips its `required` flag to `true`.
- The "regexp" attribute of @Pattern on a property is forwarded into the "regex" field.
- @JsonProperty(Access.READ_ONLY) on a property is translated into the "readOnly" flag.

The default for the "required" flag have been changed to false even for PUT and POST as whether they're required or not is completely determined by the model, not the HTTP method.

All of this is backed by significant refactoring in the way that Affordance instances are build internally. The new API is centered around the Affordances type in the mediatype package. The methods on Link to create the Affordance` from its details have been removed and replaced by builder style APIs on Affordance. AffordanceModelFactory has been moved to the mediatype as well.

PropertyUtils has been significantly revamped to expose a PayloadMetadata/PropertyMetadata model to abstract the individual traits of a property. This allows to optionally plug in the support for JSR-303 annotations. AffordanceModelFactory has been refactored to rather work with those instead of ResolvableType.
This commit is contained in:
Oliver Drotbohm
2019-07-15 22:01:16 +02:00
parent ca68c7d1b2
commit fc1b4a8b51
48 changed files with 2276 additions and 860 deletions

View File

@@ -15,19 +15,82 @@
*/
package org.springframework.hateoas;
import lombok.Data;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.experimental.Wither;
import java.util.Optional;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Representation of a web request's query parameter (https://example.com?name=foo) => {"name", "foo", true}.
*
* @author Greg Turnquist
* @author Oliver Drotbohm
*/
@Data
@RequiredArgsConstructor
@Value
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class QueryParameter {
private final String name;
private final String value;
private final @Nullable @Wither String value;
private final boolean required;
/**
* Creates a new {@link QueryParameter} from the given {@link MethodParameter}.
*
* @param parameter must not be {@literal null}.
* @return will never be {@literal null}.
*/
public static QueryParameter of(MethodParameter parameter) {
MergedAnnotation<RequestParam> annotation = MergedAnnotations //
.from(parameter.getParameter()) //
.get(RequestParam.class);
String name = annotation.isPresent() && annotation.hasNonDefaultValue("name") //
? annotation.getString("name") //
: parameter.getParameterName();
if (name == null || !StringUtils.hasText(name)) {
throw new IllegalStateException(String.format("Couldn't determine parameter name for %s!", parameter));
}
boolean required = annotation.isPresent() && annotation.hasNonDefaultValue("required") //
? annotation.getBoolean("required") //
: !Optional.class.equals(parameter.getParameterType()); //
return required ? required(name) : optional(name);
}
/**
* Creates a new required {@link QueryParameter} with the given name;
*
* @param name must not be {@literal null} or empty.
* @return
*/
public static QueryParameter required(String name) {
Assert.hasText(name, "Name must not be null or empty!");
return new QueryParameter(name, null, true);
}
/**
* Creates a new optional {@link QueryParameter} with the given name;
*
* @param name must not be {@literal null} or empty.
* @return
*/
public static QueryParameter optional(String name) {
return new QueryParameter(name, null, false);
}
}