#158 - HalEmbeddedBuilder now uses AopUtils.getTargetClass().
To correctly resolve the rel for a proxied class we need to build up relation types for, HalEmbeddedBuilder now uses Spring's AopUtils.getTargetClass(), which will use the object's getTargetClass() method in case it implements TargetClassAware.
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package org.springframework.hateoas.core;
|
||||
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.hateoas.Resource;
|
||||
|
||||
/**
|
||||
@@ -32,25 +31,12 @@ public class ObjectUtils {
|
||||
* @param object can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static Class<?> getResourceType(Object object) {
|
||||
public static Object getResourceType(Object object) {
|
||||
|
||||
if (object instanceof Resource) {
|
||||
return nullSafeType(((Resource<?>) object).getContent());
|
||||
return ((Resource<?>) object).getContent();
|
||||
}
|
||||
|
||||
return nullSafeType(object);
|
||||
}
|
||||
|
||||
private static Class<?> nullSafeType(Object object) {
|
||||
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (object instanceof Advised) {
|
||||
return ((Advised) object).getTargetClass();
|
||||
}
|
||||
|
||||
return object.getClass();
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.hateoas.RelProvider;
|
||||
import org.springframework.hateoas.Resource;
|
||||
import org.springframework.hateoas.core.ObjectUtils;
|
||||
@@ -58,16 +59,16 @@ class HalEmbeddedBuilder {
|
||||
*/
|
||||
public void add(Object value) {
|
||||
|
||||
Class<?> type = ObjectUtils.getResourceType(value);
|
||||
Object unwrapped = ObjectUtils.getResourceType(value);
|
||||
|
||||
if (type == null) {
|
||||
if (unwrapped == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String rel = getDefaultedRelFor(type, true);
|
||||
String rel = getDefaultedRelFor(unwrapped, true);
|
||||
|
||||
if (!embeddeds.containsKey(rel)) {
|
||||
rel = getDefaultedRelFor(type, enforceCollections);
|
||||
rel = getDefaultedRelFor(unwrapped, enforceCollections);
|
||||
}
|
||||
|
||||
List<Object> currentValue = embeddeds.get(rel);
|
||||
@@ -79,18 +80,20 @@ class HalEmbeddedBuilder {
|
||||
} else if (currentValue.size() == 1) {
|
||||
currentValue.add(value);
|
||||
embeddeds.remove(rel);
|
||||
embeddeds.put(getDefaultedRelFor(type, true), currentValue);
|
||||
embeddeds.put(getDefaultedRelFor(unwrapped, true), currentValue);
|
||||
} else {
|
||||
currentValue.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
private String getDefaultedRelFor(Class<?> type, boolean forCollection) {
|
||||
private String getDefaultedRelFor(Object value, boolean forCollection) {
|
||||
|
||||
if (provider == null) {
|
||||
return DEFAULT_REL;
|
||||
}
|
||||
|
||||
Class<?> type = AopUtils.getTargetClass(value);
|
||||
|
||||
String rel = forCollection ? provider.getCollectionResourceRelFor(type) : provider.getItemResourceRelFor(type);
|
||||
return rel == null ? DEFAULT_REL : rel;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user