Fix invalid JSON deserialization in v0.6.0#34
Conversation
| {{"); | ||
| foreach (var field in Fields) | ||
| sb.AppendLine($@" {field.PropertyName} = {GetParameterNameForConstructor(field)};"); | ||
| sb.AppendLine($@" this.{field.PropertyName} = {GetParameterNameForConstructor(field)};"); |
There was a problem hiding this comment.
field.PropertyName and GetParameterNameForConstructor(field) are mostly the same.
So I added this in front of property name.
| sb.AppendLine($@" {field.PropertyName} = {GetParameterNameForConstructor(field)};"); | ||
| sb.AppendLine($@" this.{field.PropertyName} = {GetParameterNameForConstructor(field)};"); | ||
| sb.AppendLine($@" | ||
| AddInternal(this); |
There was a problem hiding this comment.
AddInternal must be called at the end of JSON constructor because AddInternal isn't updating the reference of a item in the cache, but updating the each values of the item.
| public ItemBase(string? id, DateTime? created, DateTime? updated) | ||
| { | ||
| Id = id; | ||
| _Id = id; |
There was a problem hiding this comment.
Use _Id rather than Id to avoid AddInternal being called at this point.
AddInternal must be called at the end of JSON constructor.
| _Id = id; | ||
| Created = created; | ||
| Updated = updated; | ||
| Metadata_.SetLoaded(); |
There was a problem hiding this comment.
This line is also required to avoid redundant update of the item.
| } | ||
|
|
||
| //protected object? AddInternal(object? element) => Collection.AddInternal(element); | ||
| protected object? AddInternal(object? element) => Collection.AddInternal(element); |
There was a problem hiding this comment.
Collection.AddInternal is internal method. Exposing it for derived classes.
|
Thanks a lot.
Again thanks! |
This PR includes a fix of two issues.
The first issue
The first issue is a invalid JSON deserialization of
PocketBaseExtensions.Send<T>.In previous version, PocketBaseClient was using pocketbase-csharp-sdk's SendAsync method. But in v0.6.0, it seems like you have migrated from it to PocketBaseExtensions.
PocketBaseExtensions.Send uses System.Text.Json's JSON deserializer, which is case sensitive by default. But pocketbase-csharp-sdk is using HttpClient's method in order to deserialize, so that JSON deserializer uses case-insensitive option.
What we need to add is JSON serialization options of case-insensitive to JSON deserializer.
I used
JsonSerializerDefaults.Webrather than creating our own serializer options. Because pocketbase-csharp-sdk is based on this options according to this article.The second issue
I found that my previous PR about invalid json deserialization wasn't fully fixed.
The main cause of it is
AddInternalis being called before all fields of a item being set.I'll explain in comments line by line. So please reference to that.