Skip to content

Commit d81dc62

Browse files
committed
Support HAL-Forms options 'selectedValues'
1 parent d6265fb commit d81dc62

2 files changed

Lines changed: 48 additions & 19 deletions

File tree

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static Inline inline(Collection<? extends Object> values) {
6363

6464
Assert.notNull(values, "Values must not be null!");
6565

66-
return new Inline(values, null, null, null, null, null);
66+
return new Inline(values, null, null, null, null, null, null);
6767
}
6868

6969
/**
@@ -76,7 +76,7 @@ public static Remote remote(Link link) {
7676

7777
Assert.notNull(link, "Link must not be null!");
7878

79-
return new Remote(link, null, null, null, null, null);
79+
return new Remote(link, null, null, null, null, null, null);
8080
}
8181

8282
/**
@@ -127,15 +127,19 @@ public static Remote remote(String href) {
127127
@Nullable
128128
Object getSelectedValue();
129129

130+
@Nullable
131+
Object getSelectedValues();
132+
130133
public static abstract class AbstractHalFormsOptions<T extends AbstractHalFormsOptions<T>>
131134
implements HalFormsOptions {
132135

133136
private final @Nullable String promptField, valueField;
134137
private final @Nullable Long minItems, maxItems;
135138
private final @Nullable Object selectedValue;
139+
private final @Nullable Object selectedValues;
136140

137141
protected AbstractHalFormsOptions(@Nullable String promptRef, @Nullable String valueRef, @Nullable Long minItems,
138-
@Nullable Long maxItems, @Nullable Object selectedValue) {
142+
@Nullable Long maxItems, @Nullable Object selectedValue, @Nullable Object selectedValues) {
139143

140144
Assert.isTrue(minItems == null || minItems >= 0, "MinItems must be greater than or equal to 0!");
141145

@@ -144,6 +148,7 @@ protected AbstractHalFormsOptions(@Nullable String promptRef, @Nullable String v
144148
this.minItems = minItems;
145149
this.maxItems = maxItems;
146150
this.selectedValue = selectedValue;
151+
this.selectedValues = selectedValues;
147152
}
148153

149154
/*
@@ -201,6 +206,13 @@ public Object getSelectedValue() {
201206
return selectedValue;
202207
}
203208

209+
@Nullable
210+
@Override
211+
@JsonProperty
212+
public Object getSelectedValues() {
213+
return selectedValues;
214+
}
215+
204216
/**
205217
* Configures the given field to be used as prompt field.
206218
*
@@ -213,7 +225,7 @@ public T withPromptField(String promptField) {
213225
throw new IllegalArgumentException("Prompt field has to either be null or actually have text!");
214226
}
215227

216-
return with(promptField, valueField, minItems, maxItems, selectedValue);
228+
return with(promptField, valueField, minItems, maxItems, selectedValue, selectedValues);
217229
}
218230

219231
/**
@@ -228,7 +240,7 @@ public T withValueField(String valueField) {
228240
throw new IllegalArgumentException("Value field has to either be null or actually have text!");
229241
}
230242

231-
return with(promptField, valueField, minItems, maxItems, selectedValue);
243+
return with(promptField, valueField, minItems, maxItems, selectedValue, selectedValues);
232244
}
233245

234246
/**
@@ -243,7 +255,7 @@ public T withMinItems(Long minItems) {
243255
throw new IllegalArgumentException("minItems has to be null or greater or equal to zero!");
244256
}
245257

246-
return with(promptField, valueField, minItems, maxItems, selectedValue);
258+
return with(promptField, valueField, minItems, maxItems, selectedValue, selectedValues);
247259
}
248260

249261
/**
@@ -258,17 +270,29 @@ public T withMaxItems(@Nullable Long maxItems) {
258270
throw new IllegalArgumentException("maxItems has to be null or greater than zero!");
259271
}
260272

261-
return with(promptField, valueField, minItems, maxItems, selectedValue);
273+
return with(promptField, valueField, minItems, maxItems, selectedValue, selectedValues);
262274
}
263275

264276
/**
265277
* Configured the value to be initially selected
266278
*
267279
* @param value
268280
* @return
281+
* @deprecated Use {@link #withSelectedValues(Object)} instead
269282
*/
283+
@Deprecated(forRemoval = true)
270284
public T withSelectedValue(@Nullable Object value) {
271-
return with(promptField, valueField, minItems, maxItems, value);
285+
return with(promptField, valueField, minItems, maxItems, value, selectedValues);
286+
}
287+
288+
/**
289+
* Configured the values to be initially selected
290+
*
291+
* @param values
292+
* @return
293+
*/
294+
public T withSelectedValues(@Nullable Object values) {
295+
return with(promptField, valueField, minItems, maxItems, selectedValue, values);
272296
}
273297

274298
/**
@@ -281,7 +305,7 @@ public T withSelectedValue(@Nullable Object value) {
281305
* @return
282306
*/
283307
protected abstract T with(@Nullable String promptRef, @Nullable String valueRef, @Nullable Long minItems,
284-
@Nullable Long maxItems, @Nullable Object selectedValue);
308+
@Nullable Long maxItems, @Nullable Object selectedValue, @Nullable Object selectedValues);
285309
}
286310

