Skip to content

Commit 88b0095

Browse files
authored
fix: AlgoliaHelper.GetObjectID (#824)
1 parent d965bae commit 88b0095

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Algolia.Search.Utils;
2+
using Newtonsoft.Json.Linq;
3+
using NUnit.Framework;
4+
5+
namespace Algolia.Search.Test.Utils;
6+
7+
class TestRecord
8+
{
9+
public string ObjectID { get; set; }
10+
public string Name { get; set; }
11+
}
12+
13+
[TestFixture]
14+
[Parallelizable]
15+
public class AlgoliaHelperTest
16+
{
17+
[Test]
18+
[Parallelizable]
19+
public void TestGetObjectIdFromClass()
20+
{
21+
var objectId = "1-2_1-2";
22+
var record = new TestRecord
23+
{
24+
ObjectID = objectId,
25+
Name = "this is a test"
26+
};
27+
Assert.AreEqual(objectId, AlgoliaHelper.GetObjectID(record));
28+
}
29+
30+
[Test]
31+
[Parallelizable]
32+
public void TestGetObjectIdFromJObject()
33+
{
34+
var objectId = "1-2_1-2";
35+
var record = new JObject
36+
{
37+
{ "objectID", objectId },
38+
{ "name", "this is a test" }
39+
};
40+
Assert.AreEqual(objectId, AlgoliaHelper.GetObjectID(record));
41+
}
42+
}

src/Algolia.Search/Utils/AlgoliaHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ internal static string GetObjectID<T>(T data)
105105

106106
if (itemType == typeof(JObject))
107107
{
108-
JProperty objectID = JObject.FromObject(data).Property("objectID");
108+
var objectIdJProperty = JObject.FromObject(data).Property("objectID");
109109

110-
if (objectID != null && objectID.ToString() != "")
110+
if (objectIdJProperty != null && !string.IsNullOrWhiteSpace(objectIdJProperty.Value.ToString()))
111111
{
112-
return objectID.ToString();
112+
return objectIdJProperty.Value.ToString();
113113
}
114114
}
115115

0 commit comments

Comments
 (0)