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.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user