Skip to content

Commit 79d8597

Browse files
Copilotstephentoub
andauthored
Fix operator precedence bug in ValidateSchemaDocument causing rejection of valid boolean schemas (#7066)
* Initial plan * Fix operator precedence bug in ValidateSchemaDocument and add tests Co-authored-by: stephentoub <[email protected]> * Replace reflection-based tests with public API tests Co-authored-by: stephentoub <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: stephentoub <[email protected]>
1 parent cba42b4 commit 79d8597

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public static JsonElement CreateJsonSchema(
193193
/// <summary>Validates the provided JSON schema document.</summary>
194194
internal static void ValidateSchemaDocument(JsonElement document, [CallerArgumentExpression("document")] string? paramName = null)
195195
{
196-
if (document.ValueKind is not JsonValueKind.Object or JsonValueKind.False or JsonValueKind.True)
196+
if (document.ValueKind is not (JsonValueKind.Object or JsonValueKind.False or JsonValueKind.True))
197197
{
198198
Throw.ArgumentException(paramName ?? "schema", "The schema document must be an object or a boolean value.");
199199
}

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,34 @@ public static void TransformJsonSchema_InvalidInput_ThrowsArgumentException(stri
14161416
Assert.Throws<ArgumentException>("schema", () => AIJsonUtilities.TransformSchema(schema, transformOptions));
14171417
}
14181418

1419+
[Theory]
1420+
[InlineData("true")]
1421+
[InlineData("false")]
1422+
public static void TransformJsonSchema_BooleanSchemas_Success(string booleanSchema)
1423+
{
1424+
// Boolean schemas (true/false) are valid JSON schemas per the spec.
1425+
// This test verifies they are accepted by TransformSchema.
1426+
JsonElement schema = JsonDocument.Parse(booleanSchema).RootElement;
1427+
AIJsonSchemaTransformOptions transformOptions = new() { ConvertBooleanSchemas = true };
1428+
1429+
// Should not throw - boolean schemas are valid
1430+
JsonElement result = AIJsonUtilities.TransformSchema(schema, transformOptions);
1431+
1432+
// Verify the transformation happened correctly
1433+
if (booleanSchema == "true")
1434+
{
1435+
// 'true' schema should be converted to empty object
1436+
Assert.Equal(JsonValueKind.Object, result.ValueKind);
1437+
}
1438+
else
1439+
{
1440+
// 'false' schema should be converted to {"not": true}
1441+
Assert.Equal(JsonValueKind.Object, result.ValueKind);
1442+
Assert.True(result.TryGetProperty("not", out JsonElement notValue));
1443+
Assert.Equal(JsonValueKind.True, notValue.ValueKind);
1444+
}
1445+
}
1446+
14191447
private class DerivedAIContent : AIContent
14201448
{
14211449
public int DerivedValue { get; set; }

0 commit comments

Comments
 (0)