From a6b5c1ec469ed358af00b410c1f924a53da70ee8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 1 Oct 2013 18:24:32 +0200 Subject: [PATCH] DATAREST-113 - Fixed glitches in JSON schema creation. PersistentEntityToJsonSchemaConverter looks up the ResourceMetadata correctly and builds up URIs in a proper way. --- ...PersistentEntityToJsonSchemaConverter.java | 5 ++--- .../webmvc/AbstractWebIntegrationTests.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java index f6e33ac08..63d89261e 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverter.java @@ -3,7 +3,6 @@ package org.springframework.data.rest.webmvc.json; import static org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module.*; import static org.springframework.util.StringUtils.*; -import java.net.URI; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -79,7 +78,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { PersistentEntity persistentEntity = repositories.getPersistentEntity((Class) source); - final ResourceMetadata metadata = mappings.getMappingFor(persistentEntity.getClass()); + final ResourceMetadata metadata = mappings.getMappingFor(persistentEntity.getType()); String entityDesc = persistentEntity.getType().isAnnotationPresent(Description.class) ? persistentEntity.getType() .getAnnotation(Description.class).value() : null; @@ -122,7 +121,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric return; } - RepositoryLinkBuilder builder = new RepositoryLinkBuilder(metadata, URI.create("{id}")); + RepositoryLinkBuilder builder = new RepositoryLinkBuilder(metadata, null).slash("{id}"); maybeAddAssociationLink(builder, mappings, persistentProperty, links); } }); 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 eebb191ea..fabc99e24 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 @@ -168,5 +168,27 @@ public abstract class AbstractWebIntegrationTests { } } + /** + * @see DATAREST-113 + */ + @Test + public void exposesSchemasForResourcesExposed() throws Exception { + + MockHttpServletResponse response = request("/"); + + for (String rel : expectedRootLinkRels()) { + + Link link = assertHasLinkWithRel(rel, response); + + // Resource + request(link); + + // Schema - TODO:Improve by using hypermedia + mvc.perform(get(link.getHref() + "/schema").// + accept(MediaType.parseMediaType("application/schema+json"))).// + andExpect(status().isOk()); + } + } + protected abstract Iterable expectedRootLinkRels(); }