Skip to content

fix(postgrest-typegen): escape database values in Swift string literals - #123

Merged
grdsdev merged 1 commit into
mainfrom
fix/typegen-swift-fixes
Aug 31, 2026
Merged

fix(postgrest-typegen): escape database values in Swift string literals#123
grdsdev merged 1 commit into
mainfrom
fix/typegen-swift-fixes

Conversation

@spydon

@spydon spydon commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Ports the Swift string literal escaping fix from postgres-meta's open template PRs into this package (the templates are being deleted in favor of this package in supabase/postgres-meta#1084, so open fixes there are triaged and re-landed here).

Database-provided names were interpolated raw into Swift string literals, so a double quote, backslash (including interpolation sequences like \(...)), or line break in an enum label or column name produced Swift that does not compile (supabase/postgres-meta#1126). A swiftStringLiteral helper now escapes per Swift's string literal grammar (backslash, quote, tab, newline, carriage return, remaining C0 controls and DEL as \u{n}, plus U+2028/U+2029) and is applied in generateEnum, the single rendering point for both vulnerable positions: Postgres enum raw values and CodingKeys raw values.

Triage of origin PRs

postgres-meta PR Verdict Reasoning
#1128 Ported The stronger duplicate: character-loop escaping covering controls and U+2028/U+2029, returns the complete quoted literal, and the author validated output with swiftc -parse.
#1132 Skipped Duplicate; regex-based, misses U+2028/U+2029, and returns only inner text so call sites keep hand-placed quotes. The regex would also trip oxlint's control-character rule here.

Identifier positions (enum case names, property names) are already safe through the existing formatForSwiftTypeName/formatForSwiftPropertyName sanitization, so escaping is only needed at the literal seam. Pre-existing identifier gaps (all-punctuation names yielding empty case names, leading digits) exist upstream too and need a separate sanitization/dedup design; deliberately out of scope.

Validation

  • Snapshot test with pathological labels (quote, backslash, \(now), newline, tab, CR, BEL, U+2028) across enum raw values and CodingKeys in Select/Insert/Update, plus a pin that ordinary output stays byte-identical.
  • Generated pathological output passes swiftc -parse.
  • check-types, format-and-lint, knip, build, test (95 pass, parity 4/4) all green; parity golden unchanged.

The Swift generator interpolated database-provided enum labels and column
names directly into string literals for enum raw values and CodingKeys. A
double quote terminated the literal early, a backslash could start an
escape or interpolation sequence, and line breaks or control characters
are invalid in single-line literals, so such names produced Swift that
failed to compile. Escape those characters per Swift's string literal
grammar; Swift unescapes at compile time, so raw values round-trip
unchanged. Identifier positions were already safe because name formatting
strips non-alphanumeric characters.

Ported from supabase/postgres-meta#1128 and supabase/postgres-meta#1132.
@spydon
spydon requested a review from a team as a code owner August 31, 2026 13:13
@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c465c33d-57e5-4f3a-a607-4b3cb23a7451


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@grdsdev
grdsdev merged commit 4a5cef5 into main Aug 31, 2026
5 checks passed
@grdsdev
grdsdev deleted the fix/typegen-swift-fixes branch August 31, 2026 14:29
@supabase-releaser supabase-releaser Bot mentioned this pull request Aug 31, 2026
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.

3 participants