Skip to content

Render items for collection like properties when deriving jsonSchema. #3837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3766-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3766-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3766-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3766-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.Encrypted;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.ArrayJsonSchemaProperty;
import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.EncryptedJsonSchemaProperty;
import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.ObjectJsonSchemaProperty;
import org.springframework.data.mongodb.core.schema.JsonSchemaObject;
Expand Down Expand Up @@ -160,15 +160,15 @@ private JsonSchemaProperty computeSchemaForProperty(List<MongoPersistentProperty
Class<?> rawTargetType = computeTargetType(property); // target type before conversion
Class<?> targetType = converter.getTypeMapper().getWriteTargetTypeFor(rawTargetType); // conversion target type

if (property.isEntity() && ObjectUtils.nullSafeEquals(rawTargetType, targetType)) {
if (!isCollection(property) && property.isEntity() && ObjectUtils.nullSafeEquals(rawTargetType, targetType)) {
return createObjectSchemaPropertyForEntity(path, property, required);
}

String fieldName = computePropertyFieldName(property);

JsonSchemaProperty schemaProperty;
if (property.isCollectionLike()) {
schemaProperty = createSchemaProperty(fieldName, targetType, required);
if (isCollection(property)) {
schemaProperty = createSchemaPropertyForCollection(fieldName, property, required);
} else if (property.isMap()) {
schemaProperty = createSchemaProperty(fieldName, Type.objectType(), required);
} else if (ClassUtils.isAssignable(Enum.class, targetType)) {
Expand All @@ -180,6 +180,48 @@ private JsonSchemaProperty computeSchemaForProperty(List<MongoPersistentProperty
return applyEncryptionDataIfNecessary(property, schemaProperty);
}

private JsonSchemaProperty createSchemaPropertyForCollection(String fieldName, MongoPersistentProperty property,
boolean required) {

ArrayJsonSchemaProperty schemaProperty = JsonSchemaProperty.array(fieldName);

if (property.getActualType() != Object.class) {

MongoPersistentEntity<?> persistentEntity = mappingContext
.getPersistentEntity(property.getTypeInformation().getComponentType());

if (persistentEntity == null) {

if (ClassUtils.isAssignable(Enum.class, property.getActualType())) {

List<Object> possibleValues = new ArrayList<>();

for (Object enumValue : EnumSet.allOf((Class) property.getActualType())) {
possibleValues.add(converter.convertToMongoType(enumValue));
}

Class targetType = possibleValues.isEmpty() ? property.getActualType()
: possibleValues.iterator().next().getClass();
schemaProperty = schemaProperty
.items(Collections.singleton(JsonSchemaObject.of(targetType).possibleValues(possibleValues)));
} else {
schemaProperty = schemaProperty.items(Collections.singleton(JsonSchemaObject.of(property.getActualType())));
}
} else {

List<JsonSchemaProperty> nestedProperties = computePropertiesForEntity(Collections.emptyList(),
persistentEntity);

if (!nestedProperties.isEmpty()) {
schemaProperty = schemaProperty.items(Collections
.singleton(JsonSchemaObject.object().properties(nestedProperties.toArray(new JsonSchemaProperty[0]))));
}
}
}

return createPotentiallyRequiredSchemaProperty(schemaProperty, required);
}

@Nullable
private JsonSchemaProperty applyEncryptionDataIfNecessary(MongoPersistentProperty property,
JsonSchemaProperty schemaProperty) {
Expand All @@ -197,7 +239,6 @@ private JsonSchemaProperty applyEncryptionDataIfNecessary(MongoPersistentPropert
enc = enc.keys(property.getEncryptionKeyIds());
}
return enc;

}

private JsonSchemaProperty createObjectSchemaPropertyForEntity(List<MongoPersistentProperty> path,
Expand Down Expand Up @@ -268,6 +309,10 @@ private Class<?> computeTargetType(PersistentProperty<?> property) {
return mongoProperty.getFieldType() != mongoProperty.getActualType() ? Object.class : mongoProperty.getFieldType();
}

private static boolean isCollection(MongoPersistentProperty property) {
return property.isCollectionLike() && !property.getType().equals(byte[].class);
}

static JsonSchemaProperty createPotentiallyRequiredSchemaProperty(JsonSchemaProperty property, boolean required) {

if (!required) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ enum JustSomeEnum {
" 'arrayProperty' : { 'type' : 'array' }," + //
" 'binaryDataProperty' : { 'bsonType' : 'binData' }," + //
" 'collectionProperty' : { 'type' : 'array' }," + //
" 'simpleTypeCollectionProperty' : { 'type' : 'array', 'items' : { 'type' : 'string' } }," + //
" 'complexTypeCollectionProperty' : { 'type' : 'array', 'items' : { 'type' : 'object', 'properties' : { 'field' : { 'type' : 'string'} } } }" + //
" 'enumTypeCollectionProperty' : { 'type' : 'array', 'items' : " + JUST_SOME_ENUM + " }" + //
" 'mapProperty' : { 'type' : 'object' }," + //
" 'objectProperty' : { 'type' : 'object' }," + //
" 'enumProperty' : " + JUST_SOME_ENUM + " }" + //
Expand All @@ -203,12 +206,19 @@ static class VariousFieldTypes {
Date dateProperty;
Object[] arrayProperty;
byte[] binaryDataProperty;
List<String> collectionProperty;
List<Object> collectionProperty;
List<String> simpleTypeCollectionProperty;
List<SomeDomainType> complexTypeCollectionProperty;
List<JustSomeEnum> enumTypeCollectionProperty;
Map<String, String> mapProperty;
Object objectProperty;
JustSomeEnum enumProperty;
}

static class SomeDomainType {
String field;
}

// --> NESTED DOMAIN TYPE

static final String WITH_NESTED_DOMAIN_TYPE = "" + //
Expand Down