Skip to content

Commit 08813a7

Browse files
christophstroblsxhinzvc
authored andcommitted
Fix schema generation for encrypted fields that are considered domain entities.
This commit makes sure to consider the encrypted annotation on fields that are considered domain type property values, encrypting the entire object if necessary.
1 parent c1b760c commit 08813a7

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ private JsonSchemaProperty computeSchemaForProperty(List<MongoPersistentProperty
203203
target.properties(nestedProperties.toArray(new JsonSchemaProperty[0])), required));
204204
}
205205
}
206-
return targetProperties.size() == 1 ? targetProperties.iterator().next()
206+
JsonSchemaProperty schemaProperty = targetProperties.size() == 1 ? targetProperties.iterator().next()
207207
: JsonSchemaProperty.merged(targetProperties);
208+
return applyEncryptionDataIfNecessary(property, schemaProperty);
208209
}
209210
}
210211

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/IdentifiableJsonSchemaProperty.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.lang.Nullable;
3737
import org.springframework.util.Assert;
3838
import org.springframework.util.ObjectUtils;
39+
import org.springframework.util.StringUtils;
3940

4041
/**
4142
* {@link JsonSchemaProperty} implementation.
@@ -1139,7 +1140,9 @@ public Document toDocument() {
11391140
enc.append("bsonType", type.toBsonType().value()); // TODO: no samples with type -> is it bson type all the way?
11401141
}
11411142

1142-
enc.append("algorithm", algorithm);
1143+
if(StringUtils.hasText(algorithm)) {
1144+
enc.append("algorithm", algorithm);
1145+
}
11431146

11441147
propertySpecification.append("encrypt", enc);
11451148

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreatorUnitTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ void bsonTypeVsJustTypeValueResolutionIsDoneByDefault() {
271271
.containsEntry("properties.value", new Document("type", "string"));
272272
}
273273

274+
@Test // GH-4454
275+
void wrapEncryptedEntityTypeLikeProperty() {
276+
277+
MongoJsonSchema schema = MongoJsonSchemaCreator.create() //
278+
.filter(MongoJsonSchemaCreator.encryptedOnly()) // filter non encrypted fields
279+
.createSchemaFor(WithEncryptedEntityLikeProperty.class);
280+
281+
assertThat(schema.schemaDocument()) //
282+
.containsEntry("properties.domainTypeValue", Document.parse("{'encrypt': {'bsonType': 'object' } }"));
283+
}
284+
274285
// --> TYPES AND JSON
275286

276287
// --> ENUM
@@ -676,4 +687,9 @@ static class C extends A {
676687
static class PropertyClashWithA {
677688
Integer aNonEncrypted;
678689
}
690+
691+
@Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
692+
static class WithEncryptedEntityLikeProperty {
693+
@Encrypted SomeDomainType domainTypeValue;
694+
}
679695
}

0 commit comments

Comments
 (0)