From 44fda4f8b74666378f50fff1484e51a39d06cfe1 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 21 Feb 2014 11:53:18 +0100 Subject: [PATCH] DATAREST-250 - Fixed exposure of inline properties. Fixed PersistentEntityResource serialization to correctly export associations that are not backed by an exported repository. --- .../json/PersistentEntityJackson2Module.java | 24 ++++--------------- .../webmvc/AbstractWebIntegrationTests.java | 8 +++++-- .../data/rest/webmvc/util/TestUtils.java | 2 ++ 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java index 4a4ffd7ad..7e83c76e5 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityJackson2Module.java @@ -190,10 +190,7 @@ public class PersistentEntityJackson2Module extends SimpleModule { public void doWithAssociation(Association> association) { PersistentProperty property = association.getInverse(); - - if (maybeAddAssociationLink(builder, mappings, property, links)) { - return; - } + maybeAddAssociationLink(builder, mappings, property, links); } }); @@ -250,23 +247,12 @@ public class PersistentEntityJackson2Module extends SimpleModule { PersistentProperty persistentProperty = entity.getPersistentProperty(writer.getName()); - if (persistentProperty.isAssociation()) { - - if (!resourceMetadata.isManagedResource(persistentProperty)) { - continue; - } - - if (mappings.getMappingFor(persistentProperty.getActualType()).isExported()) { - continue; - } - - ResourceMapping propertyMapping = resourceMetadata.getMappingFor(persistentProperty); - - if (!propertyMapping.isExported()) { - continue; - } + // Skip exported associations + if (persistentProperty.isAssociation() && resourceMetadata.isExported(persistentProperty)) { + continue; } + // Skip ids unless explicitly configured to expose if (persistentProperty.isIdProperty() && !configuration.isIdExposedFor(entity.getType())) { continue; } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java index 4741fcc4a..e4be39458 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java @@ -259,6 +259,11 @@ public abstract class AbstractWebIntegrationTests { Object jsonPathResult = JsonPath.read(response.getContentAsString(), path); assertThat(jsonPathResult, is(notNullValue())); + if (jsonPathResult instanceof JSONArray) { + JSONArray array = (JSONArray) jsonPathResult; + assertThat(array, hasSize(greaterThan(0))); + } + return (T) jsonPathResult; } @@ -267,8 +272,7 @@ public abstract class AbstractWebIntegrationTests { try { JsonPath.read(response.getContentAsString(), path); fail(path + " should have failed"); - } catch (InvalidPathException e) { - } + } catch (InvalidPathException e) {} } protected String assertJsonPathEquals(String path, String expected, MockHttpServletResponse response) diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/util/TestUtils.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/util/TestUtils.java index c8c0513c9..745d9e0e5 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/util/TestUtils.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/util/TestUtils.java @@ -21,6 +21,8 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.data.rest.webmvc.jpa.JpaWebTests; /** + * Test helper methods. + * * @author Oliver Gierke */ public class TestUtils {