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

[Bug] "infinity" date fails to deserialize #41

Closed kamyker closed 2 years ago

kamyker commented 2 years ago

Inifinity is insterted when using npgsql https://www.npgsql.org/doc/types/datetime.html , then postgrest fails to desterilize it

System.FormatException : String '-infinity' was not recognized as a valid DateTime.
   at Postgrest.Converters.DateTimeConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Postgrest.Responses.ModeledResponse`1..ctor(BaseResponse baseResponse, JsonSerializerSettings serializerSettings, Boolean shouldParse)
   at Postgrest.Helpers.MakeRequest[T](HttpMethod method, String url, JsonSerializerSettings serializerSettings, Object data, Dictionary`2 headers)
steve-chavez commented 2 years ago

PostgREST is able to use -/+Infinity with no problem:

curl localhost:3000/articleStars?createdAt=gte.+Infinity
[]

curl localhost:3000/articleStars?createdAt=gte.-Infinity
[{"articleId":1,"userId":1,"createdAt":"2015-12-08T04:22:57.472738"},
 {"articleId":1,"userId":2,"createdAt":"2015-12-08T04:22:57.472738"},
 {"articleId":2,"userId":3,"createdAt":"2015-12-08T04:22:57.472738"},
 {"articleId":3,"userId":2,"createdAt":"2015-12-08T04:22:57.472738"},
 {"articleId":1,"userId":3,"createdAt":"2015-12-08T04:22:57.472738"}]

(More special values documented in: https://postgrest.org/en/latest/how-tos/working-with-postgresql-data-types.html)

So I guess this must be an issue with postgrest-csharp itself.

acupofjose commented 2 years ago

@steve-chavez thanks for the reply! Wasn't aware of being able to use -/+Infinity like that - good to know!

@kamyker to clarify, you have a record in the database with Datetime as its type and a value of -infinity and you are attempting to deserialize into a C# model that has DateTime as its type as well?

Since we have to return a DateTime, would it be alright if those were coerced into DateTime.MinValue and DateTime.MaxValue for -/+Infinity respectively?

kamyker commented 2 years ago

@steve-chavez thanks for the reply! Wasn't aware of being able to use -/+Infinity like that - good to know!

@kamyker to clarify, you have a record in the database with Datetime as its type and a value of -infinity and you are attempting to deserialize into a C# model that has DateTime as its type as well?

Yes, exactly.

Since we have to return a DateTime, would it be alright if those were coerced into DateTime.MinValue and DateTime.MaxValue for -/+Infinity respectively?

Yes the same way as npgsql does.