Skip to content

Commit 62f21a9

Browse files
authored
Remove duplicates in Search API fields
1 parent e7e7dfc commit 62f21a9

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

CloudinaryDotNet.Tests/SearchTest.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Newtonsoft.Json.Linq;
4+
using NUnit.Framework;
5+
6+
namespace CloudinaryDotNet.Tests
7+
{
8+
public class SearchTest
9+
{
10+
private MockedCloudinary m_cloudinary = new MockedCloudinary();
11+
12+
[Test]
13+
public void TestShouldNotDuplicateValues()
14+
{
15+
m_cloudinary
16+
.Search()
17+
.SortBy("created_at", "asc")
18+
.SortBy("created_at", "desc")
19+
.SortBy("public_id", "asc")
20+
.Aggregate("format")
21+
.Aggregate("format")
22+
.Aggregate("resource_type")
23+
.WithField("context")
24+
.WithField("context")
25+
.WithField("tags")
26+
.Execute();
27+
28+
AssertCorrectRequest(m_cloudinary.HttpRequestContent);
29+
}
30+
31+
private void AssertCorrectRequest(string request)
32+
{
33+
var requestJson = JToken.Parse(request);
34+
35+
Assert.IsNotNull(requestJson["sort_by"]);
36+
Assert.AreEqual(
37+
new List<Dictionary<string, string>>
38+
{
39+
new Dictionary<string, string> { ["created_at"] = "desc" },
40+
new Dictionary<string, string> { ["public_id"] = "asc" }
41+
},
42+
requestJson["sort_by"]
43+
.Children<JObject>()
44+
.Select(item =>
45+
new Dictionary<string, string>
46+
{
47+
[item.Properties().First().Name] = item.Properties().First().Value.ToString()
48+
})
49+
);
50+
51+
Assert.IsNotNull(requestJson["aggregate"]);
52+
Assert.AreEqual(new[] { "format", "resource_type" }, requestJson["aggregate"].Values<string>());
53+
54+
Assert.IsNotNull(requestJson["with_field"]);
55+
Assert.AreEqual(new[] { "context", "tags" }, requestJson["with_field"].Values<string>());
56+
}
57+
}
58+
}

CloudinaryDotNet/Search.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace CloudinaryDotNet
22
{
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using CloudinaryDotNet.Actions;
@@ -129,17 +130,17 @@ public Dictionary<string, object> ToQuery()
129130

130131
if (withFieldParam.Count > 0)
131132
{
132-
queryParams.Add("with_field", withFieldParam);
133+
queryParams.Add("with_field", withFieldParam.Distinct());
133134
}
134135

135136
if (sortByParam.Count > 0)
136137
{
137-
queryParams.Add("sort_by", sortByParam);
138+
queryParams.Add("sort_by", sortByParam.GroupBy(d => d.Keys.First()).Select(l => l.Last()));
138139
}
139140

140141
if (aggregateParam.Count > 0)
141142
{
142-
queryParams.Add("aggregate", aggregateParam);
143+
queryParams.Add("aggregate", aggregateParam.Distinct());
143144
}
144145

145146
return queryParams;

0 commit comments

Comments
 (0)