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

Subtle bug in deserializing payload with property without value #1307

Open
gathogojr opened this issue Sep 6, 2024 · 0 comments · May be fixed by #1308
Open

Subtle bug in deserializing payload with property without value #1307

gathogojr opened this issue Sep 6, 2024 · 0 comments · May be fixed by #1308
Assignees
Labels
bug Something isn't working P2

Comments

@gathogojr
Copy link
Contributor

gathogojr commented Sep 6, 2024

Assemblies affected

  • Microsoft.AspNetCore.OData 9.x

Describe the bug
After introducing support for reading properties without value in OData .NET 9, there's a subtle bug in this line of code

foreach (ODataProperty property in resourceWrapper.Resource.Properties)

The property type for ODataResource.Properties is IEnumerable<ODataPropertyInfo> and not IEnumerable<ODataProperty> as it was in OData .NET 8.

While the statement compiles okay, if an object of ODataPropertyInfo is present in the Properties collection property, an exception is thrown.

Reproduce steps
The issue can be reproduced using the following sample code:

using System;
using System.Collections.Generic;

class ODataPropertyInfo
{
    public string Name { get; set; }
}

class ODataProperty : ODataPropertyInfo
{
    public object Value { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var properties = new List<ODataPropertyInfo>()
        {
            new ODataProperty { Name = "Id", Value = 13 },
            new ODataPropertyInfo { Name = "Password" },
            new ODataProperty { Name = "LogonName", Value = "foobar" }
        };

        foreach (ODataProperty property in properties)
        {
            Console.WriteLine($"Name = {property.Name}, Value = {property.Value}");
        }
    }
}

Exception:

Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
  at Program.Main (System.String[] args) [0x0007a] in <86b2fbb858794e808f5e125cc32a50ce>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Specified cast is not valid.
  at Program.Main (System.String[] args) [0x0007a] in <86b2fbb858794e808f5e125cc32a50ce>:0

Expected behavior
The property without value (i,e,. ODataPropertyInfo) should be handled gracefully.

@gathogojr gathogojr added bug Something isn't working P2 labels Sep 6, 2024
@gathogojr gathogojr self-assigned this Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant