diff --git a/JsonFlatFileDataStore.Test/CollectionTests.cs b/JsonFlatFileDataStore.Test/CollectionTests.cs index 1d67d82..89ce29f 100644 --- a/JsonFlatFileDataStore.Test/CollectionTests.cs +++ b/JsonFlatFileDataStore.Test/CollectionTests.cs @@ -56,10 +56,11 @@ public void GetNextIdValue_IntegerId() var nextId = collection.GetNextIdValue(); Assert.Equal(4, nextId); + collection.InsertOne(new { id = nextId + 1 }); collection.InsertOne(new { id = nextId }); nextId = collection.GetNextIdValue(); - Assert.Equal(5, nextId); + Assert.Equal(6, nextId); } [Fact] @@ -124,7 +125,7 @@ public void GetNextIdValue_StringType_AnonymousType() collection.InsertOne(new { myId = "hello2" }); nextId = collection.GetNextIdValue(); - Assert.Equal("hello3", nextId); + Assert.Equal("hello5", nextId); var same = collection.AsQueryable().Where(e => e.myId == "hello2"); Assert.Equal(2, same.Count()); @@ -140,17 +141,19 @@ public void GetNextIdValue_StringType_JToken() var collection = store.GetCollection("collectionWithStringId"); // Insert seed value with upsert - collection.ReplaceOne(e => e, JToken.Parse("{ 'myId': 'test1' }"), true); + collection.ReplaceOne(e => true, JToken.Parse("{ 'myId': 'test1' }"), true); + collection.InsertOne(JToken.Parse("{ 'myId': 'test3' }")); + collection.InsertOne(JToken.Parse("{ 'myId': 'test2' }")); var nextId = collection.GetNextIdValue(); - Assert.Equal("test2", nextId); + Assert.Equal("test4", nextId); - var nextUpdate = JToken.Parse("{ 'myId': 'somethingWrong2' }"); + var nextUpdate = JToken.Parse("{ 'myId': 'somethingWrong4' }"); collection.InsertOne(nextUpdate); Assert.Equal(nextId, nextUpdate["myId"]); nextId = collection.GetNextIdValue(); - Assert.Equal("test3", nextId); + Assert.Equal("test5", nextId); nextUpdate = JToken.Parse("{ 'xxx': 111 }"); collection.InsertOne(nextUpdate); @@ -158,7 +161,7 @@ public void GetNextIdValue_StringType_JToken() Assert.Equal(111, nextUpdate["xxx"]); nextId = collection.GetNextIdValue(); - Assert.Equal("test4", nextId); + Assert.Equal("test6", nextId); } [Fact] diff --git a/JsonFlatFileDataStore/DocumentCollection.cs b/JsonFlatFileDataStore/DocumentCollection.cs index 57ae392..7950ccd 100644 --- a/JsonFlatFileDataStore/DocumentCollection.cs +++ b/JsonFlatFileDataStore/DocumentCollection.cs @@ -372,20 +372,29 @@ private dynamic GetNextIdValue(List data) if (!data.Any()) return 0; - var lastItem = data.Last(); - var expando = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(lastItem), new ExpandoObjectConverter()); - // Problem here is if we have typed data with upper camel case properties but lower camel case in JSON, so need to use OrdinalIgnoreCase string comparer - var expandoAsIgnoreCase = new Dictionary(expando, StringComparer.OrdinalIgnoreCase); + var idValues = data.Select(item => + { + var expando = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(item), new ExpandoObjectConverter()); + // Problem here is if we have typed data with upper camel case properties but lower camel case in JSON, so need to use OrdinalIgnoreCase string comparer + var expandoIgnoreCase = new Dictionary(expando, StringComparer.OrdinalIgnoreCase); + + if (!expandoIgnoreCase.ContainsKey(_idField)) + { + return null; + } + + return expandoIgnoreCase[_idField]; + }); - if (!expandoAsIgnoreCase.ContainsKey(_idField)) - return null; + var maxIdValue = idValues.Max(); - dynamic keyValue = expandoAsIgnoreCase[_idField]; + if (maxIdValue == null) + return 0; - if (keyValue is Int64) - return (int)keyValue + 1; + if (maxIdValue is Int64) + return (int)maxIdValue + 1; - return ParseNextIntegertToKeyValue(keyValue.ToString()); + return ParseNextIntegertToKeyValue(maxIdValue.ToString()); } private string ParseNextIntegertToKeyValue(string input)