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

Using $compute with ODataQueryOptions<T> not supported? #921

Closed
blueghostuk opened this issue May 15, 2023 Discussed in #920 · 3 comments · Fixed by #928
Closed

Using $compute with ODataQueryOptions<T> not supported? #921

blueghostuk opened this issue May 15, 2023 Discussed in #920 · 3 comments · Fixed by #928
Assignees
Labels
bug Something isn't working

Comments

@blueghostuk
Copy link

blueghostuk commented May 15, 2023

Discussed in #920

Originally posted by blueghostuk May 15, 2023
$compute does not seem to work when using ODataQueryOptions<T>. A simple example calculating the length of a string and selecting it.

We are using EF Core, if I just pass $compute and $select, it fails with an error like:

Microsoft.OData.ODataException: Could not find a property named '{{propertyName}}' on type '{{entityType}}'.

If I also pass a $filter, I can see the SQL being generated but then fails with:
System.InvalidOperationException: Sequence contains no matching element at System.Linq.ThrowHelper.ThrowNoMatchException() at Microsoft.AspNetCore.OData.Query.Container.JsonPropertyNameMapper.MapProperty(String propertyName) at Microsoft.AspNetCore.OData.Query.Container.NamedProperty1.ToDictionaryCore(Dictionary2 dictionary, IPropertyMapper propertyMapper, Boolean includeAutoSelected) at Microsoft.AspNetCore.OData.Query.Wrapper.SelectExpandWrapper.ToDictionary(Func3 mapperProvider)
at Microsoft.AspNetCore.OData.Query.Wrapper.SelectSomeConverter1.Write(Utf8JsonWriter writer, SelectSome1 value, JsonSerializerOptions options)`

Example project here - https://github.com/blueghostuk/odata-compute-issue

Is there anything I am doing wrong? Or is this not supported?

@xuzhg xuzhg self-assigned this May 15, 2023
@xuzhg xuzhg added the bug Something isn't working label May 15, 2023
@xuzhg
Copy link
Member

xuzhg commented May 15, 2023

For Microsoft.OData.ODataException: Could not find a property named '{{propertyName}}' on type '{{entityType}}'.

==> @mikepizzo ODL still needs the structural type to be 'open' type then we can use extra/dynamic properties on it. But, OData spec doesn't have such a limitation. Do you think it's time to change ODL behavior?

For System.InvalidOperationException: Sequence contains no matching element at System.Linq.ThrowHelper.ThrowNoMatchException() at Microsoft.AspNetCore.OData.Query.Container.JsonPropertyNameMapper.MapProperty(String propertyName)

==> @xuzhg This is an issue in non-odata scenarios containing dynamic properties. It's a bug and should fix it (@1)

As a workaround, customer can customize the 'IPropertyMapper' to skip it, but there's no way for customer to cusotmize it on System.Text.Json. So, we need to fix it. ( @2 )

@blueghostuk Before the fix, here's a workaround.

  1. Add 'public IDictionary<string, object> Data { get; set; }' into 'WeatherForecast' to let the model builder to build the type as open type. (This can kill the first error)

  2. We'd have to switch to use OData Newtonsoft.JSON.

2.a) Update the project as:

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.16" />
  <PackageReference Include="Microsoft.AspNetCore.OData" Version="8.1.2" />
  <PackageReference Include="Microsoft.AspNetCore.OData.NewtonsoftJson" Version="8.1.1" />
</ItemGroup>

a.b) Create the following class.

    public class MyPropertyMapper : IPropertyMapper
    {
        public string MapProperty(string propertyName)
        {
            return propertyName;
        }
    }
2.c) use the class in configuration as:
builder.Services.AddControllers() .AddOData().AddODataNewtonsoftJson((model, structuredType) =>
  {
      return new MyPropertyMapper();
  });

then it should work as:
image

@xuzhg
Copy link
Member

xuzhg commented May 17, 2023

@blueghostuk Please take a look the fix at: #928. Thanks.

@blueghostuk
Copy link
Author

Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants