Skip to content

Commit 93209ce

Browse files
committed
Provide a property creation context to HalFormsOptionsFactory
1 parent 1c3eab8 commit 93209ce

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

src/main/java/org/springframework/hateoas/AffordanceModel.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,35 @@ public PayloadMetadata getOutput() {
147147
* @return will never be {@literal null}.
148148
* @since 1.3
149149
*/
150-
public <T> List<T> createProperties(BiFunction<InputPayloadMetadata, PropertyMetadata, T> creator) {
150+
public <T> List<T> createProperties(PropertyCreator<T> creator) {
151151

152+
PropertyCreationContext context = new PropertyCreationContext(getLink());
152153
return input.stream() //
153-
.map(it -> creator.apply(input, it)) //
154+
.map(it -> creator.apply(input, it, context)) //
154155
.collect(Collectors.toList());
155156
}
156157

158+
@FunctionalInterface
159+
public interface PropertyCreator<T> {
160+
T apply(InputPayloadMetadata payload, PropertyMetadata metadata, PropertyCreationContext context);
161+
}
162+
163+
public static final class PropertyCreationContext {
164+
165+
private final Link link;
166+
167+
private PropertyCreationContext(Link link) {
168+
this.link = link;
169+
}
170+
171+
/**
172+
* {@link Link} for the URI of the resource.
173+
*/
174+
public Link link() {
175+
return link;
176+
}
177+
}
178+
157179
/*
158180
* (non-Javadoc)
159181
* @see java.lang.Object#equals(java.lang.Object)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Optional;
24+
import java.util.function.BiFunction;
2425
import java.util.function.Consumer;
2526
import java.util.function.Function;
2627

2728
import org.springframework.core.ResolvableType;
29+
import org.springframework.hateoas.AffordanceModel;
2830
import org.springframework.hateoas.AffordanceModel.PropertyMetadata;
2931
import org.springframework.hateoas.MediaTypes;
3032
import org.springframework.hateoas.mediatype.hal.HalConfiguration;
@@ -165,7 +167,7 @@ public HalFormsConfiguration customize(ObjectMapper mapper) {
165167
* @return
166168
*/
167169
public <T> HalFormsConfiguration withOptions(Class<T> type, String property,
168-
Function<PropertyMetadata, HalFormsOptions> creator) {
170+
BiFunction<PropertyMetadata, AffordanceModel.PropertyCreationContext, HalFormsOptions> creator) {
169171

170172
return new HalFormsConfiguration(halConfiguration, patterns, options.withOptions(type, property, creator),
171173
objectMapperCustomizer, mediaTypes);

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
import java.util.HashMap;
1919
import java.util.Map;
20+
import java.util.function.BiFunction;
2021
import java.util.function.Function;
2122

2223
import org.springframework.hateoas.AffordanceModel.PayloadMetadata;
2324
import org.springframework.hateoas.AffordanceModel.PropertyMetadata;
25+
import org.springframework.hateoas.AffordanceModel.PropertyCreationContext;
2426
import org.springframework.lang.Nullable;
2527
import org.springframework.util.Assert;
2628

@@ -34,7 +36,7 @@
3436
*/
3537
class HalFormsOptionsFactory {
3638

37-
private final Map<Class<?>, Map<String, Function<PropertyMetadata, HalFormsOptions>>> options;
39+
private final Map<Class<?>, Map<String, BiFunction<PropertyMetadata, PropertyCreationContext, HalFormsOptions>>> options;
3840

3941
/**
4042
* Creates a new, empty {@link HalFormsOptionsFactory}.
@@ -48,7 +50,7 @@ public HalFormsOptionsFactory() {
4850
*
4951
* @param options must not be {@literal null}.
5052
*/
51-
private HalFormsOptionsFactory(Map<Class<?>, Map<String, Function<PropertyMetadata, HalFormsOptions>>> options) {
53+
private HalFormsOptionsFactory(Map<Class<?>, Map<String, BiFunction<PropertyMetadata, PropertyCreationContext, HalFormsOptions>>> options) {
5254
this.options = options;
5355
}
5456

@@ -64,13 +66,13 @@ private HalFormsOptionsFactory(Map<Class<?>, Map<String, Function<PropertyMetada
6466
* @see HalFormsOptions#remote(org.springframework.hateoas.Link)
6567
*/
6668
HalFormsOptionsFactory withOptions(Class<?> type, String property,
67-
Function<PropertyMetadata, HalFormsOptions> creator) {
69+
BiFunction<PropertyMetadata, PropertyCreationContext, HalFormsOptions> creator) {
6870

6971
Assert.notNull(type, "Type must not be null!");
7072
Assert.hasText(property, "Property must not be null or empty!");
7173
Assert.notNull(creator, "Creator function must not be null!");
7274

73-
Map<Class<?>, Map<String, Function<PropertyMetadata, HalFormsOptions>>> options = new HashMap<>(this.options);
75+
Map<Class<?>, Map<String, BiFunction<PropertyMetadata, PropertyCreationContext, HalFormsOptions>>> options = new HashMap<>(this.options);
7476

7577
options.compute(type, (it, map) -> {
7678

@@ -95,22 +97,22 @@ HalFormsOptionsFactory withOptions(Class<?> type, String property,
9597
* @return
9698
*/
9799
@Nullable
98-
HalFormsOptions getOptions(PayloadMetadata payload, PropertyMetadata property) {
100+
HalFormsOptions getOptions(PayloadMetadata payload, PropertyMetadata property, PropertyCreationContext context) {
99101

100102
Assert.notNull(payload, "Payload metadata must not be null!");
101103
Assert.notNull(property, "Property metadata must not be null!");
102104

103105
Class<?> type = payload.getType();
104106
String name = property.getName();
105107

106-
Map<String, Function<PropertyMetadata, HalFormsOptions>> map = options.get(type);
108+
Map<String, BiFunction<PropertyMetadata, PropertyCreationContext, HalFormsOptions>> map = options.get(type);
107109

108110
if (map == null) {
109111
return null;
110112
}
111113

112-
Function<PropertyMetadata, HalFormsOptions> function = map.get(name);
114+
BiFunction<PropertyMetadata, PropertyCreationContext, HalFormsOptions> function = map.get(name);
113115

114-
return function == null ? null : function.apply(property);
116+
return function == null ? null : function.apply(property, context);
115117
}
116118
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public List<HalFormsProperty> createProperties(HalFormsAffordanceModel model) {
8080

8181
HalFormsOptionsFactory optionsFactory = configuration.getOptionsFactory();
8282

83-
return model.createProperties((payload, metadata) -> {
83+
return model.createProperties((payload, metadata, context) -> {
8484

8585
String inputType = metadata.getInputType();
86-
HalFormsOptions options = optionsFactory.getOptions(payload, metadata);
86+
HalFormsOptions options = optionsFactory.getOptions(payload, metadata, context);
8787

8888
HalFormsProperty property = new HalFormsProperty()
8989
.withName(metadata.getName())

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void rendersInlineOptions() {
182182
List<String> values = Arrays.asList("1234123412341234", "4321432143214321");
183183

184184
HalFormsConfiguration configuration = new HalFormsConfiguration() //
185-
.withOptions(PatternExample.class, "number", metadata -> HalFormsOptions.inline(values));
185+
.withOptions(PatternExample.class, "number", (metadata, context) -> HalFormsOptions.inline(values));
186186

187187
RepresentationModel<?> models = new RepresentationModel<>(
188188
Affordances.of(Link.of("/example", LinkRelation.of("create"))) //
@@ -209,7 +209,7 @@ void propagatesSelectedValueToProperty() {
209209

210210
HalFormsConfiguration configuration = new HalFormsConfiguration() //
211211
.withOptions(PatternExample.class, "number",
212-
metadata -> HalFormsOptions.inline(values).withSelectedValue(selected));
212+
(metadata, context) -> HalFormsOptions.inline(values).withSelectedValue(selected));
213213

214214
RepresentationModel<?> models = new RepresentationModel<>(
215215
Affordances.of(Link.of("/example", LinkRelation.of("create"))) //

0 commit comments

Comments
 (0)