supabase-community / postgrest-csharp

A C# Client library for Postgrest
https://supabase-community.github.io/postgrest-csharp/api/Postgrest.html
MIT License
114 stars 22 forks source link

Range type Exception when Inserting #1

Closed elrhomariyounes closed 3 years ago

elrhomariyounes commented 3 years ago

Bug report

Describe the bug

Hello, while trying to insert a new record in the database using the project PostgrestExample in the current repo an Exception is thrown from Newtonsoft.Json.JsonReaderException.

After Debugging the Exception is thrown in PrepareRequestData Method in the Builder class. I think the reason why is because the User table contains an AgeRange property of type Sytem.Range So when calling the JsonConvert.Deserialize to get a dictionary of string key and a string value is not possible because the System.Range contains multiple properties as Start and End that are also objects of type Index so the when deserializing the JsonReader will find curly brackets and this cannot be deserialized to dictionnary<string, string>

In the Reproduce step with the current values the json object will be in this shape :

{
  "Username":"younes",
  "Data":null,
  "AgeRange":
                      {
                         "Start":
                                      {
                                          "Value":1,
                                          "IsFromEnd":false
                                       },
                          "End":
                                       {
                                          "Value":3,
                                          "IsFromEnd":false
                                        }
                        },
"Catchphrase":"Hii",
"Status":null,
"InsertedAt":"0001-01-01T00:00:00",
"UpdatedAt":"0001-01-01T00:00:00"
}

This json will throw an exception when trying to the call

public Dictionary<string, string> PrepareRequestData(object data) => JsonConvert.DeserializeObject<Dictionary<string,string>>(JsonConvert.SerializeObject(data));

To Reproduce

  1. Initiliaze a new User
    var supaYounes = new User
    {
      Username = "younes",
      Catchphrase = "Hii",
      AgeRange = new Range(1, 3)
    };
  2. Insert
    var response = await client.Builder<User>().Insert(supaYounes);

Expected behavior

User Inserted successfully in the Database

System information

acupofjose commented 3 years ago

@elrhomariyounes thanks for such a detailed report! Could you see if the latest commit works for you now?

elrhomariyounes commented 3 years ago

@acupofjose Thank you working perfectly