Skip to content

Commit 5c12963

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 0b8a3cf commit 5c12963

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
@@ -207,8 +207,9 @@ private JsonSchemaProperty computeSchemaForProperty(List<MongoPersistentProperty
207207
target.properties(nestedProperties.toArray(new JsonSchemaProperty[0])), required));
208208
}
209209
}
210-
return targetProperties.size() == 1 ? targetProperties.iterator().next()
210+
JsonSchemaProperty schemaProperty = targetProperties.size() == 1 ? targetProperties.iterator().next()
211211
: JsonSchemaProperty.merged(targetProperties);
212+
return applyEncryptionDataIfNecessary(property, schemaProperty);
212213
}
213214
}
214215

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.
@@ -1171,7 +1172,9 @@ public Document toDocument() {
11711172
enc.append("bsonType", type.toBsonType().value()); // TODO: no samples with type -> is it bson type all the way?
11721173
}
11731174

1174-
enc.append("algorithm", algorithm);
1175+
if(StringUtils.hasText(algorithm)) {
1176+
enc.append("algorithm", algorithm);
1177+
}
11751178

11761179
propertySpecification.append("encrypt", enc);
11771180

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
@@ -684,4 +695,9 @@ static class C extends A {
684695
static class PropertyClashWithA {
685696
Integer aNonEncrypted;
686697
}
698+
699+
@Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
700+
static class WithEncryptedEntityLikeProperty {
701+
@Encrypted SomeDomainType domainTypeValue;
702+
}
687703
}

0 commit comments

Comments
 (0)