supabase-community / supabase-csharp

A C# Client library for Supabase
https://github.com/supabase-community/supabase-csharp/wiki
MIT License
499 stars 50 forks source link

ArgumentException raised when updating nullable JObject with null #171

Open justdmitry opened 2 months ago

justdmitry commented 2 months ago

Bug report

Updating (via IPostgrestTable.Set) nullable JObject field with null value fails with ArgumentException (Expected Value to be of Type: JObject, instead received: .)

Inserts are Ok, only updates fail.

Steps to reproduce

Table structure (non-relevant fields skipped):

public.analytics (
  id bigint generated by default as identity not null,
  properties jsonb null
)

Model (non-relevant fields skipped):

[Table("analytics")]
public class Analytics : BaseModel
{
    [PrimaryKey("id")]
    public long Id { get; set; }

    [Column("properties")]
    public Newtonsoft.Json.Linq.JObject? Properties { get; set; }
}

Program code:

var client = new Supabase.Client(...);
JObject? properties = null;

var existing = await client.From<Analytics>()
    .Filter(x => /* some filtering here */)
    .Limit(1)
    .Single();
if (existing != null)
{
    await client.From<Analytics>()
        .Where(x => x.Id == existing.Id)
        .Set(x => x.Properties, properties)
        .Update();
}
else
{
    var item = new Analytics  { Properties = properties };
    await client.From<Analytics>().Insert(item);
}

Bottom code part (that handles Insert logic) works, but middle one (Update) does not: System.ArgumentException gets raised with Expected Value to be of Type: JObject, instead received: . message.

Expected behavior

Both code parts should work Ok.

System information

justdmitry commented 2 months ago

Workaround: Replacing null value with empty object ({}) is a workaround, but empty object is not the same as NULL value...