infobip / infobip-api-csharp-client

Infobip API client library in C#, distributed as a NuGet package.
https://www.infobip.com/docs/api
MIT License
11 stars 17 forks source link

Add support for System.Text.Json #31

Open BluMichele opened 7 months ago

BluMichele commented 7 months ago

Since ASP.NET Core 3.0 the default serializer is System.Text.Json which means that on most of the modern services an API such as this would not work:

[HttpPost("api/sms/delivery-reports")]
    public IActionResult ReceiveDeliveryReport([FromBody] SmsDeliveryResult deliveryResult)
    {
        foreach (var result in deliveryResult.Results)
        {
            System.Diagnostics.Debug.WriteLine($"{result.MessageId} - {result.Status.Name}");
        }
        return Ok();
    }

(from your example of using webhooks)

The issue is that the models are decorated only with WCF and Newtonsoft's attributes like [DataMember(Name = "results", EmitDefaultValue = false)] or [JsonConstructor]. When a user uses one of your models in their controllers ASP.NET will try to deserialize the JSON using System.Text.Json.JsonSerializer.Deserialize but will fail because the capitalization of properties is not the same as expected. To solve this you should add attributes such as [JsonPropertyName] where you use DataMember

I hope to have been useful :)