Skip to content

Commit 966a008

Browse files
Fixed issue when parsing enums in query parameters.
1 parent 2c582f5 commit 966a008

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

ApiClient.Tests/Api/ApiTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Reflection;
23
using System.Runtime.Serialization;
34
using Infobip.Api.Client;
@@ -29,6 +30,8 @@ public class ApiTest
2930
[TestInitialize]
3031
public void StartMockServer()
3132
{
33+
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
34+
3235
wireMockServer = WireMockServer.Start();
3336

3437
configuration = new Configuration

ApiClient.Tests/Api/CallsApiTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6430,7 +6430,7 @@ public void ShouldGetCallRecordings()
64306430
var givenLocation = CallsRecordingLocation.Frankfurt;
64316431
var givenQueryParameters = new Dictionary<string, string>
64326432
{
6433-
{ "location", givenLocation.ToString() }
6433+
{ "location", GetEnumAttributeValue(givenLocation) }
64346434
};
64356435

64366436
var givenResponse = @"
@@ -6505,7 +6505,7 @@ public void ShouldDeleteCallRecordings()
65056505
var givenLocation = CallsRecordingLocation.Frankfurt;
65066506
var givenQueryParameters = new Dictionary<string, string>
65076507
{
6508-
{ "location", givenLocation.ToString() }
6508+
{ "location", GetEnumAttributeValue(givenLocation) }
65096509
};
65106510

65116511
var givenResponse = @"
@@ -6814,7 +6814,7 @@ public void ShouldGetConferenceRecordings()
68146814
var givenLocation = CallsRecordingLocation.Frankfurt;
68156815
var givenQueryParameters = new Dictionary<string, string>
68166816
{
6817-
{ "location", givenLocation.ToString() }
6817+
{ "location", GetEnumAttributeValue(givenLocation) }
68186818
};
68196819

68206820
var givenResponse = @"
@@ -7000,7 +7000,7 @@ public void ShouldDeleteConferenceRecording()
70007000
var givenLocation = CallsRecordingLocation.Frankfurt;
70017001
var givenQueryParameters = new Dictionary<string, string>
70027002
{
7003-
{ "location", givenLocation.ToString() }
7003+
{ "location", GetEnumAttributeValue(givenLocation) }
70047004
};
70057005

70067006
var givenResponse = @"
@@ -7186,7 +7186,7 @@ public void ShouldComposeConferenceRecordingOnCalls()
71867186
var givenLocation = CallsRecordingLocation.Frankfurt;
71877187
var givenQueryParameters = new Dictionary<string, string>
71887188
{
7189-
{ "location", givenLocation.ToString() }
7189+
{ "location", GetEnumAttributeValue(givenLocation) }
71907190
};
71917191

71927192
var givenRequest = @"
@@ -7457,7 +7457,7 @@ public void ShouldGetDialogRecordings()
74577457
var givenLocation = CallsRecordingLocation.Frankfurt;
74587458
var givenQueryParameters = new Dictionary<string, string>
74597459
{
7460-
{ "location", givenLocation.ToString() }
7460+
{ "location", GetEnumAttributeValue(givenLocation) }
74617461
};
74627462

74637463
var givenResponse = @"
@@ -7639,7 +7639,7 @@ public void ShouldDeleteDialogRecordings()
76397639
var givenLocation = CallsRecordingLocation.Frankfurt;
76407640
var givenQueryParameters = new Dictionary<string, string>
76417641
{
7642-
{ "location", givenLocation.ToString() }
7642+
{ "location", GetEnumAttributeValue(givenLocation) }
76437643
};
76447644

76457645
var givenResponse = @"
@@ -7821,7 +7821,7 @@ public void ShouldComposeDialogRecordingOnCalls()
78217821
var givenLocation = CallsRecordingLocation.Frankfurt;
78227822
var givenQueryParameters = new Dictionary<string, string>
78237823
{
7824-
{ "location", givenLocation.ToString() }
7824+
{ "location", GetEnumAttributeValue(givenLocation) }
78257825
};
78267826

78277827
var givenRequest = @"
@@ -7865,7 +7865,7 @@ public void ShouldDeleteRecordingFile()
78657865
var givenLocation = CallsRecordingLocation.Frankfurt;
78667866
var givenQueryParameters = new Dictionary<string, string>
78677867
{
7868-
{ "location", givenLocation.ToString() }
7868+
{ "location", GetEnumAttributeValue(givenLocation) }
78697869
};
78707870

78717871
var givenResponse = @"

ApiClient.Tests/Api/EmailApiTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ public void ShouldGetSuppresions()
11071107
var givenQueryParameters = new Dictionary<string, string>
11081108
{
11091109
{ "domainName", expectedEmailAddress },
1110-
{ "type", givenType.ToString() },
1110+
{ "type", GetEnumAttributeValue(EmailSuppressionType.Bounce) },
11111111
{ "page", givenPage.ToString() },
11121112
{ "size", givenSize.ToString() }
11131113
};
@@ -1134,16 +1134,16 @@ void AssertEmailSuppressionInfoPageResponse(EmailSuppressionInfoPageResponse ema
11341134
Assert.AreEqual(expectedSize, emailSuppressionInfoPageResponse.Paging.Size);
11351135
}
11361136

1137-
AssertResponse(emailApi.GetSuppressions(givenDomainName, givenType),
1137+
AssertResponse(emailApi.GetSuppressions(domainName: givenDomainName, type: givenType, page: givenPage, size:givenSize),
11381138
AssertEmailSuppressionInfoPageResponse);
1139-
AssertResponse(emailApi.GetSuppressionsAsync(givenDomainName, givenType).Result,
1139+
AssertResponse(emailApi.GetSuppressionsAsync(domainName: givenDomainName, type: givenType, page: givenPage, size:givenSize).Result,
11401140
AssertEmailSuppressionInfoPageResponse);
11411141

11421142
AssertResponseWithHttpInfo(
1143-
emailApi.GetSuppressionsWithHttpInfo(givenDomainName, givenType),
1143+
emailApi.GetSuppressionsWithHttpInfo(domainName: givenDomainName, type: givenType, page: givenPage, size:givenSize),
11441144
AssertEmailSuppressionInfoPageResponse, 200);
11451145
AssertResponseWithHttpInfo(
1146-
emailApi.GetSuppressionsWithHttpInfoAsync(givenDomainName, givenType).Result,
1146+
emailApi.GetSuppressionsWithHttpInfoAsync(domainName: givenDomainName, type: givenType, page: givenPage, size:givenSize).Result,
11471147
AssertEmailSuppressionInfoPageResponse, 200);
11481148
}
11491149

ApiClient.Tests/Api/SmsApiTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,10 +1027,10 @@ public void ShouldGetOutboundLogs()
10271027
}}";
10281028

