Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors: Fixes error text to point to correct missing property when deserializing session object #62

Merged
merged 3 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

## <a name="1.2.0"/> 1.2.0 - 2022-02-23

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/CosmosCacheSessionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();

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<string>());
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/CosmosSessionSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonReaderException>(() => JsonConvert.DeserializeObject<CosmosCacheSession>(withoutId));
Assert.Contains("Missing 'id'", withoutIdException.Message);

const string withoutContent = "{\"id\":\"1\", \"ttl\":5,\"isSlidingExpiration\":true,\"absoluteSlidingExpiration\":10}";
JsonReaderException withoutContentException = Assert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<CosmosCacheSession>(withoutContent));
Assert.Contains("Missing 'content'", withoutContentException.Message);
}
}
}