From e7a91281383471b7c40c2bb88b4e797417a9eb97 Mon Sep 17 00:00:00 2001 From: westey <164392973+westey-m@users.noreply.github.com> Date: Fri, 17 Oct 2025 15:58:50 +0100 Subject: [PATCH] Force source generation tests in .net core (#1298) --- .../AgentAbstractionsJsonUtilitiesTests.cs | 4 ++++ .../InMemoryAgentThreadTests.cs | 5 +++-- ...icrosoft.Agents.AI.Abstractions.UnitTests.csproj | 4 ++++ .../ServiceIdAgentThreadTests.cs | 13 ++++++++++--- .../TestJsonSerializerContext.cs | 4 ++++ .../AgentJsonUtilitiesTests.cs | 4 ++++ .../Microsoft.Agents.AI.UnitTests.csproj | 4 ++++ 7 files changed, 33 insertions(+), 5 deletions(-) diff --git a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentAbstractionsJsonUtilitiesTests.cs b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentAbstractionsJsonUtilitiesTests.cs index 82e4dab0ec..e286796243 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentAbstractionsJsonUtilitiesTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentAbstractionsJsonUtilitiesTests.cs @@ -55,6 +55,9 @@ public class AgentAbstractionsJsonUtilitiesTests Assert.Equal(JsonSerializer.IsReflectionEnabledByDefault, AgentAbstractionsJsonUtilities.DefaultOptions.TryGetTypeInfo(anonType, out _)); } + // The following two tests validate behaviors of reflection-based serialization + // which is only available in .NET Framework builds. +#if NETFRAMEWORK [Fact] public void DefaultOptions_AllowsReadingNumbersFromStrings_AndOmitsNulls() { @@ -73,6 +76,7 @@ public class AgentAbstractionsJsonUtilitiesTests { Assert.Equal("\"Monday\"", JsonSerializer.Serialize(DayOfWeek.Monday, AgentAbstractionsJsonUtilities.DefaultOptions)); } +#endif [Fact] public void DefaultOptions_UsesCamelCasePropertyNames_ForAgentRunResponse() diff --git a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/InMemoryAgentThreadTests.cs b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/InMemoryAgentThreadTests.cs index f0fde6fc61..906db4d30c 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/InMemoryAgentThreadTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/InMemoryAgentThreadTests.cs @@ -62,7 +62,8 @@ public class InMemoryAgentThreadTests // Arrange InMemoryChatMessageStore store = [new(ChatRole.User, "TestMsg")]; var storeState = store.Serialize(); - var json = JsonSerializer.SerializeToElement(new { storeState }); + var threadStateWrapper = new InMemoryAgentThread.InMemoryAgentThreadState { StoreState = storeState }; + var json = JsonSerializer.SerializeToElement(threadStateWrapper, TestJsonSerializerContext.Default.InMemoryAgentThreadState); // Act var thread = new TestInMemoryAgentThread(json); @@ -77,7 +78,7 @@ public class InMemoryAgentThreadTests public void Constructor_WithInvalidJson_ThrowsArgumentException() { // Arrange - var invalidJson = JsonSerializer.SerializeToElement(42); + var invalidJson = JsonSerializer.SerializeToElement(42, TestJsonSerializerContext.Default.Int32); // Act & Assert Assert.Throws(() => new TestInMemoryAgentThread(invalidJson)); diff --git a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/Microsoft.Agents.AI.Abstractions.UnitTests.csproj b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/Microsoft.Agents.AI.Abstractions.UnitTests.csproj index 0d4df17c8f..b7c5412a53 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/Microsoft.Agents.AI.Abstractions.UnitTests.csproj +++ b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/Microsoft.Agents.AI.Abstractions.UnitTests.csproj @@ -5,6 +5,10 @@ $(NoWarn);MEAI001 + + false + + diff --git a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ServiceIdAgentThreadTests.cs b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ServiceIdAgentThreadTests.cs index 8999b0abe4..e451359c23 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ServiceIdAgentThreadTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ServiceIdAgentThreadTests.cs @@ -36,7 +36,8 @@ public class ServiceIdAgentThreadTests public void Constructor_WithSerializedId_SetsProperty() { // Arrange - var json = JsonSerializer.SerializeToElement(new { ServiceThreadId = "service-id-456" }); + var serviceThreadWrapper = new ServiceIdAgentThread.ServiceIdAgentThreadState { ServiceThreadId = "service-id-456" }; + var json = JsonSerializer.SerializeToElement(serviceThreadWrapper, TestJsonSerializerContext.Default.ServiceIdAgentThreadState); // Act var thread = new TestServiceIdAgentThread(json); @@ -49,7 +50,8 @@ public class ServiceIdAgentThreadTests public void Constructor_WithSerializedUndefinedId_SetsProperty() { // Arrange - var json = JsonSerializer.SerializeToElement(new { }); + var emptyObject = new EmptyObject(); + var json = JsonSerializer.SerializeToElement(emptyObject, TestJsonSerializerContext.Default.EmptyObject); // Act var thread = new TestServiceIdAgentThread(json); @@ -62,7 +64,7 @@ public class ServiceIdAgentThreadTests public void Constructor_WithInvalidJson_ThrowsArgumentException() { // Arrange - var invalidJson = JsonSerializer.SerializeToElement(42); + var invalidJson = JsonSerializer.SerializeToElement(42, TestJsonSerializerContext.Default.Int32); // Act & Assert Assert.Throws(() => new TestServiceIdAgentThread(invalidJson)); @@ -111,4 +113,9 @@ public class ServiceIdAgentThreadTests public TestServiceIdAgentThread(JsonElement serializedThreadState) : base(serializedThreadState) { } public string? GetServiceThreadId() => this.ServiceThreadId; } + + // Helper class to represent empty objects + internal sealed class EmptyObject + { + } } diff --git a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/TestJsonSerializerContext.cs b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/TestJsonSerializerContext.cs index 0c8703028f..b7c553d348 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/TestJsonSerializerContext.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/TestJsonSerializerContext.cs @@ -18,4 +18,8 @@ namespace Microsoft.Agents.AI.Abstractions.UnitTests; [JsonSerializable(typeof(JsonElement))] [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(string[]))] +[JsonSerializable(typeof(int))] +[JsonSerializable(typeof(InMemoryAgentThread.InMemoryAgentThreadState))] +[JsonSerializable(typeof(ServiceIdAgentThread.ServiceIdAgentThreadState))] +[JsonSerializable(typeof(ServiceIdAgentThreadTests.EmptyObject))] internal sealed partial class TestJsonSerializerContext : JsonSerializerContext; diff --git a/dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentJsonUtilitiesTests.cs b/dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentJsonUtilitiesTests.cs index 0be7b91dea..65815607d9 100644 --- a/dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentJsonUtilitiesTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentJsonUtilitiesTests.cs @@ -55,6 +55,9 @@ public class AgentJsonUtilitiesTests Assert.Equal(JsonSerializer.IsReflectionEnabledByDefault, AgentJsonUtilities.DefaultOptions.TryGetTypeInfo(anonType, out _)); } + // The following two tests validate behaviors of reflection-based serialization + // which is only available in .NET Framework builds. +#if NETFRAMEWORK [Fact] public void DefaultOptions_AllowsReadingNumbersFromStrings_AndOmitsNulls() { @@ -73,6 +76,7 @@ public class AgentJsonUtilitiesTests { Assert.Equal("\"Monday\"", JsonSerializer.Serialize(DayOfWeek.Monday, AgentJsonUtilities.DefaultOptions)); } +#endif [Fact] public void DefaultOptions_UsesCamelCasePropertyNames_ForAgentRunResponse() diff --git a/dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj b/dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj index ef5f827d76..f871781d03 100644 --- a/dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj +++ b/dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj @@ -4,6 +4,10 @@ $(ProjectsTargetFrameworks) + + false + +