10291029
SetUpGetRequest(SMS_LOGS_ENDPOINT, 200, expectedResponse,
1030-
new Dictionary<string, string> { { "bulkId", bulkId } });
1030+
new Dictionary<string, string> { { "bulkId", bulkId }, {"generalStatus", GetEnumAttributeValue(MessageGeneralStatus.Accepted)}});
10311031

10321032
var smsApiClient = new SmsApi(configuration);
1033-
var actualResponse = smsApiClient.GetOutboundSmsMessageLogs(bulkId: new List<string> { bulkId });
1033+
var actualResponse = smsApiClient.GetOutboundSmsMessageLogs(bulkId: new List<string> {bulkId}, generalStatus: MessageGeneralStatus.Accepted);
10341034

10351035
var expectedDeserializedLogs = new SmsLogsResponse(new List<SmsLog>
10361036
{

src/Infobip.Api.Client/Client/ClientUtils.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using System.Globalization;
1515
using System.IO;
1616
using System.Linq;
17+
using System.Reflection;
18+
using System.Runtime.Serialization;
1719
using System.Text;
1820
using System.Text.RegularExpressions;
1921

@@ -117,10 +119,29 @@ public static string ParameterToString(object obj)
117119
return boolean ? "true" : "false";
118120
if (obj is ICollection collection)
119121
return string.Join(",", collection.Cast<object>());
122+
if (obj is Enum)
123+
return EnumToString(obj);
120124

121125
return Convert.ToString(obj, CultureInfo.InvariantCulture);
122126
}
123127

128+
/// <summary>
129+
/// Returns the string value of an enum, preferring [EnumMember(Value=...)] if present, otherwise ToString().
130+
/// </summary>
131+
/// <param name="enumValue">The enum value to be transformed into string.</param>
132+
/// <returns>A string representation of the enum value.</returns>
133+
public static string EnumToString(object enumValue)
134+
{
135+
var type = enumValue.GetType();
136+
var enumType = Nullable.GetUnderlyingType(type) ?? type;
137+
if (!enumType.IsEnum)
138+
return Convert.ToString(enumValue, CultureInfo.InvariantCulture);
139+
var name = Convert.ToString(enumValue, CultureInfo.InvariantCulture);
140+
var member = enumType.GetMember(name).FirstOrDefault();
141+
var attr = member?.GetCustomAttribute<EnumMemberAttribute>();
142+
return attr?.Value ?? name.ToUpperInvariant();
143+
}
144+
124145
/// <summary>
125146
/// Convert stream to byte array
126147
/// </summary>

0 commit comments

Comments
 (0)