34
34
import org .springframework .data .mongodb .core .mapping .Field ;
35
35
import org .springframework .data .mongodb .core .query .Collation ;
36
36
import org .springframework .data .mongodb .core .schema .IdentifiableJsonSchemaProperty ;
37
+ import org .springframework .data .mongodb .core .schema .IdentifiableJsonSchemaProperty .EncryptedJsonSchemaProperty ;
37
38
import org .springframework .data .mongodb .core .schema .IdentifiableJsonSchemaProperty .QueryableJsonSchemaProperty ;
38
39
import org .springframework .data .mongodb .core .schema .JsonSchemaProperty ;
39
40
import org .springframework .data .mongodb .core .schema .MongoJsonSchema ;
@@ -681,17 +682,17 @@ public static class EncryptedFieldsOptions {
681
682
private static final EncryptedFieldsOptions NONE = new EncryptedFieldsOptions ();
682
683
683
684
private final @ Nullable MongoJsonSchema schema ;
684
- private final List <QueryableJsonSchemaProperty > queryableProperties ;
685
+ private final List <JsonSchemaProperty > properties ;
685
686
686
687
EncryptedFieldsOptions () {
687
688
this (null , List .of ());
688
689
}
689
690
690
691
private EncryptedFieldsOptions (@ Nullable MongoJsonSchema schema ,
691
- List <QueryableJsonSchemaProperty > queryableProperties ) {
692
+ List <JsonSchemaProperty > queryableProperties ) {
692
693
693
694
this .schema = schema ;
694
- this .queryableProperties = queryableProperties ;
695
+ this .properties = queryableProperties ;
695
696
}
696
697
697
698
/**
@@ -731,13 +732,33 @@ public static EncryptedFieldsOptions fromProperties(List<QueryableJsonSchemaProp
731
732
@ CheckReturnValue
732
733
public EncryptedFieldsOptions queryable (JsonSchemaProperty property , QueryCharacteristic ... characteristics ) {
733
734
734
- List <QueryableJsonSchemaProperty > targetPropertyList = new ArrayList <>(queryableProperties .size () + 1 );
735
- targetPropertyList .addAll (queryableProperties );
735
+ List <JsonSchemaProperty > targetPropertyList = new ArrayList <>(properties .size () + 1 );
736
+ targetPropertyList .addAll (properties );
736
737
targetPropertyList .add (JsonSchemaProperty .queryable (property , List .of (characteristics )));
737
738
738
739
return new EncryptedFieldsOptions (schema , targetPropertyList );
739
740
}
740
741
742
+ public EncryptedFieldsOptions with (EncryptedJsonSchemaProperty property ) {
743
+ return encrypted (property , null );
744
+ }
745
+
746
+ public EncryptedFieldsOptions encrypted (JsonSchemaProperty property , @ Nullable Object key ) {
747
+
748
+ List <JsonSchemaProperty > targetPropertyList = new ArrayList <>(properties .size () + 1 );
749
+ targetPropertyList .addAll (properties );
750
+ if (property instanceof IdentifiableJsonSchemaProperty .EncryptedJsonSchemaProperty ) {
751
+ targetPropertyList .add (property );
752
+ } else {
753
+ EncryptedJsonSchemaProperty encryptedJsonSchemaProperty = new EncryptedJsonSchemaProperty (property );
754
+ if (key != null ) {
755
+ targetPropertyList .add (encryptedJsonSchemaProperty .keyId (key ));
756
+ }
757
+ }
758
+
759
+ return new EncryptedFieldsOptions (schema , targetPropertyList );
760
+ }
761
+
741
762
public Document toDocument () {
742
763
return new Document ("fields" , selectPaths ());
743
764
}
@@ -756,20 +777,20 @@ private List<Document> selectPaths() {
756
777
757
778
private List <Document > fromProperties () {
758
779
759
- if (queryableProperties .isEmpty ()) {
780
+ if (properties .isEmpty ()) {
760
781
return List .of ();
761
782
}
762
783
763
- List <Document > converted = new ArrayList <>(queryableProperties .size ());
764
- for (QueryableJsonSchemaProperty property : queryableProperties ) {
784
+ List <Document > converted = new ArrayList <>(properties .size ());
785
+ for (JsonSchemaProperty property : properties ) {
765
786
766
787
Document field = new Document ("path" , property .getIdentifier ());
767
788
768
789
if (!property .getTypes ().isEmpty ()) {
769
790
field .append ("bsonType" , property .getTypes ().iterator ().next ().toBsonType ().value ());
770
791
}
771
792
772
- if (property
793
+ if (property instanceof QueryableJsonSchemaProperty qproperty && qproperty
773
794
.getTargetProperty () instanceof IdentifiableJsonSchemaProperty .EncryptedJsonSchemaProperty encrypted ) {
774
795
if (encrypted .getKeyId () != null ) {
775
796
if (encrypted .getKeyId () instanceof String stringKey ) {
@@ -780,10 +801,21 @@ private List<Document> fromProperties() {
780
801
}
781
802
}
782
803
}
804
+ else if (property instanceof IdentifiableJsonSchemaProperty .EncryptedJsonSchemaProperty encrypted ) {
805
+ if (encrypted .getKeyId () != null ) {
806
+ if (encrypted .getKeyId () instanceof String stringKey ) {
807
+ field .append ("keyId" ,
808
+ new BsonBinary (BsonBinarySubType .UUID_STANDARD , stringKey .getBytes (StandardCharsets .UTF_8 )));
809
+ } else {
810
+ field .append ("keyId" , encrypted .getKeyId ());
811
+ }
812
+ }
813
+ }
783
814
784
- field .append ("queries" , StreamSupport .stream (property .getCharacteristics ().spliterator (), false )
815
+ if (property instanceof QueryableJsonSchemaProperty qproperty ) {
816
+ field .append ("queries" , StreamSupport .stream (qproperty .getCharacteristics ().spliterator (), false )
785
817
.map (QueryCharacteristic ::toDocument ).toList ());
786
-
818
+ }
787
819
if (!field .containsKey ("keyId" )) {
788
820
field .append ("keyId" , BsonNull .VALUE );
789
821
}
@@ -812,7 +844,9 @@ private List<Document> fromSchema() {
812
844
if (entry .getValue ().containsKey ("bsonType" )) {
813
845
field .append ("bsonType" , entry .getValue ().get ("bsonType" ));
814
846
}
815
- field .put ("queries" , entry .getValue ().get ("queries" ));
847
+ if (entry .getValue ().containsKey ("queries" )) {
848
+ field .put ("queries" , entry .getValue ().get ("queries" ));
849
+ }
816
850
fields .add (field );
817
851
}
818
852
}
0 commit comments