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

GetQuery fails on DataMemberAttrubute-marked field with $orderby #174

Open
Galgvithr opened this issue Jan 31, 2023 · 1 comment
Open

GetQuery fails on DataMemberAttrubute-marked field with $orderby #174

Galgvithr opened this issue Jan 31, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@Galgvithr
Copy link

Galgvithr commented Jan 31, 2023

Hello!
I've encountered a problem using AutoMapper.AspNetCore.OData.EFCore extension. $orderby call seems to ignore DataMemberAttribute's name of the property.

Source/destination types

// EF model
 [Table("models")]
public class MyEntityModel
{
[Column("id")]
public int Id { get; set; }

[Column("name")]
public string? Name { get; set; }
}

// view model
[DataContract]
public class MyDtoModel
{
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "my_name")] 
    public string? Name { get; set; }
}

Mapping configuration

// default mapping between EF model and view model
CreateMap<MyEntityModel, MyDtoModel>();

Versions:

AutoMapper 12.0.1
AutoMapper.AspNetCore.OData.EFCore 4.0.0
Microsoft.AspNetCore.OData 8.0.12
Microsoft.EntityFrameworkCore.InMemory 7.0.2 OR Npgsql.EntityFrameworkCore.PostgreSQL 6.0.7 (EF version seems to be irrelevant for this bug)

Steps to reproduce

  1. Create ASP.NET project configured for default OData usage with following controller method:
[HttpGet]
public async Task<IActionResult> Get(ODataQueryOptions<MyDtoModel> odata)
{
   var query = _context.Entities;

   return Ok(query.GetQuery(_mapper, odata));
}
  1. Use in-memory EF context, populate DbSet with one or more instances of MyEntityModel
public TestContext(DbContextOptions<TestContext> op): base(op)
    {
        CreateRecords();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyEntityModel>().HasKey(p => p.Id);
    }

    public DbSet<MyEntityModel> Entities { get; set; }

    private void CreateRecords()
    {
        {
            if (Entities?.Any() ?? false)
            {
                return;  
            }
            Entities.AddRange(
                new MyEntityModel()
                {
                    Id = 1,
                    Name = "1"
                });

            SaveChanges();
        }
    }
  1. Send OData-request with query:
    ?$orderby=my_name+asc

Expected behavior

Request finishes: rows are extracted from DB as mapped DTOs. Collection of objects serialises, just like the similar query:
?$orderby=id+asc
Which results in this response body:

[
    {
        "id": 1,
        "name": "1"
    }
]

Actual behavior

Request results in status code 500 Internal Server Error with argument exception:

System.ArgumentException: Member my_name, does not exists in type ODataEfCore_DataMember_Test.Controllers.MyDtoModel.
at LogicBuilder.Expressions.Utils.TypeExtensions.GetMemberInfo(Type parentType, String memberName)
at LogicBuilder.Expressions.Utils.TypeExtensions.GetMemberInfoFromFullName(Type type, String propertyFullName)
at AutoMapper.AspNet.OData.LinqExtensions.GetOrderByCall(Expression expression, String memberFullName, String methodName, String selectorParameterName)
at AutoMapper.AspNet.OData.LinqExtensions.g__GetMethodCall|10_0(<>c__DisplayClass10_0& )
at AutoMapper.AspNet.OData.LinqExtensions.GetOrderByCall(Expression expression, OrderByClause orderByClause, ODataQueryContext context)
at AutoMapper.AspNet.OData.LinqExtensions.GetQueryableMethod(Expression expression, ODataQueryContext context, OrderByClause orderByClause, Type type, Nullable1 skip, Nullable1 top)
at AutoMapper.AspNet.OData.LinqExtensions.GetOrderByMethod[T](Expression expression, ODataQueryOptions1 options, ODataSettings oDataSettings) at AutoMapper.AspNet.OData.LinqExtensions.GetQueryableExpression[T](ODataQueryOptions1 options, ODataSettings oDataSettings)

$filter seems to not be affected by this bug. I've tried the query: $filter=my_name eq '1'
So, i presume, $orderby should also work with DataMemberAttrubite.

@BlaiseD
Copy link
Member

BlaiseD commented Feb 1, 2023

The root filter uses ApplyTo from Microsoft.AspNetCore.OData.Query. You'll find the nested filter also ignores the DataMemberAttribute - it isn't implemented at all. Ok to create PR.

@BlaiseD BlaiseD added the enhancement New feature or request label Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants