From 4c7ec24d125a14c118763e6629a02e5f34d0b7c6 Mon Sep 17 00:00:00 2001
From: Edward Neal <55035479+edwardneal@users.noreply.github.com>
Date: Fri, 14 Nov 2025 18:47:31 +0000
Subject: [PATCH] Serialize UserAgentInfoDto using source-generated serializer
---
.../netcore/src/Microsoft.Data.SqlClient.csproj | 3 +++
.../netfx/src/Microsoft.Data.SqlClient.csproj | 3 +++
.../Data/SqlClient/UserAgent/UserAgentInfo.cs | 12 +++---------
.../UserAgentInfoDtoSerializerContext.cs | 15 +++++++++++++++
4 files changed, 24 insertions(+), 9 deletions(-)
create mode 100644 src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfoDtoSerializerContext.cs
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
index de98172898..39b19b783f 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
@@ -840,6 +840,9 @@
Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs
+
+ Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs
+
Resources\ResCategoryAttribute.cs
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
index be475493fb..6890e1e549 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
@@ -1004,6 +1004,9 @@
Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs
+
+ Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs
+
Resources\ResDescriptionAttribute.cs
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs
index d927155dbb..eb2d434b2c 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs
@@ -133,13 +133,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto)
// - If the payload exceeds 2,047 bytes but remains within sensible limits, we still send it, but note that
// some servers may silently drop or reject such packets — behavior we may use for future probing or diagnostics.
// - If payload exceeds 10KB even after dropping fields , we send an empty payload.
- var options = new JsonSerializerOptions
- {
- PropertyNamingPolicy = null,
- DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
- WriteIndented = false
- };
- byte[] payload = JsonSerializer.SerializeToUtf8Bytes(dto, options);
+ byte[] payload = JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto);
// We try to send the payload if it is within the limits.
// Otherwise we drop some fields to reduce the size of the payload and try one last time
@@ -157,7 +151,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto)
dto.OS.Details = null; // drop OS.Details
}
- payload = JsonSerializer.SerializeToUtf8Bytes(dto, options);
+ payload = JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto);
if (payload.Length <= JsonPayloadMaxBytes)
{
return payload;
@@ -166,7 +160,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto)
dto.OS = null; // drop OS entirely
// Last attempt to send minimal payload driver + version only
// As per the comment in AdjustJsonPayloadSize, we know driver + version cannot be larger than the max
- return JsonSerializer.SerializeToUtf8Bytes(dto, options);
+ return JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto);
}
internal static UserAgentInfoDto BuildDto()
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfoDtoSerializerContext.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfoDtoSerializerContext.cs
new file mode 100644
index 0000000000..25af2cd2d6
--- /dev/null
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfoDtoSerializerContext.cs
@@ -0,0 +1,15 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System.Text.Json.Serialization;
+
+#nullable enable
+
+namespace Microsoft.Data.SqlClient.UserAgent;
+
+[JsonSourceGenerationOptions(WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.Unspecified)]
+[JsonSerializable(typeof(UserAgentInfoDto), GenerationMode = JsonSourceGenerationMode.Serialization)]
+internal sealed partial class UserAgentInfoDtoSerializerContext : JsonSerializerContext
+{
+}