Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mtaku3
Copy link
Collaborator

@mtaku3 mtaku3 commented Feb 7, 2023

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.Web rather 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 AddInternal is being called before all fields of a item being set.
I'll explain in comments line by line. So please reference to that.

{{");
foreach (var field in Fields)
sb.AppendLine($@" {field.PropertyName} = {GetParameterNameForConstructor(field)};");
sb.AppendLine($@" this.{field.PropertyName} = {GetParameterNameForConstructor(field)};");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collection.AddInternal is internal method. Exposing it for derived classes.

@mtaku3 mtaku3 marked this pull request as ready for review February 7, 2023 06:06
@mtaku3 mtaku3 requested a review from iluvadev February 7, 2023 06:06
@iluvadev iluvadev merged commit 3180234 into iluvadev:main Feb 7, 2023
@iluvadev
Copy link
Owner

iluvadev commented Feb 7, 2023

Thanks a lot.
I see, you are right, sorry I think I rushed with the last changes I made.
About the first issue: is caused by the need of a double implementation for async and sync methods in order to avoid async to sync conversions:

  • PocketBaseClient uses pocketbase-csharp-sdk, but only has async methods
  • Sync methods are an adapted copy from pocketbase-csharp-sdk async methods... But with some error🤦
    [I will create a PR to pocketbase-csharp-sdk with PocketBase sync calls]

Again thanks!

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants