From dec9126a941f26ae50d80c6595a15dcafb1bfb94 Mon Sep 17 00:00:00 2001 From: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> Date: Wed, 25 Sep 2024 20:38:12 +0100 Subject: [PATCH] .Net: Unit test for KernelJsonSchemaBuilder (#8989) Make sure the `KernelJsonSchemaBuilder` can build schemas for types having public properties of `object` or/and `KernelJsonSchema` types that have default values in the constructor. This test verifies that the fix https://github.com/microsoft/semantic-kernel/pull/8988 works and prevents future regressions of this scenario. --- .../Utilities/KernelJsonSchemaBuilderTests.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 dotnet/src/SemanticKernel.UnitTests/Utilities/KernelJsonSchemaBuilderTests.cs diff --git a/dotnet/src/SemanticKernel.UnitTests/Utilities/KernelJsonSchemaBuilderTests.cs b/dotnet/src/SemanticKernel.UnitTests/Utilities/KernelJsonSchemaBuilderTests.cs new file mode 100644 index 000000000000..6079979db21a --- /dev/null +++ b/dotnet/src/SemanticKernel.UnitTests/Utilities/KernelJsonSchemaBuilderTests.cs @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.SemanticKernel; +using Xunit; + +namespace SemanticKernel.UnitTests.Utilities; + +public class KernelJsonSchemaBuilderTests +{ + [Fact] + public void ItShouldBuildJsonSchemaForTypesWithPublicMembersHavingTypesThatCanRepresentOtherTypesWithDefaultValuesInTheConstructor() + { + // Arrange & Act + var schema = KernelJsonSchemaBuilder.Build(null, typeof(ClassWithDefaultValuesInConstructorForTypesThatCanRepresentOtherTypes)); + + // Assert + Assert.NotNull(schema?.RootElement); + } + +#pragma warning disable CA1812 // Instantiated by reflection + private sealed class ClassWithDefaultValuesInConstructorForTypesThatCanRepresentOtherTypes + { + public ClassWithDefaultValuesInConstructorForTypesThatCanRepresentOtherTypes(object? content = null, KernelJsonSchema? schema = null) + { + this.Content = content; + this.Schema = schema; + } + + public object? Content { get; set; } + + public KernelJsonSchema? Schema { get; set; } + } +#pragma warning restore CA1812 // Instantiated by reflection +}