Skip to content

Commit 96d01f3

Browse files
authored
fix: update functionality for collection with nested collection (#100)
1 parent 56764b5 commit 96d01f3

File tree

4 files changed

+99
-4
lines changed

4 files changed

+99
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
### [Unreleased]
4+
* FIXED: Update functionality when objects has collections with nested collections
45

56
### [2.4.2] - 2023-06-25
67
* FIXED: Duplicate collection data to JSON on save when configured case was not used with collection name

JsonFlatFileDataStore.Test/CollectionModificationTests.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,55 @@ public async Task UpdateOneAsync_TypedModel_InnerSimpleIntArray()
294294
UTHelpers.Down(newFilePath);
295295
}
296296

297+
[Fact]
298+
public async Task UpdateOneAsync_TypedModel_NestedArrays()
299+
{
300+
var newFilePath = UTHelpers.Up();
301+
302+
var store = new DataStore(newFilePath);
303+
304+
var collection = store.GetCollection<TestModelWithNestedArray>();
305+
Assert.Equal(0, collection.Count);
306+
307+
var newModel = new TestModelWithNestedArray
308+
{
309+
Id = Guid.NewGuid().ToString(),
310+
Type = "empty",
311+
NestedLists = new List<List<int>> { new List<int> { 1 }, new List<int> { 2 }, new List<int> { 3 } }
312+
};
313+
314+
var insertResult = collection.InsertOne(newModel);
315+
Assert.True(insertResult);
316+
Assert.Equal(1, collection.Count);
317+
318+
var store2 = new DataStore(newFilePath);
319+
var collection2 = store2.GetCollection<TestModelWithNestedArray>();
320+
Assert.Equal(1, collection2.Count);
321+
322+
var updateData = new
323+
{
324+
Type = "filled",
325+
NestedLists = new List<List<int>>
326+
{
327+
null,
328+
new List<int> { 4 },
329+
}
330+
};
331+
332+
await collection2.UpdateOneAsync(e => e.Id == newModel.Id, updateData);
333+
334+
var store3 = new DataStore(newFilePath);
335+
var collection3 = store3.GetCollection<TestModelWithNestedArray>();
336+
var updated = collection3.Find(e => e.Id == newModel.Id).First();
337+
Assert.Equal(3, updated.NestedLists.Count());
338+
Assert.Equal(1, updated.NestedLists[0].First());
339+
Assert.Equal(4, updated.NestedLists[1].First());
340+
Assert.Equal(3, updated.NestedLists[2].First());
341+
Assert.Equal("filled", updated.Type);
342+
343+
UTHelpers.Down(newFilePath);
344+
}
345+
297346
[Fact]
298347
public async Task UpdateOneAsync_Predicate_Id_DynamicUser()
299348
{
@@ -988,5 +1037,43 @@ public async Task UpdateInnerFloatArray_TypedWorld()
9881037

9891038
UTHelpers.Down(newFilePath);
9901039
}
1040+
1041+
[Fact]
1042+
public async Task UpdateComplexObject_Dynamic()
1043+
{
1044+
var newFilePath = UTHelpers.Up();
1045+
1046+
var store = new DataStore(newFilePath);
1047+
1048+
var collection = store.GetCollection("employee");
1049+
1050+
var ja = new JArray { "Hello World!" };
1051+
1052+
var jObj = new JObject()
1053+
{
1054+
["custom_id"] = 11,
1055+
["nestedArray"] = new JArray { ja },
1056+
};
1057+
1058+
await collection.InsertOneAsync(jObj);
1059+
1060+
var original = collection.Find(e => e.custom_id == 11).First();
1061+
Assert.Equal(0, original.id);
1062+
Assert.Equal(11, original.custom_id);
1063+
Assert.Equal("Hello World!", original.nestedArray[0][0]);
1064+
1065+
var update = new JObject()
1066+
{
1067+
["custom_id"] = 12,
1068+
["nestedArray"] = new JArray { new JArray { "Other text" } },
1069+
};
1070+
1071+
await collection.UpdateOneAsync(e => e.custom_id == 11, update);
1072+
1073+
var updated = collection.Find(e => e.custom_id == 12).First();
1074+
Assert.Equal("Other text", updated.nestedArray[0][0]);
1075+
1076+
UTHelpers.Down(newFilePath);
1077+
}
9911078
}
9921079
}

JsonFlatFileDataStore.Test/TestModels.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public class TestModelWithIntArray
1717
public List<int> Fragments { get; set; }
1818
}
1919

20+
public class TestModelWithNestedArray
21+
{
22+
public string Id { get; set; }
23+
public string Type { get; set; }
24+
public List<List<int>> NestedLists { get; set; }
25+
}
26+
2027
public class User
2128
{
2229
public int Id { get; set; }

JsonFlatFileDataStore/ObjectExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private static void HandleTypedEnumerable(object source, object destination, dyn
223223
}
224224

225225
var targetPropertyType = targetProperty.PropertyType;
226-
var type = IsGenericListOrCollection(targetPropertyType) ? targetPropertyType.GetGenericArguments()[0] : targetPropertyType.GetElementType();
226+
var targetType = IsGenericListOrCollection(targetPropertyType) ? targetPropertyType.GetGenericArguments()[0] : targetPropertyType.GetElementType();
227227

228228
for (var i = 0; i < sourceArray.Count; i++)
229229
{
@@ -234,11 +234,11 @@ private static void HandleTypedEnumerable(object source, object destination, dyn
234234

235235
if (targetArray.Count - 1 < i)
236236
{
237-
var newTargetItem = CreateInstance(type);
237+
var newTargetItem = CreateInstance(targetType);
238238
targetArray.Add(newTargetItem);
239239
}
240240

241-
if (type.GetTypeInfo().IsValueType || type == typeof(string))
241+
if (targetType.GetTypeInfo().IsValueType || targetType == typeof(string) || IsDictionary(targetType) || IsEnumerable(targetType))
242242
targetArray[i] = sourceValue;
243243
else
244244
CopyProperties(sourceValue, targetArray[i]);
@@ -340,7 +340,7 @@ Type GetTypeFromTargetItem(IList target, int index)
340340
targetArray.Add(CreateInstance(targetType));
341341
}
342342

343-
if (targetType.GetTypeInfo().IsValueType || targetType == typeof(string))
343+
if (targetType.GetTypeInfo().IsValueType || targetType == typeof(string) || IsDictionary(targetType) || IsEnumerable(targetType))
344344
targetArray[i] = sourceValue;
345345
else
346346
CopyProperties(sourceValue, targetArray[i]);

0 commit comments

Comments
 (0)