Skip to content

Fix/nested class ref sanitization#78

Closed
lior1997k wants to merge 5 commits into
IvanMurzak:mainfrom
lior1997k:fix/nested-class-ref-sanitization
Closed

Fix/nested class ref sanitization#78
lior1997k wants to merge 5 commits into
IvanMurzak:mainfrom
lior1997k:fix/nested-class-ref-sanitization

Conversation

@lior1997k

Copy link
Copy Markdown
Contributor

No description provided.

lior1997k and others added 3 commits May 2, 2026 16:38
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>
@lior1997k

Copy link
Copy Markdown
Contributor Author

Thanks for catching the nested-class case.

I opened this follow-up PR to encode +, since C# nested classes use it in reflection type names. I also updated the related schema type ID tests for arrays and generics so the generated IDs are safe for JSON Schema $ref URI references.

Let me know if this direction looks good or if you prefer another encoding approach.

@IvanMurzak IvanMurzak requested a review from Copilot May 4, 2026 07:51
@IvanMurzak IvanMurzak added bug Something isn't working enhancement New feature or request labels May 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates how schema type identifiers are produced so that nested types, arrays, and generics generate “sanitized” IDs intended to be safer when used inside JSON Schema $ref fragments.

Changes:

  • Route GetSchemaTypeId(...) through a new sanitization step that percent-encodes certain characters ([]<>+).
  • Add a new test for nested type IDs and update existing schema type-id tests to assert the new encoded outputs.

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 Changes GetSchemaTypeId to return a percent-encoded variant of GetTypeId, adding SanitizeForJsonSchemaRef.
ReflectorNet.Tests/src/SchemaTests/TestSchemaTypeId.cs Adds coverage for nested types and updates expectations for arrays/generics to match the newly encoded IDs.

Comment on lines +118 to +131
public static string GetSchemaTypeId<T>() => GetSchemaTypeId(typeof(T));
public static string GetSchemaTypeId(Type type)
{
var typeId = GetTypeId(type);
return SanitizeForJsonSchemaRef(typeId);
}

private static string SanitizeForJsonSchemaRef(string typeId)
{
if (string.IsNullOrEmpty(typeId))
return typeId;
return typeId.Replace("[", "%5B").Replace("]", "%5D")
.Replace("<", "%3C").Replace(">", "%3E")
.Replace("+", "%2B");
Comment on lines +12 to +36
@@ -18,7 +33,7 @@ public void GetSchemaTypeId_SimpleArray_ShouldAppendArray()
var result = type.GetSchemaTypeId();

// Assert
Assert.Equal($"System.Int32{TypeUtils.ArraySuffix}", result);
Assert.Equal("System.Int32%5B%5D", result);
@IvanMurzak

Copy link
Copy Markdown
Owner

Use the first PR which you made to add this changes there. Don't need to add new PRs for the same thing

@IvanMurzak IvanMurzak closed this May 4, 2026
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.

3 participants