Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix | NRE on assigning null to SqlConnectionStringBuilder.Encrypt #1778

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<format type="text/markdown"><![CDATA[

## Remarks
Implicit conversions have been added to maintain backwards compatibility with boolean behahavior for the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Encrypt%2A> property. When converting from a boolean, a value of `true` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A> and a value of `false` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A>. When converting to a boolean, <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A> and <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Strict%2A> convert to `true` and <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A> converts `false`.
Implicit conversions have been added to maintain backwards compatibility with boolean behahavior for the <xref:Microsoft.Data.SqlClient.SqlConnectionStringBuilder.Encrypt%2A> property. When converting from a boolean, a value of `true` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A> and a value of `false` converts to <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A>. When converting to a boolean, <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Mandatory%2A>, <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Strict%2A> , and `null` convert to `true` and <xref:Microsoft.Data.SqlClient.SqlConnectionEncryptOption.Optional%2A> converts `false`.

]]></format>
</remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,9 @@ public SqlConnectionEncryptOption Encrypt
get => _encrypt;
set
{
SetSqlConnectionEncryptionValue(value);
_encrypt = value;
SqlConnectionEncryptOption newValue = value ?? DbConnectionStringDefaults.Encrypt;
SetSqlConnectionEncryptionValue(newValue);
_encrypt = newValue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
builder.Encrypt = SqlConnectionEncryptOption.Strict;
Assert.Equal("Encrypt=Strict", builder.ConnectionString);
Assert.True(builder.Encrypt);

builder.Encrypt = null;
Assert.Equal("Encrypt=True", builder.ConnectionString);
Assert.True(builder.Encrypt);
}

[Theory]
Expand Down Expand Up @@ -414,7 +418,7 @@ public void EncryptParserInvalidValuesThrowsException(string value)
[InlineData("strict", "Strict")]
public void EncryptTryParseValidValuesReturnsTrue(string value, string expectedValue)
{
Assert.True(SqlConnectionEncryptOption.TryParse(value, out var result));
Assert.True(SqlConnectionEncryptOption.TryParse(value, out SqlConnectionEncryptOption result));
Assert.Equal(expectedValue, result.ToString());
}

Expand All @@ -424,7 +428,10 @@ public void EncryptTryParseValidValuesReturnsTrue(string value, string expectedV
[InlineData(null)]
[InlineData(" true ")]
public void EncryptTryParseInvalidValuesReturnsFalse(string value)
=> Assert.False(SqlConnectionEncryptOption.TryParse(value, out _));
{
Assert.False(SqlConnectionEncryptOption.TryParse(value, out SqlConnectionEncryptOption result));
Assert.Null(result);
}

internal void ExecuteConnectionStringTests(string connectionString)
{
Expand Down