Skip to content

Commit 927cb08

Browse files
JamesKovacsrstam
authored andcommitted
CSHARP-4595: Support GridFS with Stable API. (#1064)
1 parent bb93334 commit 927cb08

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

src/MongoDB.Driver.Core/Core/Misc/Feature.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public class Feature
116116
private static readonly Feature __snapshotReads = new Feature("SnapshotReads", WireVersion.Server50, notSupportedMessage: "Snapshot reads require MongoDB 5.0 or later");
117117
private static readonly Feature __sortArrayOperator = new Feature("SortArrayOperator", WireVersion.Server52);
118118
private static readonly Feature __speculativeAuthentication = new Feature("SpeculativeAuthentication", WireVersion.Server44);
119+
private static readonly Feature __stableApi = new Feature("StableAPI", WireVersion.Server50);
119120
private static readonly Feature __streamingHello = new Feature("StreamingHello", WireVersion.Server44);
120121
private static readonly Feature __tailableCursor = new Feature("TailableCursor", WireVersion.Server32);
121122
private static readonly Feature __toConversionOperators = new Feature("ToConversionOperators", WireVersion.Server40);
@@ -644,6 +645,11 @@ public class Feature
644645
/// </summary>
645646
public static Feature SpeculativeAuthentication => __speculativeAuthentication;
646647

648+
/// <summary>
649+
/// Gets the speculative authentication feature.
650+
/// </summary>
651+
public static Feature StableApi => __stableApi;
652+
647653
/// <summary>
648654
/// Gets the streaming hello feature.
649655
/// </summary>

src/MongoDB.Driver.GridFS/GridFSBucket.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
using System.Collections.Generic;
1818
using System.Diagnostics.CodeAnalysis;
1919
using System.IO;
20-
using System.Linq;
2120
using System.Threading;
2221
using System.Threading.Tasks;
2322
using MongoDB.Bson;
24-
using MongoDB.Bson.IO;
2523
using MongoDB.Bson.Serialization;
2624
using MongoDB.Bson.Serialization.Serializers;
2725
using MongoDB.Driver.Core.Bindings;
@@ -643,7 +641,7 @@ private FindOperation<GridFSFileInfo<TFileId>> CreateFindOperation(FilterDefinit
643641
Filter = renderedFilter,
644642
Limit = options.Limit,
645643
MaxTime = options.MaxTime,
646-
NoCursorTimeout = options.NoCursorTimeout ?? false,
644+
NoCursorTimeout = options.NoCursorTimeout,
647645
ReadConcern = GetReadConcern(),
648646
RetryRequested = _database.Client.Settings.RetryReads,
649647
Skip = options.Skip,

tests/MongoDB.Driver.GridFS.Tests/GridFSBucketTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,79 @@ public void UploadFromStream_should_throw_when_source_is_null(
630630
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("source");
631631
}
632632

633+
[Theory]
634+
[ParameterAttributeData]
635+
public void GridFS_should_work_with_strict_stable_api(
636+
[Values(false, true)] bool async)
637+
{
638+
RequireServer.Check().Supports(Feature.StableApi);
639+
640+
var settings = DriverTestConfiguration.GetClientSettings();
641+
settings.ServerApi = new ServerApi(ServerApiVersion.V1, strict: true, deprecationErrors: true);
642+
643+
using var client = DriverTestConfiguration.CreateDisposableClient(settings);
644+
var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
645+
var subject = new GridFSBucket<ObjectId>(database);
646+
647+
if (async)
648+
{
649+
subject.DropAsync().GetAwaiter().GetResult();
650+
}
651+
else
652+
{
653+
subject.Drop();
654+
}
655+
656+
const string filename = "hello.txt";
657+
const string content = "Hello, world!";
658+
var bytes = Encoding.UTF8.GetBytes(content);
659+
var id = ObjectId.GenerateNewId();
660+
if (async)
661+
{
662+
subject.UploadFromBytesAsync(id, filename, bytes).GetAwaiter().GetResult();
663+
}
664+
else
665+
{
666+
subject.UploadFromBytes(id, filename, bytes);
667+
}
668+
669+
var filter = Builders<GridFSFileInfo<ObjectId>>.Filter.Eq(x => x.Id, id);
670+
List<GridFSFileInfo<ObjectId>> results;
671+
if (async)
672+
{
673+
results = subject.FindAsync(filter).GetAwaiter().GetResult().ToList();
674+
}
675+
else
676+
{
677+
results = subject.Find(filter).ToList();
678+
}
679+
680+
results.Count.Should().Be(1);
681+
results.First().Filename.Should().Be(filename);
682+
683+
byte[] downloadedBytes;
684+
if (async)
685+
{
686+
downloadedBytes = subject.DownloadAsBytesAsync(id).GetAwaiter().GetResult();
687+
}
688+
else
689+
{
690+
downloadedBytes = subject.DownloadAsBytes(id);
691+
}
692+
693+
var downloadedContent = Encoding.UTF8.GetString(downloadedBytes);
694+
downloadedContent.Should().Be(content);
695+
696+
if (async)
697+
{
698+
subject.DropAsync().GetAwaiter().GetResult();
699+
}
700+
else
701+
{
702+
subject.Drop();
703+
}
704+
}
705+
633706
// private methods
634707
private GridFSBucket CreateSubject(GridFSBucketOptions options = null)
635708
{

0 commit comments

Comments
 (0)