Skip to content

Wrong property navigation action is selected in ODATA controllers #336

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

Closed
GennadyGS opened this issue Aug 29, 2018 · 4 comments · Fixed by #355
Closed

Wrong property navigation action is selected in ODATA controllers #336

GennadyGS opened this issue Aug 29, 2018 · 4 comments · Fixed by #355

Comments

@GennadyGS
Copy link

When there are multiple navigation actions in ODATA controller, first one (based on method order in class) is always executed regardless of requested navigation property.

Example (can be verified in ODataBasicSample):

    [ODataRoutePrefix( "People" )]
    public class People0Controller : ODataController
    {
        [ODataRoute( "({id})/FirstName" )]
        public IActionResult GetFirstName( [FromODataUri] string id, ODataQueryOptions<Person> options ) =>
            Ok( new { FirstName = $"{id}-first-name" } );

        [ODataRoute( "({id})/LastName" )]
        public IActionResult GetLastName( [FromODataUri] string id, ODataQueryOptions<Person> options ) =>
            Ok( new { LastName = $"{id}-lastName" } );
   }

Request:
http://localhost:1238/api/People(1)/LastName?api-version=1.0

Actual response:

{
    "firstName": "1-first-name"
}

Expected response:

{
    "lastName": "1-last-name"
}
@commonsensesoftware
Copy link
Collaborator

Thanks for reporting it. I'll have a look.

@fusionshen
Copy link

The same to me.
If you have two [HttpGet] actions no matters what [ODataRoute("****")](in a same pattern) is, the route will match the first one.
Like this:
[HttpGet("IsAuthenticated"), HttpPost("IsAuthenticated")]
[ODataRoute("IsAuthenticated")]
public bool IsAuthenticated(){...} <=======Actual response.

[HttpGet("GetUserInfo"), HttpPost("GetUserInfo")]
[ODataRoute("GetUserInfo")]
public IActionResult GetUserInfo(){...}<=======Expected response.

@mmckenz
Copy link

mmckenz commented Sep 12, 2018

I am witnessing a similar issue with a single controller offering multiple collections.

public class SampleODataController : ODataController
{
    [EnableQuery]
    [ODataRoute("Foos")]
    public IQueryable<Foo> GetFoos()
    {
        return new List<Foo>().AsQueryable();
    }

    [EnableQuery]
    [ODataRoute("Bars")]
    public IQueryable<Bar> GetBars()
    {
        return new List<Bar>().AsQueryable();
    }
}
...
public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc();
    services.AddOData();
    ...
}
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
{
    ...
    app.UseOData(MyEdmModel.GetEdmModel());
    ...
}

The above works using the standard ASP.NET Core OData config. I can hit /odata/Foos or /odata/Bars and the appropriate controller method is called as expected.

When configured to use ASP.NET Core OData versioning as follows, the first method of the controller is hit for both /v1/Foos and /v1/Bars.

[ApiVersion("1.0")]
public class SampleODataController : ODataController
{
    [EnableQuery]
    [ODataRoute("Foos")]
    public IQueryable<Foo> GetFoos()
    {
        return new List<Foo>().AsQueryable();
    }

    [EnableQuery]
    [ODataRoute("Bars")]
    public IQueryable<Bar> GetBars()
    {
        return new List<Bar>().AsQueryable();
    }
}
...
public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc();
    services.AddApiVersioning();
    services.AddOData().EnableApiVersioning();
    ...
}
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime, VersionedODataModelBuilder modelBuilder)
{
    ...
    app.UseMvc(routes =>
    {
        routes.MapVersionedODataRoutes(
            ODataApplicationBuilderExtensions.DefaultRouteName,
            "v{version:apiVersion}",
            modelBuilder.GetEdmModels());
    });
    ...
}

It seems the ODataRoute attribute is ignored with the versioned implementation.

@GennadyGS
Copy link
Author

GennadyGS commented Oct 8, 2018

Thanks a lot for fixing.
When is it going to be published to nuget?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants