From 57c503ea85beb644be07269bdb3aa6bab221a418 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 9 Nov 2015 08:29:18 +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 | 11 ++++++++++- .../json/PersistentEntityToJsonSchemaConverter.java | 11 ++++++----- ...ersistentEntityToJsonSchemaConverterUnitTests.java | 4 ++++ 3 files changed, 20 insertions(+), 6 deletions(-) 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 a47df0fc7..fd837b7c2 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 @@ -340,7 +340,7 @@ public class JsonSchema { return with(JsonSchemaFormat.DATE_TIME); } - if (type.isCollectionLike()) { + if (type.isCollectionLike() && !JsonSchemaFormat.URI.equals(format)) { if (Set.class.equals(type.getType())) { this.uniqueItems = true; @@ -353,10 +353,19 @@ public class JsonSchema { } Property with(JsonSchemaFormat format) { + this.format = format; return with(STRING_TYPE_INFORMATION); } + Property asAssociation() { + + this.items = null; + this.uniqueItems = null; + + return with(JsonSchemaFormat.URI); + } + Property with(Pattern pattern) { this.pattern = pattern.toString(); return with(STRING_TYPE_INFORMATION); 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 2e014f1ff..dde11702d 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 @@ -162,8 +162,9 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric for (BeanPropertyDefinition definition : jackson) { PersistentProperty persistentProperty = entity.getPersistentProperty(definition.getInternalName()); - TypeInformation propertyType = persistentProperty == null ? ClassTypeInformation.from(definition - .getPrimaryMember().getRawType()) : persistentProperty.getTypeInformation(); + TypeInformation propertyType = persistentProperty == null + ? ClassTypeInformation.from(definition.getPrimaryMember().getRawType()) + : persistentProperty.getTypeInformation(); Class rawPropertyType = propertyType.getType(); JsonSchemaFormat format = configuration.metadataConfiguration().getSchemaFormatFor(rawPropertyType); @@ -204,7 +205,7 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric } if (associationLinks.isLinkableAssociation(persistentProperty)) { - properties.add(property.with(JsonSchemaFormat.URI)); + properties.add(property.asAssociation()); } else { if (persistentProperty.isEntity()) { @@ -252,8 +253,8 @@ public class PersistentEntityToJsonSchemaConverter implements ConditionalGeneric return new Property(name, resolvedDescription, required); } - return new EnumProperty(name, rawType, description.getDefaultMessage().equals(resolvedDescription) ? null - : resolvedDescription, required); + return new EnumProperty(name, rawType, + description.getDefaultMessage().equals(resolvedDescription) ? null : resolvedDescription, required); } private ResourceDescription getDescriptionFor(PersistentProperty property, ResourceMetadata metadata) { 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 32f1e4497..3aa573e18 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 @@ -139,6 +139,10 @@ public class PersistentEntityToJsonSchemaConverterUnitTests { // DATAREST-531 constraints.add(new Constraint("$.properties.email.readOnly", is(true), "Email is read-only property")); + // DATAREST-690 + constraints.add(new Constraint("$.properties.colleagues.items", is(nullValue()), + "Items must not appear for collection associations.")); + assertConstraints(User.class, constraints); }