Skip to content

Commit cd76ad5

Browse files
rstamDmitryLukyanov
authored andcommitted
CSHARP-3252: AutoEncryptionOptions.ToString should handle guids correctly.
1 parent 3c2ddae commit cd76ad5

File tree

4 files changed

+103
-8
lines changed

4 files changed

+103
-8
lines changed

src/MongoDB.Bson/IO/JsonWriter.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,14 +886,24 @@ private string GuidToString(BsonBinarySubType subType, byte[] bytes, GuidReprese
886886
var message = string.Format("Length of binary subtype {0} must be 16, not {1}.", subType, bytes.Length);
887887
throw new ArgumentException(message);
888888
}
889-
if (subType == BsonBinarySubType.UuidLegacy && guidRepresentation == GuidRepresentation.Standard)
889+
if (subType == BsonBinarySubType.UuidLegacy)
890890
{
891-
throw new ArgumentException("GuidRepresentation for binary subtype UuidLegacy must not be Standard.");
891+
if (guidRepresentation == GuidRepresentation.Standard)
892+
{
893+
throw new ArgumentException("GuidRepresentation for binary subtype UuidLegacy must not be Standard.");
894+
}
892895
}
893-
if (subType == BsonBinarySubType.UuidStandard && guidRepresentation != GuidRepresentation.Standard)
896+
if (subType == BsonBinarySubType.UuidStandard)
894897
{
895-
var message = string.Format("GuidRepresentation for binary subtype UuidStandard must be Standard, not {0}.", guidRepresentation);
896-
throw new ArgumentException(message);
898+
if (guidRepresentation == GuidRepresentation.Unspecified)
899+
{
900+
guidRepresentation = GuidRepresentation.Standard;
901+
}
902+
if (guidRepresentation != GuidRepresentation.Standard)
903+
{
904+
var message = string.Format("GuidRepresentation for binary subtype UuidStandard must be Standard, not {0}.", guidRepresentation);
905+
throw new ArgumentException(message);
906+
}
897907
}
898908

899909
if (guidRepresentation == GuidRepresentation.Unspecified)

src/MongoDB.Driver/Encryption/AutoEncryptionOptions.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq;
1818
using System.Text;
1919
using MongoDB.Bson;
20+
using MongoDB.Bson.IO;
2021
using MongoDB.Driver.Core.Misc;
2122
using MongoDB.Shared;
2223

@@ -172,20 +173,28 @@ public override int GetHashCode()
172173
public override string ToString()
173174
{
174175
var sb = new StringBuilder();
176+
var jsonWriterSettings = new JsonWriterSettings();
177+
#pragma warning disable 618
178+
if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
179+
{
180+
jsonWriterSettings.GuidRepresentation = GuidRepresentation.Unspecified;
181+
}
182+
#pragma warning restore 618
183+
175184
sb.Append("{ ");
176185
sb.AppendFormat("BypassAutoEncryption : {0}, ", _bypassAutoEncryption);
177-
sb.AppendFormat("KmsProviders : {0}, ", _kmsProviders.ToJson());
186+
sb.AppendFormat("KmsProviders : {0}, ", _kmsProviders.ToJson(jsonWriterSettings));
178187
if (_keyVaultNamespace != null)
179188
{
180189
sb.AppendFormat("KeyVaultNamespace : \"{0}\", ", _keyVaultNamespace.FullName);
181190
}
182191
if (_extraOptions != null)
183192
{
184-
sb.AppendFormat("ExtraOptions : {0}, ", _extraOptions.ToJson());
193+
sb.AppendFormat("ExtraOptions : {0}, ", _extraOptions.ToJson(jsonWriterSettings));
185194
}
186195
if (_schemaMap != null)
187196
{
188-
sb.AppendFormat("SchemaMap : {0}, ", _schemaMap.ToJson());
197+
sb.AppendFormat("SchemaMap : {0}, ", _schemaMap.ToJson(jsonWriterSettings));
189198
}
190199
sb.Remove(sb.Length - 2, 2);
191200
sb.Append(" }");

tests/MongoDB.Bson.Tests/IO/JsonWriterTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,22 @@ public void TestGuid(
511511
#pragma warning restore 618, 1062
512512
}
513513

514+
[Fact]
515+
public void TestUuidStandardWhenGuidRepresentationIsUnspecified()
516+
{
517+
var guid = new Guid("00112233445566778899aabbccddeeff");
518+
var guidBytes = GuidConverter.ToBytes(guid, GuidRepresentation.Standard);
519+
520+
var binary = new BsonBinaryData(guidBytes, BsonBinarySubType.UuidStandard); // GuidRepresentation is Unspecified
521+
var result = binary.ToJson(writerSettings: new JsonWriterSettings()
522+
{
523+
#pragma warning disable CS0618 // Type or member is obsolete
524+
GuidRepresentation = GuidRepresentation.Unspecified
525+
#pragma warning restore CS0618 // Type or member is obsolete
526+
});
527+
result.Should().Be("UUID(\"00112233-4455-6677-8899-aabbccddeeff\")");
528+
}
529+
514530
[Fact]
515531
public void TestMaxKey()
516532
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* Copyright 2020–present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using FluentAssertions;
19+
using MongoDB.Bson;
20+
using MongoDB.Driver.Encryption;
21+
using Xunit;
22+
23+
namespace MongoDB.Driver.Tests
24+
{
25+
public class AutoEncryptionOptionsTests
26+
{
27+
[Fact]
28+
public void ToString_should_return_expected_result()
29+
{
30+
var guid = new Guid("00112233445566778899aabbccddeeff");
31+
var guidBytes = GuidConverter.ToBytes(guid, GuidRepresentation.Standard);
32+
var binary = new BsonBinaryData(guidBytes, BsonBinarySubType.UuidStandard);
33+
34+
var extraOptions = new Dictionary<string, object>()
35+
{
36+
{ "mongocryptdURI", "testURI" },
37+
};
38+
var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>()
39+
{
40+
{ "provider1", new Dictionary<string, object>() { { "string", "test" } } },
41+
{ "provider2", new Dictionary<string, object>() { { "binary", binary.Bytes } } }
42+
};
43+
var schemaMap = new Dictionary<string, BsonDocument>()
44+
{
45+
{ "coll1", new BsonDocument("string", "test") },
46+
{ "coll2", new BsonDocument("binary", binary) },
47+
};
48+
49+
var subject = new AutoEncryptionOptions(
50+
keyVaultNamespace: CollectionNamespace.FromFullName("db.coll"),
51+
kmsProviders: kmsProviders,
52+
bypassAutoEncryption: true,
53+
extraOptions: extraOptions,
54+
schemaMap: schemaMap);
55+
56+
var result = subject.ToString();
57+
result.Should().Be("{ BypassAutoEncryption : True, KmsProviders : { \"provider1\" : { \"string\" : \"test\" }, \"provider2\" : { \"binary\" : { \"_t\" : \"System.Byte[]\", \"_v\" : new BinData(0, \"ABEiM0RVZneImaq7zN3u/w==\") } } }, KeyVaultNamespace : \"db.coll\", ExtraOptions : { \"mongocryptdURI\" : \"testURI\" }, SchemaMap : { \"coll1\" : { \"string\" : \"test\" }, \"coll2\" : { \"binary\" : UUID(\"00112233-4455-6677-8899-aabbccddeeff\") } } }");
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)