allow algorithm on top level - maybe we should just have the encrypted annotation

This commit is contained in:
Christoph Strobl
2021-08-30 07:41:05 +02:00
parent e7308cd806
commit ae27af3d16
3 changed files with 12 additions and 4 deletions

View File

@@ -55,6 +55,7 @@ import org.springframework.util.Base64Utils;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/** /**
* {@link MongoJsonSchemaCreator} implementation using both {@link MongoConverter} and {@link MappingContext} to obtain * {@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)) { if (entity.isAnnotationPresent(Encrypted.class)) {
Encrypted encrypted = entity.findAnnotation(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<JsonSchemaProperty> schemaProperties = computePropertiesForEntity(Collections.emptyList(), entity, encyptedFieldsOnly); List<JsonSchemaProperty> schemaProperties = computePropertiesForEntity(Collections.emptyList(), entity, encyptedFieldsOnly);
@@ -229,10 +234,10 @@ class MappingMongoJsonSchemaCreator implements MongoJsonSchemaCreator {
schemaProperty = createSchemaProperty(fieldName, targetType, required); schemaProperty = createSchemaProperty(fieldName, targetType, required);
} }
if (property.findAnnotation(EncryptedField.class) != null) { if (property.findAnnotation(Encrypted.class) != null) {
EncryptedJsonSchemaProperty enc = new EncryptedJsonSchemaProperty(schemaProperty); EncryptedJsonSchemaProperty enc = new EncryptedJsonSchemaProperty(schemaProperty);
EncryptedField annotation = property.findAnnotation(EncryptedField.class); Encrypted annotation = property.findAnnotation(Encrypted.class);
enc = enc.algorithm(annotation.algorithm()); enc = enc.algorithm(annotation.algorithm());
if (!ObjectUtils.isEmpty(annotation.keyId())) { if (!ObjectUtils.isEmpty(annotation.keyId())) {

View File

@@ -31,4 +31,6 @@ import java.lang.annotation.Target;
public @interface Encrypted { public @interface Encrypted {
String[] keyId() default {}; String[] keyId() default {};
String algorithm() default "";
} }

View File

@@ -87,5 +87,6 @@ public @interface EncryptedField {
@AliasFor(annotation = Encrypted.class, attribute = "keyId") @AliasFor(annotation = Encrypted.class, attribute = "keyId")
String[] keyId() default {}; String[] keyId() default {};
String algorithm(); @AliasFor(annotation = Encrypted.class, attribute = "algorithm")
String algorithm() default "";
} }