287311
public static class Inline extends AbstractHalFormsOptions<Inline> {
@@ -294,9 +318,9 @@ public static class Inline extends AbstractHalFormsOptions<Inline> {
294318
* @param valueRef
295319
*/
296320
private Inline(Collection<? extends Object> values, @Nullable String promptRef, @Nullable String valueRef,
297-
@Nullable Long minItems, @Nullable Long maxItems, @Nullable Object selectedValue) {
321+
@Nullable Long minItems, @Nullable Long maxItems, @Nullable Object selectedValue, @Nullable Object selectedValues) {
298322

299-
super(promptRef, valueRef, minItems, maxItems, selectedValue);
323+
super(promptRef, valueRef, minItems, maxItems, selectedValue, selectedValues);
300324

301325
Assert.notNull(values, "Values must not be null!");
302326

@@ -319,8 +343,8 @@ public Collection<? extends Object> getInline() {
319343
*/
320344
@Override
321345
protected Inline with(@Nullable String promptRef, @Nullable String valueRef, @Nullable Long minItems,
322-
@Nullable Long maxItems, @Nullable Object selectedValue) {
323-
return new Inline(inline, promptRef, valueRef, minItems, maxItems, selectedValue);
346+
@Nullable Long maxItems, @Nullable Object selectedValue, @Nullable Object selectedValues) {
347+
return new Inline(inline, promptRef, valueRef, minItems, maxItems, selectedValue, selectedValues);
324348
}
325349
}
326350

@@ -334,9 +358,9 @@ public static class Remote extends AbstractHalFormsOptions<Remote> {
334358
private final Link link;
335359

336360
private Remote(Link link, @Nullable String promptRef, @Nullable String valueRef, @Nullable Long minItems,
337-
@Nullable Long maxItems, @Nullable Object selectedValue) {
361+
@Nullable Long maxItems, @Nullable Object selectedValue, @Nullable Object selectedValues) {
338362

339-
super(promptRef, valueRef, minItems, maxItems, selectedValue);
363+
super(promptRef, valueRef, minItems, maxItems, selectedValue, selectedValues);
340364

341365
Assert.notNull(link, "Link must not be null!");
342366

@@ -359,8 +383,8 @@ public Link getLink() {
359383
*/
360384
@Override
361385
protected Remote with(@Nullable String promptRef, @Nullable String valueRef, @Nullable Long minItems,
362-
@Nullable Long maxItems, @Nullable Object selectedValue) {
363-
return new Remote(link, promptRef, valueRef, minItems, maxItems, selectedValue);
386+
@Nullable Long maxItems, @Nullable Object selectedValue, @Nullable Object selectedValues) {
387+
return new Remote(link, promptRef, valueRef, minItems, maxItems, selectedValue, selectedValues);
364388
}
365389
}
366390
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import com.jayway.jsonpath.TypeRef;
2021
import jakarta.validation.constraints.NotNull;
2122
import jakarta.validation.constraints.Pattern;
2223
import lombok.Getter;
@@ -466,7 +467,8 @@ void rendersFullInlineOptions() {
466467
.withPromptField("my-prompt-field")
467468
.withValueField("my-value-field")
468469
.withMinItems(2L)
469-
.withMaxItems(3L);
470+
.withMaxItems(3L)
471+
.withSelectedValues(List.of("bar", "baz"));
470472

471473
getCuriedMapper()
472474
.assertSerializes(options)
@@ -478,6 +480,7 @@ void rendersFullInlineOptions() {
478480
assertThat(result.read("$.valueField", String.class)).isEqualTo("my-value-field");
479481
assertThat(result.read("$.minItems", Long.class)).isEqualTo(2L);
480482
assertThat(result.read("$.maxItems", Long.class)).isEqualTo(3L);
483+
assertThat(result.read("$.selectedValues", List.class)).isEqualTo(List.of("bar", "baz"));
481484
});
482485
}
483486

@@ -490,7 +493,8 @@ void rendersFullRemoteOptions() {
490493
.withPromptField("my-prompt-field")
491494
.withValueField("my-value-field")
492495
.withMinItems(2L)
493-
.withMaxItems(3L);
496+
.withMaxItems(3L)
497+
.withSelectedValues(List.of("bar", "baz"));
494498

495499
getCuriedMapper()
496500
.assertSerializes(options)
@@ -503,6 +507,7 @@ void rendersFullRemoteOptions() {
503507
assertThat(result.read("$.valueField", String.class)).isEqualTo("my-value-field");
504508
assertThat(result.read("$.minItems", Long.class)).isEqualTo(2L);
505509
assertThat(result.read("$.maxItems", Long.class)).isEqualTo(3L);
510+
assertThat(result.read("$.selectedValues", List.class)).isEqualTo(List.of("bar", "baz"));
506511
});
507512

508513
}

0 commit comments

Comments
 (0)