Skip to content

Commit ea2fe7e

Browse files
Bugfix for Enum deserialization for System.Text.Json.
1 parent 99b59f2 commit ea2fe7e

File tree

82 files changed

+151
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+151
-167
lines changed

ApiClient.Tests/Api/CallRoutingApiTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ public void ShouldUrlDestinationWebhook()
627627
var expectedSipTrunkId = "60d345fd3a799ec";
628628
var expectedValueType = "SIP";
629629
var expectedConnectTimeout = 30;
630-
var expectedRecordingType = CallRoutingRecordingType.Audio;
630+
var expectedRecordingType = "AUDIO";
631631
var expectedType = "ENDPOINT";
632632

633633
var expectedResponse = $@"
@@ -688,7 +688,7 @@ void AssertCallRoutingEndpointDestinationResponse(
688688
Assert.AreEqual(expectedSipTrunkId, callRoutingSipEndpoint.SipTrunkId);
689689

690690
Assert.AreEqual(expectedConnectTimeout, callRoutingEndpointDestinationResponse.ConnectTimeout);
691-
Assert.AreEqual(expectedRecordingType, callRoutingEndpointDestinationResponse.Recording.RecordingType);
691+
Assert.AreEqual(expectedRecordingType, GetEnumAttributeValue(callRoutingEndpointDestinationResponse.Recording.RecordingType));
692692
}
693693

694694
void AssertCallRoutingUrlDestinationHttpRequest(

ApiClient.Tests/Api/NumberMaskingApiTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public void ShouldNumberMaskingStatusApi()
409409
var givenCurrency = "7e8d64c0-6c72-4922-aa96-48728753c660";
410410
var givenRecordingFileId = "EUR";
411411
var givenRecordingCalleeAnnouncement = true;
412-
var givenRecordingStatus = NumberMaskingRecordingStatus.Sftp;
412+
var givenRecordingStatus = "SFTP";
413413
var givenClientReferenceId = "7e8d64c0-6c72-4922-aa96-48728753c660";
414414

415415
var givenRequest = $@"
@@ -460,7 +460,7 @@ void AssertNumberMaskingStatusRequest(NumberMaskingStatusRequest numberMaskingSt
460460
Assert.AreEqual(givenCurrency, numberMaskingStatusRequest.Currency);
461461
Assert.AreEqual(givenRecordingFileId, numberMaskingStatusRequest.RecordingFileId);
462462
Assert.AreEqual(givenRecordingCalleeAnnouncement, numberMaskingStatusRequest.RecordCalleeAnnouncement);
463-
Assert.AreEqual(givenRecordingStatus, numberMaskingStatusRequest.RecordingStatus);
463+
Assert.AreEqual(givenRecordingStatus, GetEnumAttributeValue(numberMaskingStatusRequest.RecordingStatus!));
464464
Assert.AreEqual(givenClientReferenceId, numberMaskingStatusRequest.ClientReferenceId);
465465
}
466466
}

ApiClient.Tests/Api/SmsApiTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,12 +1558,12 @@ public void ShouldReceiveOutBoundSmsMessageReport()
15581558
var givenPricePerMessage = 0.01m;
15591559
var givenCurrency = "EUR";
15601560
var givenStatusGroupId = 3;
1561-
var givenStatusGroupName = MessageGeneralStatus.Delivered;
1561+
var givenStatusGroupName = "DELIVERED";
15621562
var givenStatusId = 5;
15631563
var givenStatusName = "DELIVERED_TO_HANDSET";
15641564
var givenStatusDescription = "Message delivered to handset";
15651565
var givenErrorGroupId = 0;
1566-
var givenErrorGroupName = MessageErrorGroup.Ok;
1566+
var givenErrorGroupName = "USER_ERRORS";
15671567
var givenErrorId = 0;
15681568
var givenErrorName = "NO_ERROR";
15691569
var givenErrorDescription = "No Error";
@@ -1582,12 +1582,12 @@ public void ShouldReceiveOutBoundSmsMessageReport()
15821582
var givenSecondPricePerMessage = 0.01m;
15831583
var givenSecondCurrency = "EUR";
15841584
var givenSecondStatusGroupId = 3;
1585-
var givenSecondStatusGroupName = MessageGeneralStatus.Delivered;
1585+
var givenSecondStatusGroupName = "DELIVERED";
15861586
var givenSecondStatusId = 5;
15871587
var givenSecondStatusName = "DELIVERED_TO_HANDSET";
15881588
var givenSecondStatusDescription = "Message delivered to handset";
15891589
var givenSecondErrorGroupId = 0;
1590-
var givenSecondErrorGroupName = MessageErrorGroup.Ok;
1590+
var givenSecondErrorGroupName = "HANDSET_ERRORS";
15911591
var givenSecondErrorId = 0;
15921592
var givenSecondErrorName = "NO_ERROR";
15931593
var givenSecondErrorDescription = "No Error";
@@ -1691,13 +1691,13 @@ void AssertSmsDeliveryResult(SmsDeliveryResult smsDeliveryResult)
16911691
Assert.AreEqual(givenCurrency, smsDeliveryReport.Price.Currency);
16921692

