Skip to content

Commit c487fcc

Browse files
committed
#1697 - Support for custom property types in HAL FORMS.
As per the discussion in mamund/hal-forms#88, the list of property types in HAL FORMS is not exhaustive. This means we cannot align them with HTML input types and even more so not drop anything not an HTML input type. This is now implemented by switching to generic Strings in the HAL FORMS property DTO and directly piping the value originally registered on the affordance into it.
1 parent 0712563 commit c487fcc

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsProperty.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Optional;
2020

2121
import org.springframework.hateoas.AffordanceModel.Named;
22-
import org.springframework.hateoas.mediatype.html.HtmlInputType;
2322
import org.springframework.lang.Nullable;
2423
import org.springframework.util.Assert;
2524
import org.springframework.util.StringUtils;
@@ -44,7 +43,7 @@ final class HalFormsProperty implements Named {
4443
private final @JsonInclude(Include.NON_DEFAULT) boolean readOnly, required;
4544
private final @Nullable Number min, max;
4645
private final @Nullable Long minLength, maxLength;
47-
private final @Nullable HtmlInputType type;
46+
private final @Nullable String type;
4847
private final @Nullable HalFormsOptions options;
4948

5049
HalFormsProperty() {
@@ -69,7 +68,7 @@ final class HalFormsProperty implements Named {
6968
private HalFormsProperty(String name, boolean readOnly, @Nullable Object value, String prompt, String regex,
7069
boolean templated, //
7170
boolean required, boolean multi, String placeholder, @Nullable Number min, @Nullable Number max,
72-
@Nullable Long minLength, @Nullable Long maxLength, @Nullable HtmlInputType type,
71+
@Nullable Long minLength, @Nullable Long maxLength, @Nullable String type,
7372
@Nullable HalFormsOptions options) {
7473

7574
Assert.notNull(name, "Name must not be null!");
@@ -294,7 +293,7 @@ HalFormsProperty withMaxLength(@Nullable Long maxLength) {
294293
* @param type can be {@literal null}
295294
* @return will never be {@literal null}.
296295
*/
297-
HalFormsProperty withType(@Nullable HtmlInputType type) {
296+
HalFormsProperty withType(@Nullable String type) {
298297

299298
return Objects.equals(this.type, type) ? this
300299
: new HalFormsProperty(this.name, this.readOnly, this.value, this.prompt, this.regex, this.templated,
@@ -413,7 +412,7 @@ Long getMaxLength() {
413412
@Nullable
414413
@JsonProperty
415414
@JsonInclude(Include.NON_NULL)
416-
HtmlInputType getType() {
415+
String getType() {
417416
return type;
418417
}
419418

src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsPropertyFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.hateoas.AffordanceModel.InputPayloadMetadata;
3030
import org.springframework.hateoas.AffordanceModel.PropertyMetadata;
3131
import org.springframework.hateoas.mediatype.MessageResolver;
32-
import org.springframework.hateoas.mediatype.html.HtmlInputType;
3332
import org.springframework.http.HttpMethod;
3433
import org.springframework.lang.NonNull;
3534
import org.springframework.lang.Nullable;
@@ -84,8 +83,7 @@ public List<HalFormsProperty> createProperties(HalFormsAffordanceModel model) {
8483

8584
return model.createProperties((payload, metadata) -> {
8685

87-
String inputTypeSource = metadata.getInputType();
88-
HtmlInputType inputType = inputTypeSource == null ? null : HtmlInputType.of(inputTypeSource);
86+
String inputType = metadata.getInputType();
8987
HalFormsOptions options = optionsFactory.getOptions(payload, metadata);
9088

9189
HalFormsProperty property = new HalFormsProperty()

src/test/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsTemplateBuilderUnitTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.junit.jupiter.api.Test;
3838
import org.junit.jupiter.params.ParameterizedTest;
3939
import org.junit.jupiter.params.provider.CsvSource;
40+
import org.springframework.hateoas.InputType;
4041
import org.springframework.hateoas.Link;
4142
import org.springframework.hateoas.LinkRelation;
4243
import org.springframework.hateoas.RepresentationModel;
@@ -246,6 +247,23 @@ void expandsAffordanceLinkForFormTarget() {
246247
assertThat(templates.get("default").getTarget()).endsWith("/example");
247248
}
248249

250+
@Test // #1697
251+
void exposesCustomInputType() {
252+
253+
RepresentationModel<?> models = new RepresentationModel<>(
254+
Affordances.of(Link.of("/example", LinkRelation.of("example"))) //
255+
.afford(HttpMethod.POST) //
256+
.withInput(WithCustomInputType.class) //
257+
.toLink());
258+
259+
Map<String, HalFormsTemplate> templates = new HalFormsTemplateBuilder(new HalFormsConfiguration(),
260+
MessageResolver.DEFAULTS_ONLY).findTemplates(models);
261+
262+
assertThat(templates.get("default").getPropertyByName("property")).hasValueSatisfying(it -> {
263+
assertThat(it.getType()).isEqualTo("custom");
264+
});
265+
}
266+
249267
@Getter
250268
static class PatternExample extends RepresentationModel<PatternExample> {
251269

@@ -283,4 +301,11 @@ static class Payload {
283301
@DecimalMax("5.3") //
284302
BigDecimal decimal;
285303
}
304+
305+
@Getter
306+
static class WithCustomInputType {
307+
308+
@InputType("custom") //
309+
String property;
310+
}
286311
}

0 commit comments

Comments
 (0)