From ae27af3d167f64a988146bdc5d38c2029b83600a Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 30 Aug 2021 07:41:05 +0200 Subject: [PATCH] allow algorithm on top level - maybe we should just have the encrypted annotation --- .../mongodb/core/MappingMongoJsonSchemaCreator.java | 11 ++++++++--- .../data/mongodb/core/mapping/Encrypted.java | 2 ++ .../data/mongodb/core/mapping/EncryptedField.java | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreator.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreator.java index 6b1f42e38..821e171b8 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreator.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreator.java @@ -55,6 +55,7 @@ import org.springframework.util.Base64Utils; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; /** * {@link MongoJsonSchemaCreator} implementation using both {@link MongoConverter} and {@link MappingContext} to obtain @@ -96,7 +97,11 @@ class MappingMongoJsonSchemaCreator implements MongoJsonSchemaCreator { if (entity.isAnnotationPresent(Encrypted.class)) { Encrypted encrypted = entity.findAnnotation(Encrypted.class); - schemaBuilder.encryptionMetadata(new Document("keyId", stringToKeyId(encrypted.keyId()))); + Document encryptionMetadata = new Document("keyId", stringToKeyId(encrypted.keyId())); + if(StringUtils.hasText(encrypted.algorithm())) { + encryptionMetadata.append("algorithm", encrypted.algorithm()); + } + schemaBuilder.encryptionMetadata(encryptionMetadata); } List schemaProperties = computePropertiesForEntity(Collections.emptyList(), entity, encyptedFieldsOnly); @@ -229,10 +234,10 @@ class MappingMongoJsonSchemaCreator implements MongoJsonSchemaCreator { schemaProperty = createSchemaProperty(fieldName, targetType, required); } - if (property.findAnnotation(EncryptedField.class) != null) { + if (property.findAnnotation(Encrypted.class) != null) { EncryptedJsonSchemaProperty enc = new EncryptedJsonSchemaProperty(schemaProperty); - EncryptedField annotation = property.findAnnotation(EncryptedField.class); + Encrypted annotation = property.findAnnotation(Encrypted.class); enc = enc.algorithm(annotation.algorithm()); if (!ObjectUtils.isEmpty(annotation.keyId())) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Encrypted.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Encrypted.java index 7e5ad58f4..53a38bfcd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Encrypted.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/Encrypted.java @@ -31,4 +31,6 @@ import java.lang.annotation.Target; public @interface Encrypted { String[] keyId() default {}; + + String algorithm() default ""; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/EncryptedField.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/EncryptedField.java index 9798bc95f..e65683a21 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/EncryptedField.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/EncryptedField.java @@ -87,5 +87,6 @@ public @interface EncryptedField { @AliasFor(annotation = Encrypted.class, attribute = "keyId") String[] keyId() default {}; - String algorithm(); + @AliasFor(annotation = Encrypted.class, attribute = "algorithm") + String algorithm() default ""; }