16931693
Assert.AreEqual(givenStatusGroupId, smsDeliveryReport.Status.GroupId);
1694-
Assert.AreEqual(givenStatusGroupName, smsDeliveryReport.Status.GroupName);
1694+
Assert.AreEqual(givenStatusGroupName, GetEnumAttributeValue(smsDeliveryReport.Status.GroupName!));
16951695
Assert.AreEqual(givenStatusId, smsDeliveryReport.Status.Id);
16961696
Assert.AreEqual(givenStatusName, smsDeliveryReport.Status.Name);
16971697
Assert.AreEqual(givenStatusDescription, smsDeliveryReport.Status.Description);
16981698

16991699
Assert.AreEqual(givenErrorGroupId, smsDeliveryReport.Error.GroupId);
1700-
Assert.AreEqual(givenErrorGroupName, smsDeliveryReport.Error.GroupName);
1700+
Assert.AreEqual(givenErrorGroupName, GetEnumAttributeValue(smsDeliveryReport.Error.GroupName!));
17011701
Assert.AreEqual(givenErrorId, smsDeliveryReport.Error.Id);
17021702
Assert.AreEqual(givenErrorName, smsDeliveryReport.Error.Name);
17031703
Assert.AreEqual(givenErrorDescription, smsDeliveryReport.Error.Description);
@@ -1720,13 +1720,13 @@ void AssertSmsDeliveryResult(SmsDeliveryResult smsDeliveryResult)
17201720
Assert.AreEqual(givenSecondCurrency, smsSecondDeliveryReport.Price.Currency);
17211721

17221722
Assert.AreEqual(givenSecondStatusGroupId, smsSecondDeliveryReport.Status.GroupId);
1723-
Assert.AreEqual(givenSecondStatusGroupName, smsSecondDeliveryReport.Status.GroupName);
1723+
Assert.AreEqual(givenSecondStatusGroupName, GetEnumAttributeValue(smsSecondDeliveryReport.Status.GroupName!));
17241724
Assert.AreEqual(givenSecondStatusId, smsSecondDeliveryReport.Status.Id);
17251725
Assert.AreEqual(givenSecondStatusName, smsSecondDeliveryReport.Status.Name);
17261726
Assert.AreEqual(givenSecondStatusDescription, smsSecondDeliveryReport.Status.Description);
17271727

