surrealdb / surrealdb.net

SurrealDB SDK for .NET
https://surrealdb.com
Apache License 2.0
94 stars 20 forks source link

Feature: Implement ReadOnlyRecordIdJsonConverter Read #133

Closed Isaac-Guillen closed 2 weeks ago

Isaac-Guillen commented 3 weeks ago

Is your feature request related to a problem?

I have a blazor app that receives models from the backend, but now I get an error saying ReadOnlyRecordIdJsonConverter.Read is not implemented, I don't know what to do without having a great impact in my project

Describe the solution

I think this will be resolved once the read method is implemented

Alternative methods

I don't know if there is a way to override the method or I have to do anything else

SurrealDB version

2

Package version(s)

El proyecto "Demo.Server" tiene las referencias de paquete siguientes [net8.0]: Paquete de nivel superior Solicitado Resuelto

Microsoft.AspNetCore.Authentication.JwtBearer 8.0.10 8.0.10 Microsoft.AspNetCore.Components.WebAssembly.Server 8.0.10 8.0.10 Serilog.AspNetCore 8.0.3 8.0.3 Serilog.Sinks.File 6.0.0 6.0.0 Swashbuckle.AspNetCore 6.8.1 6.8.1 System.IdentityModel.Tokens.Jwt 8.1.2 8.1.2

El proyecto "Demo.Shared" tiene las referencias de paquete siguientes [net8.0]: Paquete de nivel superior Solicitado Resuelto

Microsoft.AspNetCore.Authorization 8.0.10 8.0.10 QuestPDF 2024.10.1 2024.10.1 SkiaSharp.NativeAssets.Linux 2.88.8 2.88.8 SurrealDb.Net 0.6.0 0.6.0

El proyecto "Demo.Client" tiene las referencias de paquete siguientes [net8.0]: Paquete de nivel superior Solicitado Resuelto

Blazored.SessionStorage 2.4.0 2.4.0 Microsoft.AspNetCore.Components.Authorization 8.0.10 8.0.10 Microsoft.AspNetCore.Components.WebAssembly 8.0.10 8.0.10 Microsoft.AspNetCore.Components.WebAssembly.DevServer 8.0.10 8.0.10 Microsoft.NET.ILLink.Tasks (A) [8.0.10, ) 8.0.10 Microsoft.NET.Sdk.WebAssembly.Pack (A) [8.0.10, ) 8.0.10 Radzen.Blazor 5.2.12 5.2.12

Contact Details

isaac.guillen@proton.me

Is there an existing issue for this?

Code of Conduct

Odonno commented 3 weeks ago

Hello @Isaac-Guillen

This error happens because you are using the Record id in your ASP.NET web App/API and as such the Record id is serialized to/deserialized from json.

Serializing a Record id is pretty easy in general because we only have to extract the id part of a RecordId. However, deserializing is another story.

The default JSON converter used is ReadOnlyRecordIdJsonConverter. See: https://github.com/surrealdb/surrealdb.net/blob/abb7c4de3c504a6e99584f8c1dc5d8aff2091aba/SurrealDb.Net/Models/Record.cs#L15 This converter like its name implies only read and serializes the id from the RecordId POCO and outputs it to JSON. But it is not able to read/deserialize from JSON and deduce how to create a RecordId from the JSON input.

From that, you have two solutions:

  1. Either create a DTO that will map properties from/to a Record inherited class
  2. or use the RecordIdJsonConverterAttribute https://github.com/surrealdb/surrealdb.net/blob/abb7c4de3c504a6e99584f8c1dc5d8aff2091aba/SurrealDb.Net/Json/RecordIdJsonConverterAttribute.cs#L6

The first solution is pretty easy but I can acknowledge it needs more work. For the second solution, you simply need to override the Id property to use the RecordIdJsonConverterAttribute. See example below:

public class MyRecord : Record
{
  [RecordIdJsonConverter("my-table")]
  [CborProperty("id")]
  [CborIgnoreIfDefault]
  public new RecordId? Id { get; set; }
}

The table argument of the attribute is required to determine what table the RecordId belong to.

Feel free to ask any question on this matter.

Isaac-Guillen commented 2 weeks ago

Thank you for the response and solutions proposed, I think that in long term would be better to implement DTO Models and mappers, but in the short term, the override solution has any downside?

Thanks!

Odonno commented 2 weeks ago

Thank you for the response and solutions proposed, I think that in long term would be better to implement DTO Models and mappers, but in the short term, the override solution has any downside?

I do not know if there is any.

Isaac-Guillen commented 2 weeks ago

Thanks a lot for your help!