Force source generation tests in .net core (#1298)

This commit is contained in:
westey
2025-10-17 15:58:50 +01:00
committed by GitHub
Unverified
parent 151efd2e80
commit e7a9128138
7 changed files with 33 additions and 5 deletions
@@ -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()
@@ -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<ArgumentException>(() => new TestInMemoryAgentThread(invalidJson));
@@ -5,6 +5,10 @@
<NoWarn>$(NoWarn);MEAI001</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Agents.AI.Abstractions\Microsoft.Agents.AI.Abstractions.csproj" />
</ItemGroup>
@@ -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<ArgumentException>(() => 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
{
}
}
@@ -18,4 +18,8 @@ namespace Microsoft.Agents.AI.Abstractions.UnitTests;
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(Dictionary<string, object?>))]
[JsonSerializable(typeof(string[]))]
[JsonSerializable(typeof(int))]
[JsonSerializable(typeof(InMemoryAgentThread.InMemoryAgentThreadState))]
[JsonSerializable(typeof(ServiceIdAgentThread.ServiceIdAgentThreadState))]
[JsonSerializable(typeof(ServiceIdAgentThreadTests.EmptyObject))]
internal sealed partial class TestJsonSerializerContext : JsonSerializerContext;
@@ -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()
@@ -4,6 +4,10 @@
<TargetFrameworks>$(ProjectsTargetFrameworks)</TargetFrameworks>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Agents.AI\Microsoft.Agents.AI.csproj" />
<ProjectReference Include="..\..\src\Microsoft.Agents.AI.CopilotStudio\Microsoft.Agents.AI.CopilotStudio.csproj" />