Skip to content

Commit bc3d519

Browse files
authored
Serialize UserAgentInfoDto using source-generated serializer (#3772)
1 parent 6e7d58f commit bc3d519

4 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,9 @@
995995
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs">
996996
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs</Link>
997997
</Compile>
998+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs">
999+
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs</Link>
1000+
</Compile>
9981001
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs">
9991002
<Link>Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs</Link>
10001003
</Compile>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,9 @@
10021002
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs">
10031003
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDto.cs</Link>
10041004
</Compile>
1005+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs">
1006+
<Link>Microsoft\Data\SqlClient\UserAgent\UserAgentInfoDtoSerializerContext.cs</Link>
1007+
</Compile>
10051008
<Compile Include="$(CommonSourceRoot)Resources\ResCategoryAttribute.cs">
10061009
<Link>Resources\ResCategoryAttribute.cs</Link>
10071010
</Compile>

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/UserAgent/UserAgentInfo.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto)
133133
// - If the payload exceeds 2,047 bytes but remains within sensible limits, we still send it, but note that
134134
// some servers may silently drop or reject such packets — behavior we may use for future probing or diagnostics.
135135
// - If payload exceeds 10KB even after dropping fields , we send an empty payload.
136-
var options = new JsonSerializerOptions
137-
{
138-
PropertyNamingPolicy = null,
139-
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
140-
WriteIndented = false
141-
};
142-
byte[] payload = JsonSerializer.SerializeToUtf8Bytes(dto, options);
136+
byte[] payload = JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto);
143137

144138
// We try to send the payload if it is within the limits.
145139
// 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)
157151
dto.OS.Details = null; // drop OS.Details
158152
}
159153

160-
payload = JsonSerializer.SerializeToUtf8Bytes(dto, options);
154+
payload = JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto);
161155
if (payload.Length <= JsonPayloadMaxBytes)
162156
{
163157
return payload;
@@ -166,7 +160,7 @@ internal static byte[] AdjustJsonPayloadSize(UserAgentInfoDto dto)
166160
dto.OS = null; // drop OS entirely
167161
// Last attempt to send minimal payload driver + version only
168162
// As per the comment in AdjustJsonPayloadSize, we know driver + version cannot be larger than the max
169-
return JsonSerializer.SerializeToUtf8Bytes(dto, options);
163+
return JsonSerializer.SerializeToUtf8Bytes(dto, UserAgentInfoDtoSerializerContext.Default.UserAgentInfoDto);
170164
}
171165

172166
internal static UserAgentInfoDto BuildDto()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Text.Json.Serialization;
6+
7+
#nullable enable
8+
9+
namespace Microsoft.Data.SqlClient.UserAgent;
10+
11+
[JsonSourceGenerationOptions(WriteIndented = false, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.Unspecified)]
12+
[JsonSerializable(typeof(UserAgentInfoDto), GenerationMode = JsonSourceGenerationMode.Serialization)]
13+
internal sealed partial class UserAgentInfoDtoSerializerContext : JsonSerializerContext
14+
{
15+
}

0 commit comments

Comments
 (0)