Skip to content

Commit a6c733e

Browse files
committed
Fix spring-projects#968 @JsonUnwrapped ignored with (embedded) CollectionModel and HAL Forms
1 parent c388a85 commit a6c733e

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

src/main/java/org/springframework/hateoas/mediatype/PropertyUtils.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.hateoas.mediatype;
1717

18+
import com.fasterxml.jackson.annotation.JsonUnwrapped;
1819
import lombok.AccessLevel;
1920
import lombok.RequiredArgsConstructor;
2021

@@ -85,6 +86,10 @@ static List<Class<?>> getTypesToUnwrap() {
8586
}
8687

8788
public static Map<String, Object> extractPropertyValues(@Nullable Object object) {
89+
return extractPropertyValues(object, false);
90+
}
91+
92+
public static Map<String, Object> extractPropertyValues(@Nullable Object object, boolean unwrapEligibleProperties) {
8893

8994
if (object == null) {
9095
return Collections.emptyMap();
@@ -98,7 +103,10 @@ public static Map<String, Object> extractPropertyValues(@Nullable Object object)
98103

99104
return getExposedProperties(object.getClass()).stream() //
100105
.map(PropertyMetadata::getName)
101-
.collect(HashMap::new, (map, name) -> map.put(name, wrapper.getPropertyValue(name)), HashMap::putAll);
106+
.map(name -> unwrapEligibleProperties ? unwrapPropertyIfNeeded(name, wrapper) :
107+
Collections.singletonMap(name, wrapper.getPropertyValue(name)))
108+
.flatMap(it -> it.entrySet().stream())
109+
.collect(HashMap::new, (map, it) -> map.put(it.getKey(), it.getValue()), HashMap::putAll);
102110
}
103111

104112
public static <T> T createObjectFromProperties(Class<T> clazz, Map<String, Object> properties) {
@@ -113,7 +121,6 @@ public static <T> T createObjectFromProperties(Class<T> clazz, Map<String, Objec
113121
Method writeMethod = property.getWriteMethod();
114122
ReflectionUtils.makeAccessible(writeMethod);
115123
writeMethod.invoke(obj, value);
116-
117124
} catch (IllegalAccessException | InvocationTargetException e) {
118125

119126
throw new RuntimeException(e);
@@ -151,6 +158,33 @@ public static InputPayloadMetadata getExposedProperties(@Nullable ResolvableType
151158
});
152159
}
153160

161+
private static Map<String, Object> unwrapPropertyIfNeeded(String propertyName, BeanWrapper wrapper) {
162+
Field descriptorField = ReflectionUtils.findField(wrapper.getWrappedClass(), propertyName);
163+
Method readMethod = wrapper.getPropertyDescriptor(propertyName).getReadMethod();
164+
165+
MergedAnnotation<JsonUnwrapped> unwrappedAnnotation =
166+
Stream.of(descriptorField, readMethod)
167+
.filter(Objects::nonNull)
168+
.map(MergedAnnotations::from)
169+
.flatMap(mergedAnnotations -> mergedAnnotations.stream(JsonUnwrapped.class))
170+
.filter(it -> it.getBoolean("enabled"))
171+
.findFirst()
172+
.orElse(null);
173+
174+
Object propertyValue = wrapper.getPropertyValue(propertyName);
175+
if (unwrappedAnnotation == null) {
176+
return Collections.singletonMap(propertyName, propertyValue);
177+
}
178+
179+
String prefix = unwrappedAnnotation.getString("prefix");
180+
String suffix = unwrappedAnnotation.getString("suffix");
181+
182+
Map<String, Object> properties = new HashMap<>();
183+
extractPropertyValues(propertyValue, true)
184+
.forEach((name, value) -> properties.put(prefix + name + suffix, value));
185+
return properties;
186+
}
187+
154188
private static ResolvableType unwrapDomainType(ResolvableType type) {
155189

156190
if (!type.hasGenerics()) {
@@ -169,7 +203,7 @@ private static ResolvableType unwrapDomainType(ResolvableType type) {
169203
* Replaces the given {@link ResolvableType} with the one produced by the given {@link Supplier} if the former is
170204
* assignable from one of the types to be unwrapped.
171205
*
172-
* @param type must not be {@literal null}.
206+
* @param type must not be {@literal null}.
173207
* @param mapper must not be {@literal null}.
174208
* @return
175209
* @see #TYPES_TO_UNWRAP
@@ -188,8 +222,8 @@ private static Stream<PropertyMetadata> lookupExposedProperties(@Nullable Class<
188222
return type == null //
189223
? Stream.empty() //
190224
: getPropertyDescriptors(type) //
191-
.map(it -> new AnnotatedProperty(new Property(type, it.getReadMethod(), it.getWriteMethod())))
192-
.map(it -> JSR_303_PRESENT ? new Jsr303AwarePropertyMetadata(it) : new DefaultPropertyMetadata(it));
225+
.map(it -> new AnnotatedProperty(new Property(type, it.getReadMethod(), it.getWriteMethod())))
226+
.map(it -> JSR_303_PRESENT ? new Jsr303AwarePropertyMetadata(it) : new DefaultPropertyMetadata(it));
193227
}
194228

195229
/**
@@ -359,7 +393,7 @@ public boolean hasWriteMethod() {
359393
/**
360394
* Returns the {@link MergedAnnotation} of the given type.
361395
*
362-
* @param <T> the annotation type.
396+
* @param <T> the annotation type.
363397
* @param type must not be {@literal null}.
364398
* @return the {@link MergedAnnotation} if available or {@link MergedAnnotation#missing()} if not.
365399
*/
@@ -457,7 +491,6 @@ public ResolvableType getType() {
457491
public int compareTo(DefaultPropertyMetadata that) {
458492
return BY_NAME.compare(this, that);
459493
}
460-
461494
}
462495

463496
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private HalFormsDocument() {
108108
*/
109109
public static HalFormsDocument<?> forRepresentationModel(RepresentationModel<?> model) {
110110

111-
Map<String, Object> attributes = PropertyUtils.extractPropertyValues(model);
111+
Map<String, Object> attributes = PropertyUtils.extractPropertyValues(model, true);
112112
attributes.remove("links");
113113

114114
return new HalFormsDocument<>().withAttributes(attributes);

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

Lines changed: 27 additions & 0 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.fasterxml.jackson.annotation.JsonUnwrapped;
2021
import lombok.Getter;
2122
import net.minidev.json.JSONArray;
2223

@@ -516,6 +517,18 @@ void considersJsr303AnnotationsForTemplates() throws Exception {
516517
assertValueForPath(model, "$._templates.default.properties[0].required", true);
517518
}
518519

520+
/**
521+
* @see #968
522+
*/
523+
@Test
524+
void considerJsonUnwrapped() throws Exception {
525+
UnwrappedExample unwrappedExample = new UnwrappedExample();
526+
unwrappedExample.element = new UnwrappedExampleElement();
527+
unwrappedExample.element.firstname = "john";
528+
529+
assertValueForPath(unwrappedExample, "$.firstname", "john");
530+
}
531+
519532
private void assertThatPathDoesNotExist(Object toMarshall, String path) throws Exception {
520533

521534
ObjectMapper mapper = getCuriedObjectMapper();
@@ -621,4 +634,18 @@ public String getFirstname() {
621634
return firstname;
622635
}
623636
}
637+
638+
public static class UnwrappedExample extends RepresentationModel<UnwrappedExample> {
639+
640+
private UnwrappedExampleElement element;
641+
642+
@JsonUnwrapped
643+
public UnwrappedExampleElement getElement(){
644+
return element;
645+
}
646+
}
647+
648+
public static class UnwrappedExampleElement {
649+
private @Getter String firstname;
650+
}
624651
}

0 commit comments

Comments
 (0)