OData / WebApi

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

EnableQuery $select/$expand is ignored when returning CreatedODataResult<T> #2295

Open UmairB opened 4 years ago

UmairB commented 4 years ago

When posting to create a new entity, and returning CreateODataResult, $select/$expand options via EnableQuery in the query string are ignored and not applied. The issue does not happen and query options are successfully applied if instead the standard Microsoft.AspNetCore.Mvc.CreatedResult is returned.

Assemblies affected

Microsoft.AspNetCore.OData 7.5.0 Microsoft.AspNetCore.OData 7.4.1

Reproduce steps

Create any simple odata model with atleast one entity with a primary key. E.g.

public class Store
{
    public int Id { get; set; }

    public string Name { get; set; }
}

and create the following mvc action

[HttpPost]
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Select)]
public IActionResult Post([FromBody]Store store)
{
        var result = Repository.AddStore(store);
        return new CreatedODataResult<Store>(store);
}

The following request

POST /stores?$select=id
{
    name: "Test
}

returns the full entity, rather than only the id property.

The following works as expected

[HttpPost]
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Select)]
public IActionResult Post([FromBody]Store store)
{
        var result = Repository.AddStore(store);
        return this.Created($"/stores({result.Id})", result);
}

and only the id property is returned.

Expected result

Query options should be applied and only the selected and/or expanded properties should be returned.

Actual result

Query options are ignored and the full entity is returned.

xuzhg commented 4 years ago

@UmairB Did you try the previous version? I'd like to see whether it's a regression or not?

UmairB commented 4 years ago

@UmairB Did you try the previous version? I'd like to see whether it's a regression or not?

Yep problem repoduces on version 7.4.1 of package

DimaMegaMan commented 1 year ago

@xuzhg Still reproducible, any plans for this? Seems like not a big deal right 😅?

icnocop commented 1 year ago

This seems to have been fixed in ASP.NET Core OData 8.2.0 and ASP.NET Web API OData 7.7.0. See https://github.com/OData/AspNetCoreOData/issues/434

icnocop commented 1 year ago

I've confirmed this is fixed in Microsoft.AspNetCore.OData 7.7.1.

Remember to add the key "Prefer" with value "return=representation" to the request header.