heroiclabs / nakama-dotnet

.NET client for Nakama server written in C#.
https://heroiclabs.com/docs/nakama/client-libraries/unity/
Apache License 2.0
121 stars 45 forks source link

WriteStorageObjectsAsync duplicates data in request #150

Closed SayAndrei closed 1 year ago

SayAndrei commented 1 year ago

Hey!

WriteStorageObjectsAsync makes request with JSON payload, that contains Objects and objects fields with the same content due to incorrect ApiWriteStorageObjectsRequest class definition:

internal class ApiWriteStorageObjectsRequest : IApiWriteStorageObjectsRequest
    {
        /// <inheritdoc />
        public IEnumerable<IApiWriteStorageObject> Objects => _objects ?? new List<ApiWriteStorageObject>(0);
        [DataMember(Name="objects"), Preserve]
        public List<ApiWriteStorageObject> _objects { get; set; }

        public override string ToString()
        {
            var output = "";
            output = string.Concat(output, "Objects: [", string.Join(", ", Objects), "], ");
            return output;
        }
    }

Actual payload looks like:

{
  "Objects": [
    {
      "collection": "GameSaves",
      "key": "e885c548-19a5-461f-a7d7-7ca806e9eaae",
      "permission_read": 1,
      "permission_write": 1,
      "value": "{...}"
    }
  ],
  "objects": [
    {
      "collection": "GameSaves",
      "key": "e885c548-19a5-461f-a7d7-7ca806e9eaae",
      "permission_read": 1,
      "permission_write": 1,
      "value": "{...}"
    }
  ]
}

It causes useless data transfer and parsing for client and server.

novabyte commented 1 year ago

This is a great catch @SayAndrei we'll update our code generator tool for the .NET types to use [IgnoreDataMember] to avoid the double encode which is done by the parser. Thanks 🙏

lugehorsam commented 1 year ago

We released a new client that should resolve this issue.