Skip to content

Fix @Generable description literal handling in macro expansion - #61

Merged
christopherkarani merged 2 commits into
mainfrom
codex/check-frameworks-for-issues-20260516
May 15, 2026
Merged

christopherkarani merged 2 commits into
mainfrom
codex/check-frameworks-for-issues-20260516

Conversation

@christopherkarani

Copy link
Copy Markdown
Owner

Summary

Fixes a macro-expansion correctness bug in GenerableMacro for type/enum @Generable(description: ...) strings.

Root cause

  • Description parsing in GenerableMacro used quote trimming on literal syntax text.
  • Schema description content was emitted into generated Swift without robust escaping.
  • Certain quoted/newline descriptions could break expansion (unterminated string literal) or lose intended content.

Changes

  • Switched description extraction to segment-based string literal parsing via stringLiteralValue(from:).
  • Added escapeSwiftStringLiteralContent(_:) and used it when emitting schema descriptions.
  • Applied this to struct and enum generationSchema generation paths.
  • Added regression tests in Tests/ConduitMacrosTests/GenerableMacroTests.swift for:
    • intentional leading/trailing quoted descriptions
    • escaped quotes/backslashes/newlines in type descriptions

Verification

  • swift test --filter GenerableMacroTests
  • swift test
  • swift build

All pass on this branch.

Problem\n- @generable type/enum descriptions were parsed via quote trimming and injected into generated code without robust literal escaping.\n- Descriptions containing escaped quotes/newlines could produce malformed expanded code or lossy schema descriptions.\n\nTDD\n- Added regression coverage in ConduitMacrosTests for:\n  - intentional leading/trailing quoted descriptions\n  - escaped quotes, backslashes, and newlines in type-level @generable descriptions\n- Verified the new tests fail before fix and pass after fix.\n\nFix\n- Switched description extraction in GenerableMacro to segment-based string literal parsing (matching GuideMacro behavior) instead of trimming quote characters.\n- Added centralized Swift string literal escaping for schema description emission.\n- Applied escaping to both struct and enum generationSchema emitters.\n\nVerification\n- swift test --filter GenerableMacroTests\n- swift test\n- swift build

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

馃挕 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 51e6c36c80

鈩癸笍 About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 馃憤.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +171 to +173
return literal.segments.compactMap { segment in
segment.as(StringSegmentSyntax.self)?.content.text
}.joined()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve interpolation segments in string literal parsing

stringLiteralValue(from:) now joins only StringSegmentSyntax tokens and silently drops ExpressionSegmentSyntax, so any interpolated description (for example @Generable(description: "Build \(1)") or @Guide(description: "v\(major)")) loses the interpolated content in generated schema output. This is a regression from the previous behavior, which preserved the full literal syntax text; after this change, descriptions can be truncated or materially incorrect whenever interpolation appears.

Useful? React with 馃憤聽/ 馃憥.

- Make generated PartiallyGenerated initializer non-throwing\n- Update generated asPartiallyGenerated() to avoid try!\n- Preserve ConvertibleFromGeneratedContent conformance while removing crash-prone forced try in generated code\n\nVerification:\n- swift test --filter GenerableMacroTests\n- swift test\n- swift build
@christopherkarani

Copy link
Copy Markdown
Owner Author

Added a follow-up fix for the remaining forced-try path in macro-generated code.

What changed:

  • asPartiallyGenerated() generated code no longer uses try!.
  • Generated PartiallyGenerated initializer is now non-throwing, which remains protocol-compatible and removes this crash vector.

Re-verified:

  • swift test --filter GenerableMacroTests
  • swift test
  • swift build

All green.

@christopherkarani
christopherkarani merged commit 1fe120a into main May 15, 2026
0 of 4 checks passed
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.

1 participant