Skip to content

fix: handle newline characters in specLiteralStringCode#89

Merged
t-unit merged 4 commits intomainfrom
88-fix-newline-in-spec-literal-string
Apr 4, 2026
Merged

fix: handle newline characters in specLiteralStringCode#89
t-unit merged 4 commits intomainfrom
88-fix-newline-in-spec-literal-string

Conversation

@t-unit
Copy link
Copy Markdown
Owner

@t-unit t-unit commented Apr 4, 2026

Summary

  • specLiteralStringCode() now skips single-line raw strings (r'...', r"...") when the value contains \n or \r, using triple-quoted raw strings or the escaped fallback path instead
  • escapeForSingleQuotedDartString() now escapes \n and \r so the fallback path produces valid Dart
  • Added 9 new tests covering \n, \r, \r\n, and combinations with quotes

Test plan

  • All 1,899 tonik_generate unit tests pass (including 9 new newline tests)
  • All 3,727 unit tests pass across all packages
  • All 3,450 integration tests pass across 27 suites
  • fvm dart analyze clean

Closes #88

🤖 Generated with Claude Code

Single-line raw strings (r'...' and r"...") cannot contain literal
newlines. When a spec value contains \n or \r, the function now skips
single-line raw forms and uses triple-quoted raw strings or the escaped
fallback path (which now also escapes \n and \r).

Closes #88

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.76%. Comparing base (3563c8e) to head (227d8ef).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main      #89   +/-   ##
=======================================
  Coverage   89.75%   89.76%           
=======================================
  Files         157      157           
  Lines       14479    14483    +4     
=======================================
+ Hits        12996    13000    +4     
  Misses       1483     1483           
Files with missing lines Coverage Δ
...nik_generate/lib/src/util/spec_literal_string.dart 100.00% <100.00%> (ø)
Files with missing lines Coverage Δ
...nik_generate/lib/src/util/spec_literal_string.dart 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Update the specLiteralString docstring to reflect the newline handling
added in the previous commit. Add standalone unit tests for
escapeForSingleQuotedDartString covering \n, \r, and combined escaping.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@t-unit t-unit marked this pull request as ready for review April 4, 2026 09:02
@t-unit t-unit requested a review from Copilot April 4, 2026 09:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Dart code generation for OpenAPI spec string values containing newline characters by preventing invalid single-line raw string literals and ensuring the escaped fallback path produces syntactically valid Dart.

Changes:

  • Updated specLiteralStringCode() to avoid single-line raw strings when the input contains \n or \r, preferring raw triple-quoted strings when viable.
  • Updated escapeForSingleQuotedDartString() to escape \n and \r so fallback (non-raw) literals remain valid.
  • Added unit tests covering \n, \r, \r\n, and newline interactions with quotes / """ edge cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/tonik_generate/lib/src/util/spec_literal_string.dart Adjusts quoting/escaping strategy to handle newline characters safely in generated Dart string literals.
packages/tonik_generate/test/src/util/spec_literal_string_test.dart Adds focused tests validating the new newline handling and escaping behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

t-unit added 2 commits April 4, 2026 11:29
Use triple-quoted Dart strings instead of escaped \n to better represent
how newline values arrive from YAML multiline blocks in OpenAPI specs.
Literal carriage-return bytes in raw triple-quoted strings cause issues
with formatters, diffs, and editors. Values containing \r now always use
the escaped fallback path where \r becomes the literal escape sequence.
@t-unit t-unit merged commit 85bd92f into main Apr 4, 2026
11 checks passed
@t-unit t-unit deleted the 88-fix-newline-in-spec-literal-string branch April 4, 2026 10:33
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.

specLiteralStringCode doesn't handle newline characters

2 participants