Escape control characters in generated C# string literals [patch] - #226
Merged
Merged
Conversation
CSharpCodeGenerator.Quote escaped only backslash and double quote, so a String default, unit or editor hint containing a newline produced a raw line break inside the literal (CS1010), and a tab or NUL landed in the source as an invisible raw character. Escape \n \r \t \0, anything else below U+0020 as \uXXXX, and the line separators U+0085, U+2028 and U+2029. Fixes #217 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011PMSp3sjbza8Td6fxS2Kjr
…-characters # Conflicts: # Schema.Test/CodeGenerationRoundTripTests.cs
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #217
What was wrong
CSharpCodeGenerator.Quoteescaped only\and". It builds theStringdefault initialiser, theSchemaDefault(...)argument, and theSchemaUnit/SchemaEditorHintarguments. A value containing a newline therefore produced a raw line break inside a regular literal. Generation reported success, but the output failed to compile with CS1010: Newline in constant. A tab or NUL did compile, but ended up in the source as an invisible raw character.Change
Quotenow escapes, character by character:\and", as before\n,\r,\tand\0with their short escapes\uXXXX\uXXXX. C# treats these as line terminators, so they would also break a literalThis is the set the C++
Escapealready handles, plus the Unicode line separators. I didn't add a Roslyn dependency to the library forSymbolDisplay.FormatLiteral.Tests
CodeGenerationRoundTripTests.TestAStringDefaultWithControlCharactersCompilesAndKeepsItsValuegenerates a class whoseStringdefault contains\n,\r\n,\t,\0, U+0001, U+2028, a quote and a backslash. It then:SchemaDefaultholds exactly that stringWith the
CSharpCodeGenerator.cschange reverted, the test fails withNote.g.cs(11,37): error CS1010: Newline in constant, on both the attribute line and the initialiser line. With the change, the fullSchema.Testsuite passes on net10.0 (503/503). I couldn't run the net8.0/net9.0 legs because those runtimes aren't installed in this environment.🤖 Generated with Claude Code
https://claude.ai/code/session_011PMSp3sjbza8Td6fxS2Kjr
Generated by Claude Code