From bb612e0ea0b62bfa368229f12541a7560f857f2d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 9 Nov 2015 08:36:14 +0100 Subject: [PATCH] DATAREST-690 - Fixed JSON schema handling for collection associations. We now make sure not to render an items nor an uniqueItems clause for collection associations as they are exposed as URIs for which JSON schema doesn't allow these attributes. --- .../data/rest/webmvc/json/JsonSchema.java | 13 +++++++++++++ .../json/PersistentEntityToJsonSchemaConverter.java | 2 +- ...sistentEntityToJsonSchemaConverterUnitTests.java | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JsonSchema.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JsonSchema.java index 6bfd0d423..52521855b 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JsonSchema.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/JsonSchema.java @@ -420,6 +420,19 @@ public class JsonSchema { return with(STRING_TYPE_INFORMATION); } + /** + * Turns the current {@link JsonSchemaProperty} into an association. + * + * @return + */ + public JsonSchemaProperty asAssociation() { + + this.items = null; + this.uniqueItems = null; + + return withFormat(JsonSchemaFormat.URI); + } + JsonSchemaProperty with(TypeInformation type, String reference) { if (type.isCollectionLike()) { 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 f51b7b120..4d02b0d8e 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 @@ -230,7 +230,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric } if (associationLinks.isLinkableAssociation(persistentProperty)) { - registrar.register(property.withFormat(JsonSchemaFormat.URI), null); + registrar.register(property.asAssociation(), null); } else { if (persistentProperty.isEntity()) { diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java index 57177e23f..8579a3917 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/PersistentEntityToJsonSchemaConverterUnitTests.java @@ -152,6 +152,10 @@ public class PersistentEntityToJsonSchemaConverterUnitTests { constraints.add( new Constraint("$.properties.firstname.title", is("Vorname"), "I18n from property on fully-qualified type")); + // DATAREST-690 + constraints.add(new Constraint("$.properties.colleagues.items", is(nullValue()), + "Items must not appear for collection associations.")); + assertConstraints(User.class, constraints); }