Skip to content

Escape quoted default values so the generated literal compiles - #113

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/escape-quoted-default-values
Sep 26, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/escape-quoted-default-values

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #111

What was wrong

TemplateBase.WriteDefaultValueTo wrapped DefaultValue in "…" without escaping it when DefaultValueIsQuoted was set. A backslash, quote or newline in the value then produced C# that doesn't compile, or that silently changes the value (\t becoming a tab). This affects the Field, Parameter, Property and EnumMember templates.

Change

  • A new TemplateRendering.QuoteStringLiteral writes the value as a regular C# string literal:
    • \\, \", \0, \a, \b, \f, \n, \r, \t and \v get their short escapes.
    • Any other control character, plus U+2028/U+2029 (which the compiler treats as line breaks), gets a \uXXXX escape.
  • WriteDefaultValueTo uses the new method, and the DefaultValueIsQuoted docs now say the value is escaped as well as quoted.
  • There is no new dependency, so the change stays netstandard2.0-friendly and avoids Roslyn's SymbolDisplay.FormatLiteral.

Tests

  • ParameterEscapesAQuotedDefaultValue: data rows for a backslash, a quote, newlines, tab and null, and another control character plus a line separator.
  • FieldEscapesAQuotedInitialiser: the exact reproduction from the issue.
  • With the fix reverted, all 6 new cases fail. With it applied, the full suite passes (177/177).
  • A local Sonar build (.sonarlint/sonar-local.props) reports only the pre-existing S8969 in DocComment.cs.

🤖 Generated with Claude Code

https://claude.ai/code/session_019StKk4VdpfAZ5cdW5X685e


Generated by Claude Code

DefaultValueIsQuoted wrapped DefaultValue in quotes without escaping it,
so a backslash, quote or newline in the value produced C# that does not
compile, or that silently changes the string (\t becoming a tab). The
value is now written as an escaped regular string literal. This covers
the Field, Parameter, Property and EnumMember templates.

Fixes #111

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019StKk4VdpfAZ5cdW5X685e
matt-edmondson pushed a commit that referenced this pull request Sep 26, 2026
Groups the two body writers together, and keeps this change from
touching the same lines as the string-literal escaping in #113.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019StKk4VdpfAZ5cdW5X685e
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quoted default values aren't escaped: a " or \ in DefaultValue generates C# that doesn't compile

2 participants