Skip to content

parity(realtime): parse Postgres array literals instead of splitting on commas [from supabase-flutter] #393

Description

@spydon

SDK Parity: C# implementation needed

A change was made in supabase-flutter that needs to be implemented in this repository for SDK parity.

Reference Implementation (supabase-flutter)

What Changed

toArray in packages/supabase_realtime/lib/src/transformers.dart used to try json.decode on a Postgres array literal body and, when that threw, fall back to splitting the string on every comma. That silently corrupted realtime payloads for common cases:

Literal Postgres sends Before After
{"a,b",c} ['"a', ' b"', 'c'] (three elements) ['a,b', 'c']
{NULL,a} on text[] ['NULL', 'a'] [null, 'a']
{"",a} ['""', 'a'] ['', 'a']
{{1,2},{3,4}} on int4[] [null, null, null, null] [[1, 2], [3, 4]]

The fix replaced the comma-split with an actual parser for the literal: comma separated elements, optional double quotes where \ escapes the next character, unquoted whitespace trimmed, an unquoted NULL as the null element (a quoted "NULL" stays the four character string), and nested arrays keeping their shape.

Code Reference

Confirmed in supabase-csharp: packages/Realtime/Realtime/Converters/StringArrayConverter.cs and IntArrayConverter.cs Parse() do:

foreach (var item in value.Trim('{', '}', '[', ']').Split(','))
{
    if (string.IsNullOrEmpty(item)) continue;
    result.Add(item);
}

This has the same class of bug as the pre-fix Dart code, but without even the quoted-string handling Dart had before this fix: a comma inside a quoted string element splits it into extra elements, a literal NULL string is indistinguishable from a real null element, and there's no escape (\") handling at all.

Implementation Guidance

Key Behaviors to Match

  • Quoted commas inside an array element don't split the element ({"a,b",c}["a,b", "c"])
  • \" and \\ escapes inside quoted elements are honored
  • Whitespace around unquoted elements is trimmed
  • Nested arrays (for multi-dimensional columns) keep their shape rather than being flattened/dropped

Files Likely Affected

  • packages/Realtime/Realtime/Converters/StringArrayConverter.cs
  • packages/Realtime/Realtime/Converters/IntArrayConverter.cs
  • Any other array converter under packages/Realtime/Realtime/Converters/ sharing this Parse() shape

Acceptance Criteria

  • Quoted commas inside an array element don't split the element
  • Escaped quotes/backslashes inside a quoted element are unescaped correctly
  • Nested array literals keep their shape
  • Existing plain-array behavior ({1,2,3}, {}) is unchanged
  • Unit tests cover the cases above

Context

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions