diff --git a/changelog.md b/changelog.md index 1718807..6e261a4 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased +### Fixed + +- [#62](https://github.com/Azure/Microsoft.Extensions.Caching.Cosmos/pull/62) Fixed error text to point to correct missing property when deserializing session object + ## 1.2.0 - 2022-02-23 ### Added diff --git a/src/CosmosCacheSessionConverter.cs b/src/CosmosCacheSessionConverter.cs index c909def..53e489a 100644 --- a/src/CosmosCacheSessionConverter.cs +++ b/src/CosmosCacheSessionConverter.cs @@ -36,14 +36,14 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist if (!jObject.TryGetValue(CosmosCacheSessionConverter.IdAttributeName, out JToken idJToken)) { - throw new JsonReaderException("Missing id on Cosmos DB session item."); + throw new JsonReaderException("Missing 'id' on Cosmos DB session item."); } cosmosCacheSession.SessionKey = idJToken.Value(); if (!jObject.TryGetValue(CosmosCacheSessionConverter.ContentAttributeName, out JToken contentJToken)) { - throw new JsonReaderException("Missing id on Cosmos DB session item."); + throw new JsonReaderException("Missing 'content' on Cosmos DB session item."); } cosmosCacheSession.Content = Convert.FromBase64String(contentJToken.Value()); diff --git a/tests/unit/CosmosSessionSerializationTests.cs b/tests/unit/CosmosSessionSerializationTests.cs index 399d8f7..176a785 100644 --- a/tests/unit/CosmosSessionSerializationTests.cs +++ b/tests/unit/CosmosSessionSerializationTests.cs @@ -78,5 +78,17 @@ public void ValidatesContract() string serialized = JsonConvert.SerializeObject(existingSession); Assert.Equal(expectedContract , serialized); } + + [Fact] + public void MissingRequiredProperties() + { + const string withoutId = "{\"content\":\"AQ==\",\"ttl\":5,\"isSlidingExpiration\":true,\"absoluteSlidingExpiration\":10}"; + JsonReaderException withoutIdException = Assert.Throws(() => JsonConvert.DeserializeObject(withoutId)); + Assert.Contains("Missing 'id'", withoutIdException.Message); + + const string withoutContent = "{\"id\":\"1\", \"ttl\":5,\"isSlidingExpiration\":true,\"absoluteSlidingExpiration\":10}"; + JsonReaderException withoutContentException = Assert.Throws(() => JsonConvert.DeserializeObject(withoutContent)); + Assert.Contains("Missing 'content'", withoutContentException.Message); + } } } \ No newline at end of file