From 040500387254c955ef96775f7948b9625b433e78 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Mon, 2 Sep 2024 15:41:44 +0530 Subject: [PATCH 1/3] fix SqlDataReader.IsDBNull() for json --- .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 4 ++++ .../netfx/src/Microsoft/Data/SqlClient/TdsParser.cs | 4 ++++ 2 files changed, 8 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 02b26a3a1b..d2f85f3ca9 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 @@ -5825,6 +5825,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 018078a66f..3da113fed3 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 @@ -6670,6 +6670,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; From 3990a7e7239609b42ddee8c7b50187e41c60819b Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Tue, 3 Sep 2024 18:32:16 +0530 Subject: [PATCH 2/3] add testcase --- .../ManualTests/SQL/JsonTest/JsonTest.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) 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 b4bd08c5dc..3e2dbca3b6 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs @@ -61,6 +61,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() { @@ -286,5 +296,40 @@ 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(); + } + } + } } } From f0033e7fd6489a5a92c9e368b749891ce3ed95f1 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Wed, 4 Sep 2024 15:13:09 +0530 Subject: [PATCH 3/3] resolve PR commits --- .../ManualTests/SQL/JsonTest/JsonTest.cs | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 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 3e2dbca3b6..c8be762441 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/JsonTest/JsonTest.cs @@ -306,30 +306,28 @@ public void TestNullJson() 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(); - } - } + 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); } } }