ETag property throws null ref exception when there's no raw response available (4.0.0-preview3)
See original GitHub issueDescribe the bug
Inheriting from ItemResponse<T>
class and not overriding ETag
property in the derived class will cause null reference exception being thrown when you try to access the ETag field despite it being nullable.
Looking at the source code, this seems to happen when there is no raw response available; https://github.com/Azure/azure-cosmos-dotnet-v3/blob/releases/4.0.0-preview3/Microsoft.Azure.Cosmos/azuredata/Resource/Container/ItemResponse.cs#L70
To Reproduce
public class MockItemResponse<T> : ItemResponse<T>
{
public MockItemResponse(T value) { }
}
class Program
{
static void Main(string[] args)
{
var response = new MockItemResponse<string>("test");
var test1 = response.ETag.HasValue; // Null ref exception
var test2 = response.ETag == null; // Null ref exception
var test3 = response?.ETag == null; // Null ref exception
}
}
Expected behavior Return null.
Actual behavior NullReferenceException is being thrown.
Environment summary SDK Version: 4.0.0-preview3 OS Version: Windows
Additional context I assume this it not an issue you run into in normal use, but it is a bit unexpected behavior when writing mocks for unit testing purposes, given that the property is marked as nullable.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
@Jaah thanks for reporting this. It should be updated to do the following.
this.GetRawResponse()?.Headers?.ETag;
Closing as mocking is possible and ETag is always expected on the real scenario