From d7d61520e31f1618371aba5baad171da2eb7e2a5 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Thu, 23 May 2024 22:54:58 +0530 Subject: [PATCH 1/9] change json hexa code and fix invalid login error --- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 15 +++++++++++++++ .../src/Microsoft/Data/SqlClient/TdsEnums.cs | 1 + 2 files changed, 16 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 6af6ae7734..fa59094903 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 @@ -8115,6 +8115,21 @@ internal int WriteFedAuthFeatureRequest(FederatedAuthenticationFeatureExtensionD return len; } + internal int WriteJsonSupportFeatureRequest(bool write /* if false just calculates the length */) + { + int len = 6; // 1byte = featureID, 4bytes = featureData length, 1 bytes = Version + + if (write) + { + // Write Feature ID + _physicalStateObj.WriteByte(TdsEnums.FEATUREEXT_JSONSUPPORT); + WriteInt(1, _physicalStateObj); + _physicalStateObj.WriteByte(1); + } + + return len; + } + private void WriteLoginData(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures, SessionData recoverySessionData, diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs index 8c7119a695..f60f695c06 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs @@ -238,6 +238,7 @@ public enum EnvChangeType : byte public const byte FEATUREEXT_DATACLASSIFICATION = 0x09; public const byte FEATUREEXT_UTF8SUPPORT = 0x0A; public const byte FEATUREEXT_SQLDNSCACHING = 0x0B; + public const byte FEATUREEXT_JSONSUPPORT = 0x0D; [Flags] public enum FeatureExtension : uint From 0e5398b82d529ac4df663a32197660567136eb09 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Wed, 29 May 2024 23:27:21 +0530 Subject: [PATCH 2/9] Change name to match other naming conventions --- .../src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs | 2 ++ .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 5 +++++ .../src/Microsoft/Data/SqlClient/TdsEnums.cs | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index 5ef3d781ea..f0c5fb0b04 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -1365,6 +1365,8 @@ private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword, // The SQLDNSCaching feature is implicitly set requestedFeatures |= TdsEnums.FeatureExtension.SQLDNSCaching; + requestedFeatures |= TdsEnums.FeatureExtension.JSONSupport; + _parser.TdsLogin(login, requestedFeatures, _recoverySessionData, _fedAuthFeatureExtensionData, encrypt); } 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 fa59094903..8a3c3ab691 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 @@ -8440,6 +8440,11 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures, length += WriteSQLDNSCachingFeatureRequest(write); } + if ((requestedFeatures & TdsEnums.FeatureExtension.JSONSupport) != 0) + { + length += WriteJsonSupportFeatureRequest(write); + } + length++; // for terminator if (write) { diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs index f60f695c06..d3fefa2884 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs @@ -251,7 +251,8 @@ public enum FeatureExtension : uint AzureSQLSupport = 1 << (TdsEnums.FEATUREEXT_AZURESQLSUPPORT - 1), DataClassification = 1 << (TdsEnums.FEATUREEXT_DATACLASSIFICATION - 1), UTF8Support = 1 << (TdsEnums.FEATUREEXT_UTF8SUPPORT - 1), - SQLDNSCaching = 1 << (TdsEnums.FEATUREEXT_SQLDNSCACHING - 1) + SQLDNSCaching = 1 << (TdsEnums.FEATUREEXT_SQLDNSCACHING - 1), + JSONSupport = 1 << (TdsEnums.FEATUREEXT_JSONSUPPORT - 1) } public const uint UTF8_IN_TDSCOLLATION = 0x4000000; From df94aeb2b8154dfc72445dbb9e55c01b15c40a3a Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Thu, 30 May 2024 22:44:37 +0530 Subject: [PATCH 3/9] process JSONSUpport acknowledgement --- .../Data/SqlClient/SqlInternalConnectionTds.cs | 17 +++++++++++++++++ .../src/Microsoft/Data/SqlClient/TdsParser.cs | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index f0c5fb0b04..920cb02bb4 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -2814,6 +2814,23 @@ internal void OnFeatureExtAck(int featureId, byte[] data) break; } + case TdsEnums.FEATUREEXT_JSONSUPPORT: + { + SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, Received feature extension acknowledgement for JSONSUPPORT", ObjectID); + if (data.Length < 1) + { + SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown version number for JSONSUPPORT", ObjectID); + throw SQL.ParsingError(); + } + byte JSONSupportVersion = data[0]; + if (JSONSupportVersion == 0 || JSONSupportVersion > 1) + { + SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); + } + _parser.IsJSONSupportExist = true; + break; + } + default: { // Unknown feature ack 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 8a3c3ab691..9a925c6578 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 @@ -194,6 +194,8 @@ internal static void Assert(string message) /// internal int DataClassificationVersion { get; set; } + internal bool IsJSONSupportExist = false; + private SqlCollation _cachedCollation; internal TdsParser(bool MARS, bool fAsynchronous) From 60b8329852acca752539b1699b5cc50edb13aee2 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Thu, 30 May 2024 22:54:05 +0530 Subject: [PATCH 4/9] Fix naming conventions --- .../Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs | 8 ++++---- .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 4 ++-- .../src/Microsoft/Data/SqlClient/TdsEnums.cs | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index 920cb02bb4..b108ad628f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -1365,7 +1365,7 @@ private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword, // The SQLDNSCaching feature is implicitly set requestedFeatures |= TdsEnums.FeatureExtension.SQLDNSCaching; - requestedFeatures |= TdsEnums.FeatureExtension.JSONSupport; + requestedFeatures |= TdsEnums.FeatureExtension.JsonSupport; _parser.TdsLogin(login, requestedFeatures, _recoverySessionData, _fedAuthFeatureExtensionData, encrypt); } @@ -2822,12 +2822,12 @@ internal void OnFeatureExtAck(int featureId, byte[] data) SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown version number for JSONSUPPORT", ObjectID); throw SQL.ParsingError(); } - byte JSONSupportVersion = data[0]; - if (JSONSupportVersion == 0 || JSONSupportVersion > 1) + byte JsonSupportVersion = data[0]; + if (JsonSupportVersion == 0 || JsonSupportVersion > 1) { SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); } - _parser.IsJSONSupportExist = true; + _parser.IsJsonSupportExist = true; break; } 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 9a925c6578..381904d80f 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 @@ -194,7 +194,7 @@ internal static void Assert(string message) /// internal int DataClassificationVersion { get; set; } - internal bool IsJSONSupportExist = false; + internal bool IsJsonSupportExist = false; private SqlCollation _cachedCollation; @@ -8442,7 +8442,7 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures, length += WriteSQLDNSCachingFeatureRequest(write); } - if ((requestedFeatures & TdsEnums.FeatureExtension.JSONSupport) != 0) + if ((requestedFeatures & TdsEnums.FeatureExtension.JsonSupport) != 0) { length += WriteJsonSupportFeatureRequest(write); } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs index d3fefa2884..f8b320be1a 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsEnums.cs @@ -252,7 +252,7 @@ public enum FeatureExtension : uint DataClassification = 1 << (TdsEnums.FEATUREEXT_DATACLASSIFICATION - 1), UTF8Support = 1 << (TdsEnums.FEATUREEXT_UTF8SUPPORT - 1), SQLDNSCaching = 1 << (TdsEnums.FEATUREEXT_SQLDNSCACHING - 1), - JSONSupport = 1 << (TdsEnums.FEATUREEXT_JSONSUPPORT - 1) + JsonSupport = 1 << (TdsEnums.FEATUREEXT_JSONSUPPORT - 1) } public const uint UTF8_IN_TDSCOLLATION = 0x4000000; From d58d3afda64a6141a3d247edc769bd17988998c5 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Mon, 3 Jun 2024 19:12:26 +0530 Subject: [PATCH 5/9] Processing 2nd byte from server --- .../Data/SqlClient/SqlInternalConnectionTds.cs | 15 ++++++++++----- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index b108ad628f..ba8a2a8080 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -2817,17 +2817,22 @@ internal void OnFeatureExtAck(int featureId, byte[] data) case TdsEnums.FEATUREEXT_JSONSUPPORT: { SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, Received feature extension acknowledgement for JSONSUPPORT", ObjectID); - if (data.Length < 1) + if (data.Length != 2) { - SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown version number for JSONSUPPORT", ObjectID); - throw SQL.ParsingError(); + SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown token for JSONSUPPORT", ObjectID); + throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); } - byte JsonSupportVersion = data[0]; + byte JsonSupportVersion = data[0]; if (JsonSupportVersion == 0 || JsonSupportVersion > 1) { SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); + throw SQL.ParsingError(); + } + byte enabled = data[1]; + if (enabled == 1) + { + _parser.IsJsonSupportEnabled = true; } - _parser.IsJsonSupportExist = true; break; } 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 381904d80f..ce40b30bb6 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 @@ -194,7 +194,7 @@ internal static void Assert(string message) /// internal int DataClassificationVersion { get; set; } - internal bool IsJsonSupportExist = false; + internal bool IsJsonSupportEnabled = false; private SqlCollation _cachedCollation; From 4accb5ad91ef4a11f7da98c117bb3e420313f6da Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Tue, 4 Jun 2024 21:26:12 +0530 Subject: [PATCH 6/9] Changes for netfx --- .../SqlClient/SqlInternalConnectionTds.cs | 24 +++++++++++++++++++ .../src/Microsoft/Data/SqlClient/TdsParser.cs | 22 +++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index cc80af6767..da216b43a8 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -1638,6 +1638,8 @@ private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword, // The SQLDNSCaching feature is implicitly set requestedFeatures |= TdsEnums.FeatureExtension.SQLDNSCaching; + requestedFeatures |= TdsEnums.FeatureExtension.JsonSupport; + _parser.TdsLogin(login, requestedFeatures, _recoverySessionData, _fedAuthFeatureExtensionData, _originalNetworkAddressInfo, encrypt); } @@ -3239,6 +3241,28 @@ internal void OnFeatureExtAck(int featureId, byte[] data) break; } + case TdsEnums.FEATUREEXT_JSONSUPPORT: + { + SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, Received feature extension acknowledgement for JSONSUPPORT", ObjectID); + if (data.Length != 2) + { + SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown token for JSONSUPPORT", ObjectID); + throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); + } + byte JsonSupportVersion = data[0]; + if (JsonSupportVersion == 0 || JsonSupportVersion > 1) + { + SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); + throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); + } + byte enabled = data[1]; + if (enabled == 1) + { + _parser.IsJsonSupportEnabled = true; + } + break; + } + default: { // Unknown feature ack 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 0c74f8f61b..4c814524a9 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 @@ -311,6 +311,8 @@ internal bool IsDataClassificationEnabled /// internal int DataClassificationVersion { get; set; } + internal bool IsJsonSupportEnabled = false; + private SqlCollation _cachedCollation; internal TdsParser(bool MARS, bool fAsynchronous) @@ -8914,6 +8916,21 @@ internal int WriteFedAuthFeatureRequest(FederatedAuthenticationFeatureExtensionD return len; } + internal int WriteJsonSupportFeatureRequest(bool write /* if false just calculates the length */) + { + int len = 6; // 1byte = featureID, 4bytes = featureData length, 1 bytes = Version + + if (write) + { + // Write Feature ID + _physicalStateObj.WriteByte(TdsEnums.FEATUREEXT_JSONSUPPORT); + WriteInt(1, _physicalStateObj); + _physicalStateObj.WriteByte(1); + } + + return len; + } + private void WriteLoginData(SqlLogin rec, TdsEnums.FeatureExtension requestedFeatures, SessionData recoverySessionData, @@ -9241,6 +9258,11 @@ private int ApplyFeatureExData(TdsEnums.FeatureExtension requestedFeatures, length += WriteSQLDNSCachingFeatureRequest(write); } + if ((requestedFeatures & TdsEnums.FeatureExtension.JsonSupport) != 0) + { + length += WriteJsonSupportFeatureRequest(write); + } + length++; // for terminator if (write) { From 921455708d5e6fad3138078c35767ebb8724d67a Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Thu, 6 Jun 2024 22:42:55 +0530 Subject: [PATCH 7/9] Json support feature ack in Test TDS server --- .../TDS/TDS.EndPoint/ITDSServerSession.cs | 5 +++ .../tools/TDS/TDS.Servers/GenericTDSServer.cs | 33 +++++++++++++++++++ .../TDS.Servers/GenericTDSServerSession.cs | 5 +++ .../tests/tools/TDS/TDS/TDSFeatureID.cs | 5 +++ 4 files changed, 48 insertions(+) diff --git a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.EndPoint/ITDSServerSession.cs b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.EndPoint/ITDSServerSession.cs index 0c9320f512..bbe039d426 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.EndPoint/ITDSServerSession.cs +++ b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.EndPoint/ITDSServerSession.cs @@ -77,5 +77,10 @@ public interface ITDSServerSession /// Indicates whether this session supports transport-level recovery /// bool IsSessionRecoveryEnabled { get; set; } + + /// + /// Indicates whether the client supports Json column type + /// + bool IsJsonSupportEnabled { get; set; } } } diff --git a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs index 535304964d..49adfd4cf6 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs +++ b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs @@ -230,6 +230,12 @@ public virtual TDSMessageCollection OnLogin7Request(ITDSServerSession session, T // Save the fed auth library to be used (session as GenericTDSServerSession).FederatedAuthenticationLibrary = federatedAuthenticationOption.Library; + break; + } + case TDSFeatureID.JsonSupport: + { + // Enable Json Support + session.IsJsonSupportEnabled = true; break; } default: @@ -544,6 +550,33 @@ protected virtual TDSMessageCollection OnAuthenticationCompleted(ITDSServerSessi responseMessage.Add(featureExtActToken); } + // Check if Json is supported + if (session.IsJsonSupportEnabled) + { + // Create ack data (2 bytes: Version number, Enabled) + byte[] data = new byte[2]; + data[0] = (byte)1; + data[1] = (byte)1; + + // Create Json support as a generic feature extension option + TDSFeatureExtAckGenericOption jsonSupportOption = new TDSFeatureExtAckGenericOption(TDSFeatureID.JsonSupport, 2, data); + + // Look for feature extension token + TDSFeatureExtAckToken featureExtAckToken = (TDSFeatureExtAckToken)responseMessage.Where(t => t is TDSFeatureExtAckToken).FirstOrDefault(); + + if (featureExtAckToken == null) + { + // Create feature extension ack token + featureExtAckToken = new TDSFeatureExtAckToken(jsonSupportOption); + responseMessage.Add(featureExtAckToken); + } + else + { + // Update the existing token + featureExtAckToken.Options.Add(jsonSupportOption); + } + } + // Create DONE token TDSDoneToken doneToken = new TDSDoneToken(TDSDoneTokenStatusType.Final); diff --git a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServerSession.cs b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServerSession.cs index 4c1de9f986..3272036e15 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServerSession.cs +++ b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServerSession.cs @@ -108,6 +108,11 @@ public class GenericTDSServerSession : ITDSServerSession /// public bool IsSessionRecoveryEnabled { get; set; } + /// + /// Indicates whether this session supports Json column type + /// + public bool IsJsonSupportEnabled { get; set; } + #region Session Options /// diff --git a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSFeatureID.cs b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSFeatureID.cs index bac86b591c..eb84a631d0 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSFeatureID.cs +++ b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSFeatureID.cs @@ -19,6 +19,11 @@ public enum TDSFeatureID : byte /// FederatedAuthentication = 0x02, + /// + /// JSON Support + /// + JsonSupport = 0x0D, + /// /// End of the list /// From add8664d031486694d54fd874d4f9881dee6b2f3 Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Tue, 11 Jun 2024 17:45:44 +0530 Subject: [PATCH 8/9] Remove enabled flag --- .../Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs | 8 ++------ .../Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs | 8 ++------ .../tests/tools/TDS/TDS.Servers/GenericTDSServer.cs | 7 +++---- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index ba8a2a8080..5050d9ea73 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -2817,7 +2817,7 @@ internal void OnFeatureExtAck(int featureId, byte[] data) case TdsEnums.FEATUREEXT_JSONSUPPORT: { SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, Received feature extension acknowledgement for JSONSUPPORT", ObjectID); - if (data.Length != 2) + if (data.Length != 1) { SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown token for JSONSUPPORT", ObjectID); throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); @@ -2828,11 +2828,7 @@ internal void OnFeatureExtAck(int featureId, byte[] data) SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); throw SQL.ParsingError(); } - byte enabled = data[1]; - if (enabled == 1) - { - _parser.IsJsonSupportEnabled = true; - } + _parser.IsJsonSupportEnabled = true; break; } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index da216b43a8..2297dd7ff2 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -3244,7 +3244,7 @@ internal void OnFeatureExtAck(int featureId, byte[] data) case TdsEnums.FEATUREEXT_JSONSUPPORT: { SqlClientEventSource.Log.TryAdvancedTraceEvent(" {0}, Received feature extension acknowledgement for JSONSUPPORT", ObjectID); - if (data.Length != 2) + if (data.Length != 1) { SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown token for JSONSUPPORT", ObjectID); throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); @@ -3255,11 +3255,7 @@ internal void OnFeatureExtAck(int featureId, byte[] data) SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); } - byte enabled = data[1]; - if (enabled == 1) - { - _parser.IsJsonSupportEnabled = true; - } + _parser.IsJsonSupportEnabled = true; break; } diff --git a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs index 49adfd4cf6..1a6e60fd08 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs +++ b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs @@ -553,13 +553,12 @@ protected virtual TDSMessageCollection OnAuthenticationCompleted(ITDSServerSessi // Check if Json is supported if (session.IsJsonSupportEnabled) { - // Create ack data (2 bytes: Version number, Enabled) - byte[] data = new byte[2]; + // Create ack data (1 byte: Version number) + byte[] data = new byte[1]; data[0] = (byte)1; - data[1] = (byte)1; // Create Json support as a generic feature extension option - TDSFeatureExtAckGenericOption jsonSupportOption = new TDSFeatureExtAckGenericOption(TDSFeatureID.JsonSupport, 2, data); + TDSFeatureExtAckGenericOption jsonSupportOption = new TDSFeatureExtAckGenericOption(TDSFeatureID.JsonSupport, (uint)data.Length, data); // Look for feature extension token TDSFeatureExtAckToken featureExtAckToken = (TDSFeatureExtAckToken)responseMessage.Where(t => t is TDSFeatureExtAckToken).FirstOrDefault(); From aaa9640608c1745646765d89933300b5cd8ad0cc Mon Sep 17 00:00:00 2001 From: Deepak Saini Date: Tue, 23 Jul 2024 18:49:53 +0530 Subject: [PATCH 9/9] address PR comments --- .../Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs | 9 ++++++--- .../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs | 2 -- .../Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs | 9 ++++++--- .../netfx/src/Microsoft/Data/SqlClient/TdsParser.cs | 2 -- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index 5050d9ea73..7f9f74577c 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -201,6 +201,9 @@ internal bool IsDNSCachingBeforeRedirectSupported internal SQLDNSInfo pendingSQLDNSObject = null; + // Json Support Flag + internal bool IsJsonSupportEnabled = false; + // TCE flags internal byte _tceVersionSupported; @@ -2822,13 +2825,13 @@ internal void OnFeatureExtAck(int featureId, byte[] data) SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown token for JSONSUPPORT", ObjectID); throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); } - byte JsonSupportVersion = data[0]; - if (JsonSupportVersion == 0 || JsonSupportVersion > 1) + byte jsonSupportVersion = data[0]; + if (jsonSupportVersion == 0 || jsonSupportVersion > 1) { SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); throw SQL.ParsingError(); } - _parser.IsJsonSupportEnabled = true; + IsJsonSupportEnabled = true; break; } 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 ce40b30bb6..e43bc94952 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 @@ -194,8 +194,6 @@ internal static void Assert(string message) /// internal int DataClassificationVersion { get; set; } - internal bool IsJsonSupportEnabled = false; - private SqlCollation _cachedCollation; internal TdsParser(bool MARS, bool fAsynchronous) diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs index 2297dd7ff2..672eafe472 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs @@ -212,6 +212,9 @@ internal bool IsDNSCachingBeforeRedirectSupported internal SQLDNSInfo pendingSQLDNSObject = null; + // Json Support Flag + internal bool IsJsonSupportEnabled = false; + // TCE flags internal byte _tceVersionSupported; @@ -3249,13 +3252,13 @@ internal void OnFeatureExtAck(int featureId, byte[] data) SqlClientEventSource.Log.TryTraceEvent(" {0}, Unknown token for JSONSUPPORT", ObjectID); throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); } - byte JsonSupportVersion = data[0]; - if (JsonSupportVersion == 0 || JsonSupportVersion > 1) + byte jsonSupportVersion = data[0]; + if (jsonSupportVersion == 0 || jsonSupportVersion > 1) { SqlClientEventSource.Log.TryTraceEvent(" {0}, Invalid version number for JSONSUPPORT", ObjectID); throw SQL.ParsingError(ParsingErrorState.CorruptedTdsStream); } - _parser.IsJsonSupportEnabled = true; + IsJsonSupportEnabled = true; 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 4c814524a9..639e6f5a98 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 @@ -311,8 +311,6 @@ internal bool IsDataClassificationEnabled /// internal int DataClassificationVersion { get; set; } - internal bool IsJsonSupportEnabled = false; - private SqlCollation _cachedCollation; internal TdsParser(bool MARS, bool fAsynchronous)