AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
140 stars 38 forks source link

OData $select query not working #147

Closed Schinwinkwinsky closed 2 years ago

Schinwinkwinsky commented 2 years ago

Hello, everybody

Working with this extension I have noticed that OData $select query is not working with it. I have tested with: $top, $skip, $select, $expand, $filter and $orderBy. All kind of queries are working great, but $select.

image

BlaiseD commented 2 years ago

There are tests here using $select - maybe compare those with your configuration.

Schinwinkwinsky commented 2 years ago

I could see some differences. I set up my project using OData documentation. And it's like that:

using MediatR;
using Microsoft.AspNetCore.OData;
using Microsoft.EntityFrameworkCore;
using School.Data;
using School.Domain;
using School.WebAPI.Other;
using System.Reflection;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers()
    .AddOData()
    .AddJsonOptions(options => options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);

But on your tests, OData is added on an IMvcCoreBuilder.

private void Initialize()
{
    IServiceCollection services = new ServiceCollection();
    IMvcCoreBuilder builder = new TestMvcCoreBuilder
    {
        Services = services
    };

    builder.AddOData();

I noticed that in your tests, you use OData v8.0.6. So I reverted to that version in my project and the result was the same. If you could test on my project, it's available on https://github.com/Schinwinkwinsky/School. I'd really appreciate your help.

BlaiseD commented 2 years ago

It's probably better if you reproduce your failing case using the tests from this library - there are web tests calling controllers as well as tests creating the EDM model without the controller.

You're probably just missing an explicit expansion in your configuration e.g. compare the profiles here to yours.

Schinwinkwinsky commented 2 years ago

Ok, I think I got it. I set up profile as your tests:

Before: image

After: image

When we query with $select, the properties not selected come with null or default value. image

I think that's the reason all tests are ok. I don't know if it's a normal behavior, but when I use $select without AutoMapper, the properties not selected just don't come. It was not the result I expected. I'm not sure, but I thought ExplicitExpansion is to use with $expand query. Sorry, I'm new with OData and AutoMapper. Thank you for your help.

BlaiseD commented 2 years ago

Yep - not sure what the difference is with other libraries. We project to the TModel and return it.

image

PRs are welcome for any improvement.

BlaiseD commented 2 years ago

With regard to explicit expansion, literal types are expanded by default unless a $select is specified. All members are eligible for expansion - not just navigation properties.

Schinwinkwinsky commented 2 years ago

Thank you very much! I'm gonna study more and try to submit a PR.