@@ -281,7 +281,11 @@ public void DropCollection(IClientSessionHandle session, string name, Cancellati
281281 public void DropCollection ( IClientSessionHandle session , string name , DropCollectionOptions options , CancellationToken cancellationToken )
282282 {
283283 Ensure . IsNotNull ( session , nameof ( session ) ) ;
284- var operation = CreateDropCollectionOperation ( name , options , session , cancellationToken ) ;
284+ Ensure . IsNotNullOrEmpty ( name , nameof ( name ) ) ;
285+
286+ var collectionNamespace = new CollectionNamespace ( _databaseNamespace , name ) ;
287+ var encryptedFields = GetEncryptedFields ( session , collectionNamespace , options , cancellationToken ) ;
288+ var operation = CreateDropCollectionOperation ( collectionNamespace , encryptedFields ) ;
285289 _operationExecutor . ExecuteWriteOperation ( operation , _writeOperationOptions , session , cancellationToken ) ;
286290 }
287291
@@ -304,8 +308,11 @@ public Task DropCollectionAsync(IClientSessionHandle session, string name, Cance
304308 public async Task DropCollectionAsync ( IClientSessionHandle session , string name , DropCollectionOptions options , CancellationToken cancellationToken )
305309 {
306310 Ensure . IsNotNull ( session , nameof ( session ) ) ;
311+ Ensure . IsNotNullOrEmpty ( name , nameof ( name ) ) ;
307312
308- var operation = await CreateDropCollectionOperationAsync ( name , options , session , cancellationToken ) . ConfigureAwait ( false ) ;
313+ var collectionNamespace = new CollectionNamespace ( _databaseNamespace , name ) ;
314+ var encryptedFields = await GetEncryptedFieldsAsync ( session , collectionNamespace , options , cancellationToken ) . ConfigureAwait ( false ) ;
315+ var operation = CreateDropCollectionOperation ( collectionNamespace , encryptedFields ) ;
309316 await _operationExecutor . ExecuteWriteOperationAsync ( operation , _writeOperationOptions , session , cancellationToken ) . ConfigureAwait ( false ) ;
310317 }
311318
@@ -652,61 +659,8 @@ private CreateViewOperation CreateCreateViewOperation<TDocument, TResult>(
652659 } ;
653660 }
654661
655- private IWriteOperation < BsonDocument > CreateDropCollectionOperation ( string name , DropCollectionOptions options , IClientSessionHandle session , CancellationToken cancellationToken )
662+ private IWriteOperation < BsonDocument > CreateDropCollectionOperation ( CollectionNamespace collectionNamespace , BsonDocument effectiveEncryptedFields )
656663 {
657- Ensure . IsNotNullOrEmpty ( name , nameof ( name ) ) ;
658- var collectionNamespace = new CollectionNamespace ( _databaseNamespace , name ) ;
659- options ??= new DropCollectionOptions ( ) ;
660-
661- var encryptedFieldsMap = _client . Settings ? . AutoEncryptionOptions ? . EncryptedFieldsMap ;
662- if ( ! EncryptedCollectionHelper . TryGetEffectiveEncryptedFields ( collectionNamespace , options . EncryptedFields , encryptedFieldsMap , out var effectiveEncryptedFields ) )
663- {
664- if ( encryptedFieldsMap != null )
665- {
666- var listCollectionOptions = new ListCollectionsOptions ( ) { Filter = $ "{{ name : '{ collectionNamespace . CollectionName } ' }}" } ;
667- var currrentCollectionInfo = ListCollections ( session , listCollectionOptions , cancellationToken ) . FirstOrDefault ( ) ;
668- effectiveEncryptedFields = currrentCollectionInfo
669- ? . GetValue ( "options" , defaultValue : null )
670- ? . AsBsonDocument
671- ? . GetValue ( "encryptedFields" , defaultValue : null )
672- ? . ToBsonDocument ( ) ;
673- }
674- }
675-
676- var messageEncoderSettings = GetMessageEncoderSettings ( ) ;
677- return DropCollectionOperation . CreateEncryptedDropCollectionOperationIfConfigured (
678- collectionNamespace ,
679- effectiveEncryptedFields ,
680- messageEncoderSettings ,
681- ( dco ) =>
682- {
683- dco . WriteConcern = _settings . WriteConcern ;
684- } ) ;
685- }
686-
687- private async Task < IWriteOperation < BsonDocument > > CreateDropCollectionOperationAsync ( string name , DropCollectionOptions options , IClientSessionHandle session , CancellationToken cancellationToken )
688- {
689- Ensure . IsNotNullOrEmpty ( name , nameof ( name ) ) ;
690- var collectionNamespace = new CollectionNamespace ( _databaseNamespace , name ) ;
691-
692- options = options ?? new DropCollectionOptions ( ) ;
693-
694- var encryptedFieldsMap = _client . Settings ? . AutoEncryptionOptions ? . EncryptedFieldsMap ;
695- if ( ! EncryptedCollectionHelper . TryGetEffectiveEncryptedFields ( collectionNamespace , options . EncryptedFields , encryptedFieldsMap , out var effectiveEncryptedFields ) )
696- {
697- if ( encryptedFieldsMap != null )
698- {
699- var listCollectionOptions = new ListCollectionsOptions ( ) { Filter = $ "{{ name : '{ collectionNamespace . CollectionName } ' }}" } ;
700- var currentCollectionsInfo = await ListCollectionsAsync ( session , listCollectionOptions , cancellationToken ) . ConfigureAwait ( false ) ;
701- var currentCollectionInfo = await currentCollectionsInfo . FirstOrDefaultAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
702- effectiveEncryptedFields = currentCollectionInfo
703- ? . GetValue ( "options" , defaultValue : null )
704- ? . AsBsonDocument
705- ? . GetValue ( "encryptedFields" , defaultValue : null )
706- ? . ToBsonDocument ( ) ;
707- }
708- }
709-
710664 var messageEncoderSettings = GetMessageEncoderSettings ( ) ;
711665 return DropCollectionOperation . CreateEncryptedDropCollectionOperationIfConfigured (
712666 collectionNamespace ,
@@ -795,6 +749,48 @@ private IEnumerable<string> ExtractCollectionNames(IEnumerable<BsonDocument> col
795749 return collections . Select ( collection => collection [ "name" ] . AsString ) ;
796750 }
797751
752+
753+ private BsonDocument GetEncryptedFields ( IClientSessionHandle session , CollectionNamespace collectionNamespace , DropCollectionOptions options , CancellationToken cancellationToken )
754+ {
755+ var encryptedFieldsMap = _client . Settings ? . AutoEncryptionOptions ? . EncryptedFieldsMap ;
756+ if ( ! EncryptedCollectionHelper . TryGetEffectiveEncryptedFields ( collectionNamespace , options ? . EncryptedFields , encryptedFieldsMap , out var effectiveEncryptedFields ) )
757+ {
758+ if ( encryptedFieldsMap != null )
759+ {
760+ var listCollectionOptions = new ListCollectionsOptions ( ) { Filter = $ "{{ name : '{ collectionNamespace . CollectionName } ' }}" } ;
761+ var currentCollectionInfo = ListCollections ( session , listCollectionOptions , cancellationToken ) . FirstOrDefault ( ) ;
762+ effectiveEncryptedFields = currentCollectionInfo
763+ ? . GetValue ( "options" , defaultValue : null )
764+ ? . AsBsonDocument
765+ ? . GetValue ( "encryptedFields" , defaultValue : null )
766+ ? . ToBsonDocument ( ) ;
767+ }
768+ }
769+
770+ return effectiveEncryptedFields ;
771+ }
772+
773+ private async Task < BsonDocument > GetEncryptedFieldsAsync ( IClientSessionHandle session , CollectionNamespace collectionNamespace , DropCollectionOptions options , CancellationToken cancellationToken )
774+ {
775+ var encryptedFieldsMap = _client . Settings ? . AutoEncryptionOptions ? . EncryptedFieldsMap ;
776+ if ( ! EncryptedCollectionHelper . TryGetEffectiveEncryptedFields ( collectionNamespace , options ? . EncryptedFields , encryptedFieldsMap , out var effectiveEncryptedFields ) )
777+ {
778+ if ( encryptedFieldsMap != null )
779+ {
780+ var listCollectionOptions = new ListCollectionsOptions ( ) { Filter = $ "{{ name : '{ collectionNamespace . CollectionName } ' }}" } ;
781+ var currentCollectionsInfo = await ListCollectionsAsync ( session , listCollectionOptions , cancellationToken ) . ConfigureAwait ( false ) ;
782+ var currentCollectionInfo = await currentCollectionsInfo . FirstOrDefaultAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
783+ effectiveEncryptedFields = currentCollectionInfo
784+ ? . GetValue ( "options" , defaultValue : null )
785+ ? . AsBsonDocument
786+ ? . GetValue ( "encryptedFields" , defaultValue : null )
787+ ? . ToBsonDocument ( ) ;
788+ }
789+ }
790+
791+ return effectiveEncryptedFields ;
792+ }
793+
798794 private MessageEncoderSettings GetMessageEncoderSettings ( )
799795 {
800796 var messageEncoderSettings = new MessageEncoderSettings
0 commit comments