-
-
Notifications
You must be signed in to change notification settings - Fork 64
Add Unicode support for XML doc comment generation #948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
390ee8c
608eb08
46b5bcd
204103b
3414c6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,10 @@ | ||
| --- | ||
| updated_at: 2026-03-02T15:49:46Z | ||
| focus_area: Post-review backlog — bugs, tests, CI fixes | ||
| active_issues: [] | ||
| updated_at: 2026-03-06T16:09:05.259Z | ||
| focus_area: Issue #944 implementation and validation | ||
| active_issues: | ||
| - 944 | ||
| --- | ||
|
|
||
| # What We're Focused On | ||
|
|
||
| Full codebase review completed (2026-03-02). Keaton, Fenster, Hockney, McManus all ran. Findings are in `.squad/decisions.md` and `.squad/identity/backlog.md`. | ||
|
|
||
| Next session: pick from the backlog in priority order. Start with the security fix (Codecov token), then the bug trio (GetInterfaceName, ContractsOutputFolder, ParameterExtractor mutation), then test gaps. | ||
| Implementing and validating the fix for issue #944. The patch is in `src\Refitter.Core\XmlDocumentationGenerator.cs` with regression coverage in `src\Refitter.Tests\XmlDocumentationGeneratorTests.cs` and `src\Refitter.Tests\Examples\GenerateStatusCodeCommentsTests.cs`; full PR-gate validation is now running. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Fenster — Issue #944 Orchestration Log — 2026-03-06T16-29-27Z | ||
|
|
||
| ## Task: Implement production fix for non-ASCII XML documentation | ||
|
|
||
| **Outcome:** ✅ COMPLETE | ||
|
|
||
| Fixed `src/Refitter.Core/XmlDocumentationGenerator.cs` to decode JSON-style escaped Unicode sequences (`\uXXXX`) from NSwag response descriptions before XML-escaping. The `DecodeJsonEscapedText()` method safely handles: | ||
| - `\uXXXX` four-hex-digit Unicode escape sequences | ||
| - `\"` escaped quotes | ||
| - `\\` escaped backslashes | ||
| - `\n`, `\r`, `\t` whitespace escapes | ||
| - Preserves hardcoded XML markup (`<see cref="..."/>`, `<list>`, etc.) | ||
| - XML-escapes `&`, `<`, `>` in user text after decoding | ||
|
|
||
| **Files Modified:** | ||
| - `src/Refitter.Core/XmlDocumentationGenerator.cs` — added `DecodeJsonEscapedText()` helper and applied to `BuildResponseDescription()` method | ||
|
|
||
| **Build Status:** Passed | ||
| **Test Status:** Targeted tests passed | ||
| **Gates:** Build ✅ | Tests ✅ | Format ✅ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Hockney — Issue #944 Orchestration Log — 2026-03-06T16-29-27Z | ||
|
|
||
| ## Task: Add regression coverage for non-ASCII XML documentation fix | ||
|
|
||
| **Outcome:** ✅ COMPLETE | ||
|
|
||
| Added comprehensive regression test coverage at two levels: | ||
|
|
||
| 1. **Unit Tests** — `src/Refitter.Tests/XmlDocumentationGeneratorTests.cs` | ||
| - Direct assertion on `XmlDocumentationGenerator.BuildResponseDescription()` | ||
| - Validates that readable Unicode (Cyrillic example: `Возврат`) is preserved in XML output | ||
| - Verifies that `\uXXXX` escape sequences are decoded, not emitted raw | ||
|
|
||
| 2. **Integration Tests** — `src/Refitter.Tests/Examples/GenerateStatusCodeCommentsTests.cs` | ||
| - End-to-end test: inline OpenAPI spec → full generator pipeline → C# code generation | ||
| - Verifies generated code compiles successfully via `BuildHelper.BuildCSharp()` | ||
| - Protects against regressions in the full compilation workflow | ||
|
|
||
| **Files Modified:** | ||
| - `src/Refitter.Tests/XmlDocumentationGeneratorTests.cs` — new unit tests | ||
| - `src/Refitter.Tests/Examples/GenerateStatusCodeCommentsTests.cs` — new integration tests | ||
|
|
||
| **Test Results:** All targeted tests passed | ||
| **Scope:** Narrow, focused coverage; no production code touched in test project |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Keaton — Issue #944 Orchestration Log — 2026-03-06T16-29-27Z | ||
|
|
||
| ## Task: Review and validate Issue #944 patch | ||
|
|
||
| **Outcome:** ✅ APPROVED | ||
|
|
||
| Conducted thorough review of the non-ASCII XML documentation fix. Validated: | ||
|
|
||
| **Correctness:** | ||
| - Root cause correctly identified: NSwag's `ConversionUtilities.ConvertToStringLiteral()` encodes non-ASCII as `\uXXXX` escape sequences | ||
| - Fix location is precise: only `ExceptionDescription` uses this encoding; other NSwag fields (Summary, Description) are raw strings | ||
| - Decode-then-XML-escape ordering prevents injection attacks on decoded `<`, `>`, `&` characters | ||
| - Bounds checking on `\uXXXX` parsing is correct: `index + 4 < input.Length` | ||
| - Surrogate pairs handled correctly via C# UTF-16 char semantics | ||
| - Fast-path optimization for ASCII-only strings (early return if no backslashes) | ||
|
|
||
| **Testing:** | ||
| - Unit test: readable Cyrillic assertion validates decoding | ||
| - Integration test: compilation verification of generated code | ||
| - All 1415 tests pass; no regressions | ||
|
|
||
| **Gates:** Build ✅ | Tests ✅ (1415/1415) | Format ✅ | ||
|
|
||
| **Files Affected:** | ||
| - `src/Refitter.Core/XmlDocumentationGenerator.cs` | ||
| - `src/Refitter.Tests/XmlDocumentationGeneratorTests.cs` | ||
| - `src/Refitter.Tests/Examples/GenerateStatusCodeCommentsTests.cs` | ||
|
|
||
| **Recommendation:** READY FOR MERGE |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| --- | ||
| name: "tunit-test-filtering" | ||
| description: "Run focused Refitter tests with TUnit treenode filters" | ||
| domain: "testing" | ||
| confidence: "high" | ||
| source: "manual" | ||
| --- | ||
|
|
||
| ## Context | ||
| Refitter test projects use TUnit on Microsoft.Testing.Platform. In this repo, `dotnet test --filter ...` is not supported by the compiled test application, so the fastest reliable way to run a tiny subset of tests is through the generated test executable and a tree-node filter. | ||
|
|
||
| ## Pattern | ||
|
|
||
| ### Use the compiled test executable | ||
| - Test app path: `src\Refitter.Tests\bin\Release\net10.0\Refitter.Tests.exe` | ||
| - Source-generator test discovery means you should build the test project first if you added or renamed tests. | ||
|
|
||
| ### Tree filter syntax | ||
| - Format: `/*/<Namespace>/<Class>/<Test>` | ||
| - Example: | ||
|
|
||
| ```powershell | ||
| & 'src\Refitter.Tests\bin\Release\net10.0\Refitter.Tests.exe' ` | ||
| --treenode-filter '/*/Refitter.Tests.Examples/GenerateStatusCodeCommentsTests/Generated_Code_With_Unicode_Status_Code_Comments_Can_Build' ` | ||
| --disable-logo ` | ||
| --no-progress | ||
| ``` | ||
|
|
||
| ### Verified repo-specific examples | ||
| - `/*/Refitter.Tests/XmlDocumentationGeneratorTests/Can_Generate_Method_Throws_With_Readable_Unicode_Status_Code_Comments` | ||
| - `/*/Refitter.Tests.Examples/GenerateStatusCodeCommentsTests/Generated_Code_Preserves_Readable_Unicode_In_Status_Code_Comments` | ||
| - `/*/Refitter.Tests.Examples/GenerateStatusCodeCommentsTests/Generated_Code_With_Unicode_Status_Code_Comments_Can_Build` | ||
|
|
||
| ## Anti-Patterns | ||
| - Do **not** rely on `dotnet test --filter ...` here; the TUnit/Microsoft.Testing.Platform test app rejects that option. | ||
| - Do **not** assume OR expressions are necessary for a tiny run; separate single-test invocations are often simpler and more reliable. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --- | ||
| name: "xml-doc-description-sanitization" | ||
| description: "Preserve readable Unicode when copying OpenAPI response descriptions into XML doc comments" | ||
| domain: "code-generation" | ||
| confidence: "high" | ||
| source: "manual" | ||
| --- | ||
|
|
||
| ## Context | ||
| `src\Refitter.Core\XmlDocumentationGenerator.cs` writes OpenAPI response descriptions into generated XML documentation comments. Those descriptions may arrive from NSwag in JSON-escaped form (`\uXXXX`, `\"`, `\n`) even though the source OpenAPI file already contains readable Unicode. | ||
|
|
||
| ## Pattern | ||
|
|
||
| ### Sanitize user-sourced response descriptions only | ||
| - Decode JSON-style escape sequences before writing the text into XML docs. | ||
| - After decoding, escape XML-reserved characters (`&`, `<`, `>`) so the generated comments remain valid XML. | ||
| - Apply this only to OpenAPI-sourced response description text such as `method.ResultDescription` and `response.ExceptionDescription`. | ||
|
|
||
| ### Preserve intentional XML doc markup | ||
| - Do **not** run the same sanitization over hardcoded fallback strings that intentionally contain `<see cref="..."/>`, `<list>`, or other XML doc tags. | ||
| - Sanitize at the insertion point where raw API text enters the XML comment output. | ||
|
|
||
| ## Example | ||
|
|
||
| ```csharp | ||
| var description = this.EscapeSymbols(this.DecodeJsonEscapedText(responseDescription)); | ||
| ``` | ||
|
|
||
| ## Anti-Patterns | ||
| - Appending `method.ResultDescription` or `response.ExceptionDescription` directly into XML comments. | ||
| - XML-escaping entire doc fragments that intentionally contain XML doc markup. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| using System.Globalization; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| using System.Text; | ||
| using NSwag; | ||
| using NSwag.CodeGeneration.CSharp.Models; | ||
|
|
@@ -184,7 +185,14 @@ | |
| // Document the result with a fallback description. | ||
| var description = method.ResultDescription; | ||
| if (string.IsNullOrWhiteSpace(description)) | ||
| { | ||
| description = "A <see cref=\"Task\"/> representing the result of the request."; | ||
| } | ||
| else | ||
| { | ||
| description = this.SanitizeResponseDescription(description); | ||
| } | ||
|
|
||
| this.AppendXmlCommentBlock("returns", description, code); | ||
| } | ||
| else | ||
|
|
@@ -316,9 +324,11 @@ | |
|
|
||
| if (!string.IsNullOrWhiteSpace(response.ExceptionDescription)) | ||
| { | ||
| var responseDescription = this.SanitizeResponseDescription(response.ExceptionDescription); | ||
|
|
||
| description | ||
| .Append("<description>") | ||
| .Append(response.ExceptionDescription) | ||
| .Append(responseDescription) | ||
| .AppendLine("</description>"); | ||
| } | ||
|
|
||
|
|
@@ -338,4 +348,75 @@ | |
| .Replace("<", "<") | ||
| .Replace(">", ">"); | ||
| } | ||
|
|
||
| private string SanitizeResponseDescription(string input) | ||
| { | ||
| return this.EscapeSymbols(this.DecodeJsonEscapedText(input)); | ||
| } | ||
|
|
||
| private string DecodeJsonEscapedText(string input) | ||
|
Check warning on line 357 in src/Refitter.Core/XmlDocumentationGenerator.cs
|
||
| { | ||
| if (string.IsNullOrEmpty(input) || input.IndexOf('\\') < 0) | ||
| { | ||
| return input; | ||
| } | ||
|
|
||
| var result = new StringBuilder(input.Length); | ||
|
|
||
| for (var index = 0; index < input.Length; index++) | ||
| { | ||
| var current = input[index]; | ||
| if (current != '\\' || index == input.Length - 1) | ||
| { | ||
| result.Append(current); | ||
| continue; | ||
| } | ||
|
|
||
| var escapedCharacter = input[++index]; | ||
|
Check warning on line 375 in src/Refitter.Core/XmlDocumentationGenerator.cs
|
||
|
|
||
| if (escapedCharacter == 'u' && index + 4 < input.Length) | ||
| { | ||
| var hexValue = input.Substring(index + 1, 4); | ||
| if (int.TryParse(hexValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var unicodeCodePoint)) | ||
| { | ||
| result.Append((char)unicodeCodePoint); | ||
| index += 4; | ||
|
Check warning on line 383 in src/Refitter.Core/XmlDocumentationGenerator.cs
|
||
| continue; | ||
| } | ||
| } | ||
|
|
||
| switch (escapedCharacter) | ||
| { | ||
| case '"': | ||
| result.Append('"'); | ||
| break; | ||
| case '\\': | ||
| result.Append('\\'); | ||
| break; | ||
| case '/': | ||
| result.Append('/'); | ||
| break; | ||
| case 'b': | ||
| result.Append('\b'); | ||
| break; | ||
| case 'f': | ||
| result.Append('\f'); | ||
| break; | ||
| case 'n': | ||
| result.Append('\n'); | ||
| break; | ||
| case 'r': | ||
| result.Append('\r'); | ||
| break; | ||
| case 't': | ||
| result.Append('\t'); | ||
| break; | ||
|
||
| default: | ||
| result.Append('\\').Append(escapedCharacter); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return result.ToString(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.