question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ETag property throws null ref exception when there's no raw response available (4.0.0-preview3)

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
j82wcommented, Feb 14, 2020

@Jaah thanks for reporting this. It should be updated to do the following.

this.GetRawResponse()?.Headers?.ETag;

0reactions
ealsurcommented, Feb 20, 2020

Closing as mocking is possible and ETag is always expected on the real scenario

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What is a NullReferenceException, and how do I fix it?
I have some code and when it executes, it throws a NullReferenceException , saying: Object reference not set to an instance of an...
Read more >
Azure PowerShell release notes
Learn about all of the latest updates to the Azure PowerShell modules.
Read more >
How can I fix the error: System.NullReferenceException
NullReferenceException : 'Object reference not set to an instance of an object.' Doesn't make sense to me cause the statement under it is...
Read more >
Changelog 2023 - CAPire
2 for creating Java projects. [cds-dk@7.0.3] cds build now throws an error if the mtx sidecar and application db configuration is inconsistent.
Read more >
Changelog | Cypress Documentation
It now logs the yielded request/response object associated to the intercepted call ... Cypress no longer throws the error "cannot read property split...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found