24
24
import java .util .Map .Entry ;
25
25
import java .util .Optional ;
26
26
import java .util .function .Function ;
27
- import java .util .stream .Collectors ;
28
27
29
28
import org .bson .BsonBinary ;
30
29
import org .bson .BsonBinarySubType ;
31
30
import org .bson .BsonNull ;
32
31
import org .bson .Document ;
32
+
33
33
import org .springframework .data .mongodb .core .mapping .Field ;
34
34
import org .springframework .data .mongodb .core .query .Collation ;
35
35
import org .springframework .data .mongodb .core .schema .IdentifiableJsonSchemaProperty ;
43
43
import org .springframework .data .util .Optionals ;
44
44
import org .springframework .lang .CheckReturnValue ;
45
45
import org .springframework .lang .Contract ;
46
- import org .springframework .lang .NonNull ;
47
46
import org .springframework .lang .Nullable ;
48
47
import org .springframework .util .Assert ;
49
48
import org .springframework .util .ObjectUtils ;
@@ -241,11 +240,11 @@ public CollectionOptions collation(@Nullable Collation collation) {
241
240
* Create new {@link CollectionOptions} with already given settings and {@code validationOptions} set to given
242
241
* {@link MongoJsonSchema}.
243
242
*
244
- * @param schema can be {@literal null}.
243
+ * @param schema must not be {@literal null}.
245
244
* @return new {@link CollectionOptions}.
246
245
* @since 2.1
247
246
*/
248
- public CollectionOptions schema (@ Nullable MongoJsonSchema schema ) {
247
+ public CollectionOptions schema (MongoJsonSchema schema ) {
249
248
return validator (Validator .schema (schema ));
250
249
}
251
250
@@ -473,7 +472,7 @@ public Optional<CollectionChangeStreamOptions> getChangeStreamOptions() {
473
472
* Get the {@code encryptedFields} if available.
474
473
*
475
474
* @return {@link Optional#empty()} if not specified.
476
- * @since 4.5.0
475
+ * @since 4.5
477
476
*/
478
477
public Optional <EncryptedFieldsOptions > getEncryptedFieldsOptions () {
479
478
return Optional .ofNullable (encryptedFieldsOptions );
@@ -552,7 +551,8 @@ public static class ValidationOptions {
552
551
private final @ Nullable ValidationLevel validationLevel ;
553
552
private final @ Nullable ValidationAction validationAction ;
554
553
555
- public ValidationOptions (Validator validator , ValidationLevel validationLevel , ValidationAction validationAction ) {
554
+ public ValidationOptions (@ Nullable Validator validator , @ Nullable ValidationLevel validationLevel ,
555
+ @ Nullable ValidationAction validationAction ) {
556
556
557
557
this .validator = validator ;
558
558
this .validationLevel = validationLevel ;
@@ -669,16 +669,27 @@ public int hashCode() {
669
669
670
670
/**
671
671
* Encapsulation of Encryption options for collections.
672
- *
672
+ *
673
673
* @author Christoph Strobl
674
674
* @since 4.5
675
675
*/
676
676
public static class EncryptedFieldsOptions {
677
677
678
678
private static final EncryptedFieldsOptions NONE = new EncryptedFieldsOptions ();
679
679
680
- private @ Nullable MongoJsonSchema schema ;
681
- private List <QueryableJsonSchemaProperty > queryableProperties ;
680
+ private final @ Nullable MongoJsonSchema schema ;
681
+ private final List <QueryableJsonSchemaProperty > queryableProperties ;
682
+
683
+ EncryptedFieldsOptions () {
684
+ this (null , List .of ());
685
+ }
686
+
687
+ private EncryptedFieldsOptions (@ Nullable MongoJsonSchema schema ,
688
+ List <QueryableJsonSchemaProperty > queryableProperties ) {
689
+
690
+ this .schema = schema ;
691
+ this .queryableProperties = queryableProperties ;
692
+ }
682
693
683
694
/**
684
695
* @return {@link EncryptedFieldsOptions#NONE}
@@ -701,17 +712,6 @@ public static EncryptedFieldsOptions fromProperties(List<QueryableJsonSchemaProp
701
712
return new EncryptedFieldsOptions (null , List .copyOf (properties ));
702
713
}
703
714
704
- EncryptedFieldsOptions () {
705
- this (null , List .of ());
706
- }
707
-
708
- private EncryptedFieldsOptions (@ Nullable MongoJsonSchema schema ,
709
- List <QueryableJsonSchemaProperty > queryableProperties ) {
710
-
711
- this .schema = schema ;
712
- this .queryableProperties = queryableProperties ;
713
- }
714
-
715
715
/**
716
716
* Add a new {@link QueryableJsonSchemaProperty queryable property} for the given source property.
717
717
* <p>
@@ -739,7 +739,6 @@ public Document toDocument() {
739
739
return new Document ("fields" , selectPaths ());
740
740
}
741
741
742
- @ NonNull
743
742
private List <Document > selectPaths () {
744
743
745
744
Map <String , Document > fields = new LinkedHashMap <>();
@@ -760,10 +759,13 @@ private List<Document> fromProperties() {
760
759
761
760
List <Document > converted = new ArrayList <>(queryableProperties .size ());
762
761
for (QueryableJsonSchemaProperty property : queryableProperties ) {
762
+
763
763
Document field = new Document ("path" , property .getIdentifier ());
764
+
764
765
if (!property .getTypes ().isEmpty ()) {
765
766
field .append ("bsonType" , property .getTypes ().iterator ().next ().toBsonType ().value ());
766
767
}
768
+
767
769
if (property
768
770
.getTargetProperty () instanceof IdentifiableJsonSchemaProperty .EncryptedJsonSchemaProperty encrypted ) {
769
771
if (encrypted .getKeyId () != null ) {
@@ -775,11 +777,13 @@ private List<Document> fromProperties() {
775
777
}
776
778
}
777
779
}
778
- field .append ("queries" , property .getCharacteristics ().getCharacteristics ().stream ()
779
- .map (QueryCharacteristic ::toDocument ).collect (Collectors .toList ()));
780
+
781
+ field .append ("queries" , property .getCharacteristics ().map (QueryCharacteristic ::toDocument ).toList ());
782
+
780
783
if (!field .containsKey ("keyId" )) {
781
784
field .append ("keyId" , BsonNull .VALUE );
782
785
}
786
+
783
787
converted .add (field );
784
788
}
785
789
return converted ;
@@ -813,7 +817,7 @@ private List<Document> fromSchema() {
813
817
}
814
818
}
815
819
816
- private static void collectPaths (Document document , String currentPath , Map <String , Document > paths ) {
820
+ private static void collectPaths (Document document , @ Nullable String currentPath , Map <String , Document > paths ) {
817
821
818
822
if (document .containsKey ("type" ) && document .get ("type" ).equals ("object" )) {
819
823
Object o = document .get ("properties" );
0 commit comments