17281728
Assert.AreEqual(givenSecondErrorGroupId, smsSecondDeliveryReport.Error.GroupId);
1729-
Assert.AreEqual(givenSecondErrorGroupName, smsSecondDeliveryReport.Error.GroupName);
1729+
Assert.AreEqual(givenSecondErrorGroupName, GetEnumAttributeValue(smsSecondDeliveryReport.Error.GroupName!));
17301730
Assert.AreEqual(givenSecondErrorId, smsSecondDeliveryReport.Error.Id);
17311731
Assert.AreEqual(givenSecondErrorName, smsSecondDeliveryReport.Error.Name);
17321732
Assert.AreEqual(givenSecondErrorDescription, smsSecondDeliveryReport.Error.Description);

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to the library will be documented in this file.
55
The format of the file is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this library adheres to [Semantic Versioning](http://semver.org/) as mentioned in [README.md][readme] file.
77

8+
## [ [4.0.2](https://github.com/infobip/infobip-api-csharp-client/releases/tag/v4.0.2) ] - 2025-12-08
9+
10+
### Added
11+
- Custom Enum Converter for System.Text.Json (see ###fixed for more information)
12+
13+
### Fixed
14+
- Enum deserialization using System.Text.Json now correctly maps Json string values to enums using values defined by [EnumMember]
15+
816
## [ [4.0.1](https://github.com/infobip/infobip-api-csharp-client/releases/tag/v4.0.1) ] - 2025-12-03
917

1018
### Changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ Within Visual Studio, use the Package Manager UI to browse for `Infobip.Api.Clie
3737
#### Package Manager Console
3838
Alternatively, also within Visual Studio, use the Package Manager Console command:
3939

40-
Install-Package Infobip.Api.Client -Version 4.0.1
40+
Install-Package Infobip.Api.Client -Version 4.0.2
4141

4242
#### .NET CLI
4343
If you are used to .NET CLI, the following command is going to be sufficient for you:
4444

45-
dotnet add package Infobip.Api.Client --version 4.0.1
45+
dotnet add package Infobip.Api.Client --version 4.0.2
4646

4747
### Package reference
4848
Including the package directly into project file is also valid option.
4949

50-
<PackageReference Include="Infobip.Api.Client" Version="4.0.1" />
50+
<PackageReference Include="Infobip.Api.Client" Version="4.0.2" />
5151

5252
## Quickstart
5353

src/Infobip.Api.Client/Client/Configuration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Configuration : IReadableConfiguration
4141
/// Version of the package.
4242
/// </summary>
4343
/// <value>Version of the package.</value>
44-
public const string Version = "4.0.1";
44+
public const string Version = "4.0.2";
4545

4646
private string _dateTimeFormat = Iso8601DateTimeFormat;
4747

@@ -54,7 +54,7 @@ public class Configuration : IReadableConfiguration
5454
public Configuration()
5555
{
5656
Proxy = null;
57-
UserAgent = "infobip-api-client-csharp/4.0.1";
57+
UserAgent = "infobip-api-client-csharp/4.0.2";
5858
BasePath = "http://localhost";
5959
DefaultHeaders = new ConcurrentDictionary<string, string>();
6060

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Runtime.Serialization;
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace Infobip.Api.Client
9+
{
10+
public class JsonStringEnumMemberConverter<T> : JsonConverter<T> where T : struct, Enum
11+
{
12+
private readonly Dictionary<string, T> _fromValue;
13+
private readonly Dictionary<T, string> _toValue;
14+
15+
public JsonStringEnumMemberConverter()
16+
{
17+
_fromValue = new Dictionary<string, T>(StringComparer.OrdinalIgnoreCase);
18+
_toValue = new Dictionary<T, string>();
19+
foreach (var field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static))
20+
{
21+
var enumValue = (T)field.GetValue(null);
22+
var enumMemberAttr = field.GetCustomAttribute<EnumMemberAttribute>();
23+
var strValue = enumMemberAttr?.Value ?? field.Name;
24+
_fromValue[strValue] = enumValue;
25+
_toValue[enumValue] = strValue;
26+
}
27+
}
28+
29+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
30+
{
31+
if (reader.TokenType == JsonTokenType.String)
32+
{
33+
var enumString = reader.GetString();
34+
if (_fromValue.TryGetValue(enumString, out var value))
35+
return value;
36+
throw new JsonException($"Unknown value '{enumString}' for enum '{typeof(T).Name}'");
37+
}
38+
throw new JsonException($"Unexpected token parsing enum. Expected String, got {reader.TokenType}.");
39+
}
40+
41+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
42+
{
43+
if (_toValue.TryGetValue(value, out var stringValue))
44+
writer.WriteStringValue(stringValue);
45+
else
46+
writer.WriteStringValue(value.ToString());
47+
}
48+
}
49+
}

src/Infobip.Api.Client/Infobip.Api.Client.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<Description>C# library for consuming Infobip&#39;s API</Description>
1313
<Copyright>Copyright @ Infobip ltd. All rights reserved.</Copyright>
1414
<RootNamespace>Infobip.Api.Client</RootNamespace>
15-
<Version>4.0.1</Version>
16-
<FileVersion>4.0.1</FileVersion>
17-
<AssemblyVersion>4.0.1</AssemblyVersion>
15+
<Version>4.0.2</Version>
16+
<FileVersion>4.0.2</FileVersion>
17+
<AssemblyVersion>4.0.2</AssemblyVersion>
1818
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Infobip.Api.Client.xml</DocumentationFile>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<RepositoryUrl>https://github.com/infobip/infobip-api-csharp-client.git</RepositoryUrl>

src/Infobip.Api.Client/Model/CallDirection.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
using System.Runtime.Serialization;
13-
using System.Text.Json.Serialization;
1413
using Newtonsoft.Json.Converters;
1514
using JsonConverterAttribute = Newtonsoft.Json.JsonConverterAttribute;
1615

@@ -21,7 +20,7 @@ namespace Infobip.Api.Client.Model
2120
/// </summary>
2221
/// <value>Call direction.</value>
2322
[JsonConverter(typeof(StringEnumConverter))]
24-
[System.Text.Json.Serialization.JsonConverter(typeof(JsonStringEnumConverter))]
23+
[System.Text.Json.Serialization.JsonConverter(typeof(JsonStringEnumMemberConverter<CallDirection>))]
2524
public enum CallDirection
2625
{
2726
/// <summary>

src/Infobip.Api.Client/Model/CallEndpointType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
using System.Runtime.Serialization;
13-
using System.Text.Json.Serialization;
1413
using Newtonsoft.Json.Converters;
1514
using JsonConverterAttribute = Newtonsoft.Json.JsonConverterAttribute;
1615

@@ -20,7 +19,7 @@ namespace Infobip.Api.Client.Model
2019
/// Defines CallEndpointType.
2120
/// </summary>
2221
[JsonConverter(typeof(StringEnumConverter))]
23-
[System.Text.Json.Serialization.JsonConverter(typeof(JsonStringEnumConverter))]
22+
[System.Text.Json.Serialization.JsonConverter(typeof(JsonStringEnumMemberConverter<CallEndpointType>))]
2423
public enum CallEndpointType
2524
{
2625
/// <summary>

0 commit comments

Comments
 (0)