Skip to content

Commit

Permalink
Fix schema generation for encrypted fields that are considered domain…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
christophstrobl authored and sxhinzvc committed Aug 8, 2023
1 parent c532ec3 commit b6cd129
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ private JsonSchemaProperty computeSchemaForProperty(List<MongoPersistentProperty
target.properties(nestedProperties.toArray(new JsonSchemaProperty[0])), required));
}
}
return targetProperties.size() == 1 ? targetProperties.iterator().next()
JsonSchemaProperty schemaProperty = targetProperties.size() == 1 ? targetProperties.iterator().next()
: JsonSchemaProperty.merged(targetProperties);
return applyEncryptionDataIfNecessary(property, schemaProperty);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

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

enc.append("algorithm", algorithm);
if(StringUtils.hasText(algorithm)) {
enc.append("algorithm", algorithm);
}

propertySpecification.append("encrypt", enc);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ void bsonTypeVsJustTypeValueResolutionIsDoneByDefault() {
.containsEntry("properties.value", new Document("type", "string"));
}

@Test // GH-4454
void wrapEncryptedEntityTypeLikeProperty() {

MongoJsonSchema schema = MongoJsonSchemaCreator.create() //
.filter(MongoJsonSchemaCreator.encryptedOnly()) // filter non encrypted fields
.createSchemaFor(WithEncryptedEntityLikeProperty.class);

assertThat(schema.schemaDocument()) //
.containsEntry("properties.domainTypeValue", Document.parse("{'encrypt': {'bsonType': 'object' } }"));
}

// --> TYPES AND JSON

// --> ENUM
Expand Down Expand Up @@ -676,4 +687,9 @@ static class C extends A {
static class PropertyClashWithA {
Integer aNonEncrypted;
}

@Encrypted(algorithm = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic")
static class WithEncryptedEntityLikeProperty {
@Encrypted SomeDomainType domainTypeValue;
}
}

0 comments on commit b6cd129

Please sign in to comment.