Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 21, 2025

The ValidateSchemaDocument method incorrectly rejected valid boolean schemas (true and false) due to operator precedence in the pattern matching expression.

Problem

// Before: parsed as "is (not Object) or False or True"
if (document.ValueKind is not JsonValueKind.Object or JsonValueKind.False or JsonValueKind.True)

This evaluated to true when the schema was False or True, throwing exceptions for valid JSON schemas per the spec.

Changes

  • Fixed operator precedence with explicit grouping:

    if (document.ValueKind is not (JsonValueKind.Object or JsonValueKind.False or JsonValueKind.True))
  • Added tests using the public TransformSchema API to verify top-level boolean schemas (true, false) are correctly accepted and transformed. The new test TransformJsonSchema_BooleanSchemas_Success validates that:

    • true schemas are converted to empty objects {}
    • false schemas are converted to {"not": true}
Original prompt

AIJsonSchemaCreateOptions contains this code:
internal static void ValidateSchemaDocument(JsonElement document, [CallerArgumentExpression("document")] string? paramName = null)
{
if (document.ValueKind is not JsonValueKind.Object or JsonValueKind.False or JsonValueKind.True)
{
Throw.ArgumentException(paramName ?? "schema", "The schema document must be an object or a boolean value.");
}
}
Those ors look wrong. Should they be and not instead? Please confirm, and if this is a bug, fix the bug and add tests the would have failed before the fix and now pass.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Microsoft Reviewers: Open in CodeFlow

Copilot AI changed the title [WIP] Fix bug in schema validation logic Fix operator precedence bug in ValidateSchemaDocument causing rejection of valid boolean schemas Nov 21, 2025
Copilot AI requested a review from stephentoub November 21, 2025 02:40
@stephentoub stephentoub marked this pull request as ready for review November 21, 2025 02:56
@stephentoub stephentoub requested a review from a team as a code owner November 21, 2025 02:56
Copilot AI review requested due to automatic review settings November 21, 2025 02:56
@stephentoub stephentoub enabled auto-merge (squash) November 21, 2025 02:56
Copilot AI requested a review from stephentoub November 21, 2025 02:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a critical operator precedence bug in the ValidateSchemaDocument method that was incorrectly rejecting valid boolean JSON schemas (true/false). The issue stemmed from C#'s operator precedence rules incorrectly parsing the validation condition.

Key changes:

  • Fixed the validation logic by adding parentheses to correctly express the intended condition
  • Added comprehensive test coverage for boolean schema validation using the public TransformSchema API

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs Corrected operator precedence in ValidateSchemaDocument by wrapping the or-pattern in parentheses
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs Added test to verify boolean schemas (true/false) are correctly accepted and transformed

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh.

@stephentoub stephentoub merged commit 79d8597 into main Nov 21, 2025
11 of 12 checks passed
@stephentoub stephentoub deleted the copilot/fix-schema-validation-logic branch November 21, 2025 08:59
stephentoub added a commit to jeffhandley/extensions that referenced this pull request Nov 21, 2025
…on of valid boolean schemas (dotnet#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]>
jeffhandley pushed a commit that referenced this pull request Nov 21, 2025
…on 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]>
This was referenced Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants