Align AI schema $ref pointer escaping with updated STJ behavior#7640
$ref pointer escaping with updated STJ behavior#7640Conversation
$ref pointer escaping with updated STJ behavior
There was a problem hiding this comment.
Pull request overview
Updates AI JSON schema $ref generation so JSON Pointer reference-token escaping matches System.Text.Json’s current behavior by applying RFC 6901 token escaping followed by URI-fragment percent-encoding. This prevents under-encoded $ref fragments when parameter names contain %-style sequences.
Changes:
- Updated
EscapeJsonPointerSegmentto token-escape (~//) and then percent-encode viaUri.EscapeDataString. - Added regression coverage asserting
%sequences are double-encoded in$reffragments (e.g.,hello%20world→hello%2520world) in both impacted test suites.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs | Adds regression test ensuring % in parameter names is percent-encoded in emitted $ref fragments. |
| test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs | Adds regression test for percent-encoding behavior in AIJsonUtilities.CreateFunctionJsonSchema. |
| src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs | Updates JSON Pointer segment escaping to include URI percent-encoding after RFC 6901 token escaping. |
|
The percent-encoding here matches the JsonSchemaExporter change in dotnet/runtime#130164, but that change is on runtime main (11.0) and this repo still pins System.Text.Json at 9.0.18 / 10.0.10 / 8.0.6, none of which percent-encode yet. So until an STJ build with #130164 flows in, the prepended The top-level property segment is fine on its own since it points at a raw property key and percent-encoding is the correct way to reference it, so real impact is limited to exotic names. But should we hold this until the STJ bump lands, or is there a reason to take it ahead of the dependency? Just want to make sure we don't ship a half-encoded pointer in the interim. |
|
I thought it was fine to fix this ahead of time for MEAI, but I think the concern is valid. Let's wait until S.T.J ships #130164 and then conditionally align the fix with the STJ version — percent-encoding only when the runtime STJ also percent-encodes the exporter tail, so we don't ship a half-encoded pointer in the interim. Thanks. |
System.Text.Jsonnow percent-encodes JSON Pointer reference tokens when emitting$refURI fragments. Our AI schema fix-up only applied~0/~1token escaping to the prepended parameter segment, which could produce under-encoded references inconsistent with the rest of the document.Schema
$refalignmentEscapeJsonPointerSegmentto match STJ's current behavior:~→~0,/→~1)Uri.EscapeDataString$reftail passthrough unchanged so exporter-produced inner pointer segments are not double-encoded.Regression coverage
a/b~ccase to verify slash/tilde token escaping remains stable.hello%20world, asserting the emitted segment becomeshello%2520world.Resulting behavior
Example output change for a parameter named
hello%20world: