OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
856 stars 473 forks source link

$expand not working in derived class #1687

Open keatkeat87 opened 5 years ago

keatkeat87 commented 5 years ago

Short summary (3-5 sentences) describing the issue. $expand not working in derived class

Assemblies affected

asp.net core 2.1.1 odata 7.1.0

Reproduce steps

git clone https://github.com/keatkeat87/aspnet-core-odata-delived-class-expand-issue.git dotnet restore dotnet ef migrations add Init dotnet ef database update (note: it will create demo database ODataDelivedExpandIssue in mssqllocaldb) F5 start run application visit url : http://localhost:57987/odata/admins (note: port maybe different) it will get the right result, but when i add $expand http://localhost:57987/odata/admins?$expand=User this will fail and response nothing.

Optional, details of the root cause if known. Delete this section if you have no additional details to add. similar issue https://github.com/OData/WebApi/issues/1530

Some code here : Model.cs ` public class AccountContext : DbContext { public AccountContext(DbContextOptions options) : base(options) { } public DbSet Users { get; set; } public DbSet Characters { get; set; } public DbSet Admins { get; set; } public DbSet Members { get; set; } }

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public ICollection<Character> Characters { get; set; }
}

public abstract class Character
{
    public int Id { get; set; }

    [ForeignKey(nameof(User))]
    public int UserId { get; set; }
    public User User { get; set; }
}

public class Admin : Character
{
    public string Position { get; set; }
}

public class Member : Character
{
    public int VipLevel { get; set; }
}`

Startup.cs `private static IEdmModel GetEdmModel() { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet< User >("Users"); builder.EntitySet< Character >("Characters"); builder.EntitySet< Admin >("Admins"); builder.EntitySet< Member >("Members"); return builder.GetEdmModel(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}");

    routes.Expand();
    routes.MapODataServiceRoute("odata", "odata", GetEdmModel());
});

}`

Controller : `public class AdminsController : ODataController { private AccountContext Db; public AdminsController( AccountContext db ) { Db = db; }

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Select | AllowedQueryOptions.Format | AllowedQueryOptions.Count | AllowedQueryOptions.Filter | AllowedQueryOptions.OrderBy | AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.Expand, MaxExpansionDepth = 3)]
public IActionResult Get()
{
    return Ok(Db.Admins); // this not work!
    //return Ok(Db.Characters); // this will work!
}

}`

if you need anything more to make sure this is a bug or whatever please let me know.

biaol-odata commented 5 years ago

@keatkeat87 I think the expanded item "User" needs to be prefixed with a qualified type name "Character" on which the selected item is defined. It also explains why OK(Db.Characters) works since User is defined on Character type. Can you please provide additional information so that the issue can be examined further? I got the following error after git clone.

c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue>dotnet restore
  Restoring packages for c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\ODataDelivedExpandIssue.csproj...
c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\ODataDelivedExpandIssue.csproj : warning NU1608: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.0 requires Microsoft.AspNetCore.Razor
.Design (= 2.1.0) but version Microsoft.AspNetCore.Razor.Design 2.1.2 was resolved. [c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue.sln]
c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\ODataDelivedExpandIssue.csproj : warning NU1608: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.0 requires Microsoft.EntityFrameworkC
ore.InMemory (= 2.1.0) but version Microsoft.EntityFrameworkCore.InMemory 2.1.4 was resolved. [c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue.sln]
c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\ODataDelivedExpandIssue.csproj : error NU1107: Version conflict detected for Microsoft.AspNetCore.Razor.Language. Reference the package directly from the project to resolve t
his issue.  [c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue.sln]
c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\ODataDelivedExpandIssue.csproj : error NU1107:  ODataDelivedExpandIssue -> Microsoft.VisualStudio.Web.CodeGeneration.Design 2.1.1 -> Microsoft.VisualStudio.Web.CodeGenerators
.Mvc 2.1.1 -> Microsoft.VisualStudio.Web.CodeGeneration 2.1.1 -> Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore 2.1.1 -> Microsoft.VisualStudio.Web.CodeGeneration.Core 2.1.1 -> Microsoft.VisualStudio.Web.CodeGeneration.Templating 2.1.1 -> Micros
oft.AspNetCore.Razor.Language (>= 2.1.1)  [c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue.sln]
c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\ODataDelivedExpandIssue.csproj : error NU1107:  ODataDelivedExpandIssue -> Microsoft.AspNetCore.App 2.1.0 -> Microsoft.AspNetCore.Razor.Language (= 2.1.0). [c:\github\myTemp\
aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue.sln]
  Generating MSBuild file c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\obj\ODataDelivedExpandIssue.csproj.nuget.g.props.
  Restore failed in 721.15 ms for c:\github\myTemp\aspnet-core-odata-delived-class-expand-issue\ODataDelivedExpandIssue\ODataDelivedExpandIssue.csproj.
keatkeat87 commented 5 years ago

hi Bill,

for the dotnet restore error, i don't know what is this, so sorry for that. but i just commit all the dependencies, please try git clone again then skip dotnet restore part. hope it can work.

as you said, the User need to add prefix type. so i try do this, but still not working. http://localhost:62710/odata/admins?$expand=ODataDelivedExpandIssue.Models.Character/User

Model.cs Startup.cs Controller.ts

keatkeat87 commented 5 years ago

hi @biaol-odata , is this something further updated? have you successfully run my environment?