Bug #98322 MySqlConnection(null) now throws NullReferenceException
Submitted: 22 Jan 2020 5:56 Modified: 3 Apr 2020 20:25
Reporter: Bradley Grainger (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:8.0.19 OS:Windows (10)
Assigned to: CPU Architecture:Any

[22 Jan 2020 5:56] Bradley Grainger
Description:
There is a regression in "new MySqlConnection(null)" in Connector/NET 8.0.19.

In Connector/NET 8.0.18 and earlier, "new MySqlConnection(null)" would return a MySqlConnection object with a ConnectionString property equal to String.Empty.

In Connector.NET 8.0.19, this throws a NullReferenceException:

System.NullReferenceException: Object reference not set to an instance of an object.
   at MySql.Data.MySqlClient.MySqlBaseConnectionStringBuilder.AnalyzeConnectionString(String connectionString, Boolean isXProtocol, Boolean isDefaultPort)
   at MySql.Data.MySqlClient.MySqlConnection..ctor(String connectionString)

How to repeat:
Run this C# code:

var conn = new MySqlConnection(null);

Suggested fix:
A NullReferenceException usually indicates a programming error. If this is supposed to indicate an error to the caller, it should throw an ArgumentNullException.

Otherwise, it would be preferable to return to the 8.0.18 behaviour and coerce this value to "".
[22 Jan 2020 6:18] Bradley Grainger
A reason for restoring the old behaviour (no exception) is that the following code works:

using var connection = new MySqlConnection("server=localhost");
connection.ConnectionString = null;
Assert.Equal("", connection.ConnectionString); // passes

If "new MySqlConnection(null)" is invalid then "connection.ConnectionString = null;" should have the same behaviour.
[22 Jan 2020 7:06] MySQL Verification Team
Hello Bradley,

Thank you for the report and test case.

regards,
Umesh
[3 Apr 2020 20:25] Christine Cole
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/NET 8.0.21 release, and here's the proposed changelog entry from the documentation team:

In Connector/NET 8.0.19, calling new MySqlConnection(null) returned
NullReferenceException, rather than returning an object with a
ConnectionString property equal to String.Empty, as the previous versions
of Connector/NET did. This fix restores the earlier behavior.

Thank you for the bug report.