From e3dde1a2cd9c3ad7fed75aef85b2571b3f14614a Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Wed, 5 Sep 2012 08:57:43 -0500 Subject: [PATCH] DATAREST-39 Introduced new property on AttributeMetadata to encapsulate the decision of whether or not a property is nullable. Removed references to JPA-specific classes from everything but the JpaAttributeMetadata class. --- .../data/rest/repository/AttributeMetadata.java | 7 +++++++ .../rest/repository/jpa/JpaAttributeMetadata.java | 14 ++++++++++++++ .../data/rest/webmvc/RepositoryRestController.java | 8 ++------ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/AttributeMetadata.java b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/AttributeMetadata.java index 2d2823c4e..404b2d435 100644 --- a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/AttributeMetadata.java +++ b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/AttributeMetadata.java @@ -40,6 +40,13 @@ public interface AttributeMetadata { */ Class elementType(); + /** + * Whether this attribute can be nulled or not. + * + * @return + */ + boolean isNullable(); + /** * Can this attribute look like a {@link Collection}? * diff --git a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaAttributeMetadata.java b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaAttributeMetadata.java index 8b6ebb064..5633639de 100644 --- a/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaAttributeMetadata.java +++ b/spring-data-rest-repository/src/main/java/org/springframework/data/rest/repository/jpa/JpaAttributeMetadata.java @@ -7,6 +7,8 @@ import java.lang.reflect.Method; import java.util.Collection; import java.util.Map; import java.util.Set; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; import javax.persistence.metamodel.Attribute; import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.MapAttribute; @@ -72,6 +74,18 @@ public class JpaAttributeMetadata implements AttributeMetadata { : null); } + @Override public boolean isNullable() { + if(hasAnnotation(ManyToOne.class)) { + return annotation(ManyToOne.class).optional(); + } + + if(hasAnnotation(OneToOne.class)) { + return annotation(OneToOne.class).optional(); + } + + return true; + } + @Override public boolean isCollectionLike() { if(attribute instanceof PluralAttribute) { PluralAttribute plattr = (PluralAttribute)attribute; diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java index 2390280cc..51c47c249 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java @@ -21,8 +21,6 @@ import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicReference; -import javax.persistence.ManyToOne; -import javax.persistence.OneToOne; import javax.servlet.http.HttpServletRequest; import org.codehaus.jackson.map.ObjectMapper; @@ -1301,8 +1299,7 @@ public class RepositoryRestController } // Check if this is a @*ToOne relationship and is optional and if not, fail with a 405 Method Not Allowed - if((attrMeta.hasAnnotation(ManyToOne.class) && !attrMeta.annotation(ManyToOne.class).optional()) - || (attrMeta.hasAnnotation(OneToOne.class) && !attrMeta.annotation(OneToOne.class).optional())) { + if(!attrMeta.isNullable()) { return negotiateResponse(request, HttpStatus.METHOD_NOT_ALLOWED, new HttpHeaders(), null); } @@ -1450,8 +1447,7 @@ public class RepositoryRestController } // Check if this @*ToOne relationship is optional and if not, fail with a 405 Method Not Allowed - if((attrMeta.hasAnnotation(ManyToOne.class) && !attrMeta.annotation(ManyToOne.class).optional()) - || (attrMeta.hasAnnotation(OneToOne.class) && !attrMeta.annotation(OneToOne.class).optional())) { + if(!attrMeta.isNullable()) { return negotiateResponse(request, HttpStatus.METHOD_NOT_ALLOWED, new HttpHeaders(), null); }