Skip to content

Add test for optional parameters being required with RequireAllProperties #6265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static void CreateJsonSchema_DefaultParameters_GeneratesExpectedJsonSchem

JsonElement actual = AIJsonUtilities.CreateJsonSchema(typeof(MyPoco), serializerOptions: JsonContext.Default.Options);

Assert.True(DeepEquals(expected, actual));
AssertDeepEquals(expected, actual);
}

[Fact]
Expand Down Expand Up @@ -204,7 +204,7 @@ public static void CreateJsonSchema_OverriddenParameters_GeneratesExpectedJsonSc
serializerOptions: JsonContext.Default.Options,
inferenceOptions: inferenceOptions);

Assert.True(DeepEquals(expected, actual));
AssertDeepEquals(expected, actual);
}

[Fact]
Expand Down Expand Up @@ -249,7 +249,7 @@ public static void CreateJsonSchema_UserDefinedTransformer()

JsonElement actual = AIJsonUtilities.CreateJsonSchema(typeof(MyPoco), serializerOptions: JsonContext.Default.Options, inferenceOptions: inferenceOptions);

Assert.True(DeepEquals(expected, actual));
AssertDeepEquals(expected, actual);
}

[Fact]
Expand Down Expand Up @@ -277,7 +277,7 @@ public static void CreateJsonSchema_FiltersDisallowedKeywords()

JsonElement actual = AIJsonUtilities.CreateJsonSchema(typeof(PocoWithTypesWithOpenAIUnsupportedKeywords), serializerOptions: JsonContext.Default.Options);

Assert.True(DeepEquals(expected, actual));
AssertDeepEquals(expected, actual);
}

public class PocoWithTypesWithOpenAIUnsupportedKeywords
Expand All @@ -301,7 +301,67 @@ public static void CreateFunctionJsonSchema_ReturnsExpectedValue()
Assert.NotNull(func.UnderlyingMethod);

JsonElement resolvedSchema = AIJsonUtilities.CreateFunctionJsonSchema(func.UnderlyingMethod, title: func.Name);
Assert.True(DeepEquals(resolvedSchema, func.JsonSchema));
AssertDeepEquals(resolvedSchema, func.JsonSchema);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public static void CreateFunctionJsonSchema_OptionalParameters(bool requireAllProperties)
{
string unitJsonSchema = requireAllProperties ? """
{
"description": "The unit to calculate the current temperature to (Default value: \u0022celsius\u0022)",
"type": "string"
}
""" :
"""
{
"description": "The unit to calculate the current temperature to",
"type": "string",
"default": "celsius"
}
""";

string requiredParamsJsonSchema = requireAllProperties ?
"""["city", "unit"]""" :
"""["city"]""";

JsonElement expected = JsonDocument.Parse($$"""
{
"title": "get_weather",
"description": "Gets the current weather for a current location",
"type": "object",
"properties": {
"city": {
"description": "The city to get the weather for",
"type": "string"
},
"unit": {{unitJsonSchema}}
},
"required": {{requiredParamsJsonSchema}}
}
""").RootElement;

AIFunction func = AIFunctionFactory.Create((
[Description("The city to get the weather for")] string city,
[Description("The unit to calculate the current temperature to")] string unit = "celsius") => "sunny",
new AIFunctionFactoryOptions
{
Name = "get_weather",
Description = "Gets the current weather for a current location",
JsonSchemaCreateOptions = new AIJsonSchemaCreateOptions { RequireAllProperties = requireAllProperties }
});

Assert.NotNull(func.UnderlyingMethod);
AssertDeepEquals(expected, func.JsonSchema);

JsonElement resolvedSchema = AIJsonUtilities.CreateFunctionJsonSchema(
func.UnderlyingMethod,
title: func.Name,
description: func.Description,
inferenceOptions: new AIJsonSchemaCreateOptions { RequireAllProperties = requireAllProperties });
AssertDeepEquals(expected, resolvedSchema);
}

[Fact]
Expand Down Expand Up @@ -331,7 +391,7 @@ public static void CreateFunctionJsonSchema_TreatsIntegralTypesAsInteger_EvenWit
""").RootElement;

JsonElement actualSchema = property.Value;
Assert.True(DeepEquals(expected, actualSchema));
AssertDeepEquals(expected, actualSchema);
i++;
}
}
Expand Down Expand Up @@ -514,4 +574,17 @@ private static bool DeepEquals(JsonElement element1, JsonElement element2)
JsonSerializer.SerializeToNode(element2, AIJsonUtilities.DefaultOptions));
#endif
}

private static void AssertDeepEquals(JsonElement element1, JsonElement element2)
{
#pragma warning disable SA1118 // Parameter should not span multiple lines
Assert.True(DeepEquals(element1, element2), $"""
Elements are not equal.
Expected:
{element1}
Actual:
{element2}
""");
#pragma warning restore SA1118 // Parameter should not span multiple lines
}
}
Loading