Fix/nested class ref sanitization#78
Conversation
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>
|
Thanks for catching the nested-class case. I opened this follow-up PR to encode Let me know if this direction looks good or if you prefer another encoding approach. |
There was a problem hiding this comment.
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. |
| 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"); |
| @@ -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); | |||
…a tests instead of hardcoding
…n' into fix/nested-class-ref-sanitization
|
Use the first PR which you made to add this changes there. Don't need to add new PRs for the same thing |
No description provided.