From a67c701b52873988c42e3b2d3e01490576d3c5b8 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Wed, 4 Sep 2024 15:15:57 +0530 Subject: [PATCH 1/2] Fix SqlDataReader.IsDBNull() for json (#2830) * fix SqlDataReader.IsDBNull() for json * add testcase * resolve PR commits --- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 4 ++ .../src/Microsoft/Data/SqlClient/TdsParser.cs | 4 ++ .../ManualTests/SQL/JsonTest/JsonTest.cs | 43 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index cbd4e2142e..cf3aacdf58 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -5820,6 +5820,10 @@ internal static object GetNullSqlValue(SqlBuffer nullVal, SqlMetaDataPriv md, Sq } break; + case SqlDbTypeExtensions.Json: + nullVal.SetToNullOfType(SqlBuffer.StorageType.Json); + break; + default: Debug.Fail("unknown null sqlType!" + md.type.ToString()); break; diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index 5151392c7e..c718100c09 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6667,6 +6667,10 @@ internal static object GetNullSqlValue( } break; + case SqlDbTypeExtensions.Json: + nullVal.SetToNullOfType(SqlBuffer.StorageType.Json); + break; + default: Debug.Fail("unknown null sqlType!" + md.type.ToString()); break; diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs index 38cbbf7bc2..e5d0d8e315 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs @@ -62,6 +62,16 @@ private void ValidateSchema(SqlDataReader reader) } } + private void ValidateNullJson(SqlDataReader reader) + { + while (reader.Read()) + { + bool IsNull = reader.IsDBNull(0); + _output.WriteLine(IsNull ? "null" : "not null"); + Assert.True(IsNull); + } + } + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))] public void TestJsonWrite() { @@ -295,5 +305,38 @@ public async Task TestJsonReadAsync() } } } + + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))] + public void TestNullJson() + { + string tableName = DataTestUtility.GetUniqueNameForSqlServer("Json_Test"); + + string tableCreate = "CREATE TABLE " + tableName + " (Data json)"; + string tableInsert = "INSERT INTO " + tableName + " VALUES (@jsonData)"; + string tableRead = "SELECT * FROM " + tableName; + + using SqlConnection connection = new SqlConnection(DataTestUtility.TCPConnectionString); + connection.Open(); + using SqlCommand command = connection.CreateCommand(); + + //Create Table + command.CommandText = tableCreate; + command.ExecuteNonQuery(); + + //Insert Null value + command.CommandText = tableInsert; + var parameter = new SqlParameter("@jsonData", SqlDbTypeExtensions.Json); + parameter.Value = DBNull.Value; + command.Parameters.Add(parameter); + command.ExecuteNonQuery(); + + //Query the table + command.CommandText = tableRead; + var reader = command.ExecuteReader(); + ValidateNullJson(reader); + + reader.Close(); + DataTestUtility.DropTable(connection, tableName); + } } } From bd3c3c7507e5112313d4cbc2bd2a06681ef9f70b Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Tue, 1 Oct 2024 14:20:52 +0530 Subject: [PATCH 2/2] add CreateTable --- .../tests/ManualTests/SQL/JsonTest/JsonTest.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs index e5d0d8e315..a0f02b9c20 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs @@ -309,9 +309,8 @@ public async Task TestJsonReadAsync() [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.IsJsonSupported))] public void TestNullJson() { - string tableName = DataTestUtility.GetUniqueNameForSqlServer("Json_Test"); + string tableName = "jsonTest"; - string tableCreate = "CREATE TABLE " + tableName + " (Data json)"; string tableInsert = "INSERT INTO " + tableName + " VALUES (@jsonData)"; string tableRead = "SELECT * FROM " + tableName; @@ -320,8 +319,7 @@ public void TestNullJson() using SqlCommand command = connection.CreateCommand(); //Create Table - command.CommandText = tableCreate; - command.ExecuteNonQuery(); + DataTestUtility.CreateTable(connection, tableName, "(Data json)"); //Insert Null value command.CommandText = tableInsert;