Skip to content

fix: sanitize JSON Schema $defs keys to produce valid $ref URI references#77

Open
lior1997k wants to merge 2 commits intoIvanMurzak:mainfrom
lior1997k:main
Open

fix: sanitize JSON Schema $defs keys to produce valid $ref URI references#77
lior1997k wants to merge 2 commits intoIvanMurzak:mainfrom
lior1997k:main

Conversation

@lior1997k
Copy link
Copy Markdown

Summary

GetSchemaTypeId() returns raw C# type names (e.g. System.String[], IEnumerable<System.Int32>) which are used as $defs keys in JSON Schema. These contain characters ([ ] < >) that violate RFC 3986 URI-reference rules, producing invalid $ref values.

Changes

  • TypeUtils.Name.cs: GetSchemaTypeId() now percent-encodes [ ] < > characters before returning the type ID
  • TestSchemaTypeId.cs: Updated assertions to match the new sanitized output

Testing

All 1,423 tests pass on both net8.0 and net9.0.

Fixes IvanMurzak/Unity-MCP#715

GetSchemaTypeId() now percent-encodes [ ] < > characters in type IDs to produce valid JSON Schema $ref URI-reference keys. This fixes invalid $defs keys like System.String[] and IEnumerable<System.Int32> which violate RFC 3986.

Also updates TestSchemaTypeId assertions to match the new sanitized output.

Ultraworked with Sisyphus

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
@IvanMurzak IvanMurzak added the enhancement New feature or request label May 4, 2026
@IvanMurzak
Copy link
Copy Markdown
Owner

@lior1997k that is an interesting solution! Thanks for fixing it.
There is also a symbol + which is used in C# for a nested class. Need to cover it as well.

Copy link
Copy Markdown
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 aims to make JSON Schema $ref values valid URI references by sanitizing schema type identifiers derived from C# type names (notably array and generic syntax).

Changes:

  • Update TypeUtils.GetSchemaTypeId(Type) to percent-encode [ ] < > in the returned schema type ID.
  • Update TestSchemaTypeId assertions to expect the new percent-encoded output.

Reviewed changes

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

File Description
ReflectorNet/src/Utils/TypeUtils.Name.cs Adds SanitizeForJsonSchemaRef() and routes GetSchemaTypeId through it to percent-encode certain characters.
ReflectorNet.Tests/src/SchemaTests/TestSchemaTypeId.cs Updates expected strings for arrays/generics to use %5B%5D and %3C%3E.

Comment on lines +129 to +130
return typeId.Replace("[", "%5B").Replace("]", "%5D")
.Replace("<", "%3C").Replace(">", "%3E");
Comment on lines 20 to 22
// Assert
Assert.Equal($"System.Int32{TypeUtils.ArraySuffix}", result);
Assert.Equal("System.Int32%5B%5D", result);
_output.WriteLine($"int[] -> {result}");
@IvanMurzak
Copy link
Copy Markdown
Owner

IvanMurzak commented May 10, 2026

IMPORTANT after sanitization need to check that the new Json Schema works well for Anthropic / Google / Open AI latest and close the the latest models.

@IvanMurzak
Copy link
Copy Markdown
Owner

Related issue: IvanMurzak/Unity-MCP#668

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid JSON Schema in tools with C# array types

3 participants