Skip to content

Commit fa3143c

Browse files
committed
Added exception for empty schema
1 parent d30739d commit fa3143c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/MongoDB.Driver.Encryption/CsfleSchemaBuilder.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ public CsfleSchemaBuilder Encrypt<T>(string collectionNamespace, Action<Encrypte
5757
/// <summary>
5858
/// Builds and returns the resulting CSFLE schema.
5959
/// </summary>
60-
public IDictionary<string, BsonDocument> Build() => _schemas;
60+
public IDictionary<string, BsonDocument> Build()
61+
{
62+
if (!_schemas.Any())
63+
{
64+
throw new InvalidOperationException("No schemas were added. Use Encrypt<T> to add a schema.");
65+
}
66+
67+
return _schemas;
68+
}
6169
}
6270

6371
/// <summary>

tests/MongoDB.Driver.Tests/Encryption/CsfleSchemaBuilderTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ public void CsfleSchemaBuilder_with_multiple_types_works_as_expected()
199199
AssertOutcomeCsfleSchemaBuilder(builder, expected);
200200
}
201201

202+
[Fact]
203+
public void CsfleSchemaBuilder_with_no_schemas_throws()
204+
{
205+
var builder = CsfleSchemaBuilder.Create(_ =>
206+
{
207+
// No schemas added
208+
});
209+
210+
var exception = Record.Exception(() => builder.Build());
211+
212+
exception.Should().NotBeNull();
213+
exception.Should().BeOfType<InvalidOperationException>();
214+
}
215+
202216
[Theory]
203217
[InlineData(
204218
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
@@ -264,6 +278,10 @@ public void EncryptedCollection_PatternProperty_works_as_expected(BsonType bsonT
264278
}
265279

266280
[Theory]
281+
[InlineData(null,
282+
null,
283+
null,
284+
"")]
267285
[InlineData(new[] {BsonType.Array, BsonType.String},
268286
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
269287
null,
@@ -434,6 +452,10 @@ public void EncryptedCollection_Property_with_expression_works_as_expected(BsonT
434452
}
435453

436454
[Theory]
455+
[InlineData(null,
456+
null,
457+
null,
458+
"")]
437459
[InlineData(new[] {BsonType.Array, BsonType.String},
438460
EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Random,
439461
null,

0 commit comments

Comments
 (0)