WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

[Bug] Functions returning entity types with contained properties throw exception

Open vojtechvit opened this issue 11 years ago • 20 comments

Description

Functions that return entity types with properties that are contained entity sets throw exception when invoked.

Minimal repro steps

  • Create an empty ASP.NET website hosted on OWIN SystemWeb
  • Create an EmailAddress class with property [Key] Address : string
  • Create a User class with properties Id : int and [Contained] EmailAddresses : ICollection<EmailAddress>
  • Add Web API OData, register it with a model that contains:
    • Entity set Users of type User
    • Function ByAddress defined on a collection of User with a parameter address : string, returning a single entity from entity set Users
  • Create a controller and action for function ByEmailAddress and some sample implementation returning some actual data (non-empty result!).
  • Try to call the function endpoint ByEmailAddress with parameter address set to an existing e-mail address.

Expected result

A single entity of type User.

Actual result

An exception:

{
    "error": {
        "code": "",
        "message": "An error has occurred.",
        "innererror": {
            "message": "The Path property in ODataMessageWriterSettings.ODataUri must be set when writing contained elements.",
            "type": "Microsoft.OData.Core.ODataException",
            "stacktrace": " at Microsoft.OData.Core.ODataWriterCore.EnterScope(WriterState newState, ODataItem item)\ \ at Microsoft.OData.Core.ODataWriterCore.WriteStartNavigationLinkImplementation(ODataNavigationLink navigationLink)\ \ at Microsoft.OData.Core.ODataWriterCore.WriteStart(ODataNavigationLink navigationLink)\ \ at System.Web.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteNavigationLinks(IEnumerable`1 navigationProperties, EntityInstanceContext entityInstanceContext, ODataWriter writer)\ \ at System.Web.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteEntry(Object graph, ODataWriter writer, ODataSerializerContext writeContext)\ \ at System.Web.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)\ \ at System.Web.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\ \ at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\ \ at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\ \ --- End of stack trace from previous location where exception was thrown ---\ \ at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\ \ at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\ \ at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()"
        }
    }
}

Further technical details

Sample code

var modelBuilder = new ODataConventionModelBuilder();

var userType = modelBuilder.EntityType<User>();
var byEmailAddressFunction = userType.Collection.Function("byEmailAddress");
byEmailAddressFunction.Parameter<string>("address");
byEmailAddressFunction.ReturnsFromEntitySet<User>("Users");

modelBuilder.EntitySet<User>("Users");


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

    [Contained]
    public ICollection<EmailAddress> EmailAddresses { get; set; }
}


public class EmailAddress
{
    [Key]
    public string Address { get; set; }
}


    [HttpGet]
    public IHttpActionResult ByEmailAddress(string address)
    {
        if (address == null)
        {
            return BadRequest();
        }

        var user = _users.FirstOrDefault(u => 
            u.EmailAddresses.Any(a =>
                a.Address.Equals(address, StringComparison.InvariantCulture)));

        if (user == null)
        {
            return NotFound();
        }

        return Ok(user);
    }

vojtechvit avatar Mar 20 '15 16:03 vojtechvit

I created a fork and a branch with a fix to this issue: https://github.com/vojtechvit/odata.net/commits/bugfix/containedentityinfunction

However - I'm not fully aware of the internal mechanics of the complex ODataWriterCore class, so please consider this code just as a highlight of the problem area!

For instance, I'm not persuaded that it generates all correct path segments (e.g. entity key segments are missing).

vojtechvit avatar Mar 22 '15 17:03 vojtechvit

It would be great if you could include the bugfix already in the 5.5 release! O:-)

vojtechvit avatar Mar 22 '15 22:03 vojtechvit

Is there an ETA for this fix?

jogonzal avatar Apr 15 '15 05:04 jogonzal

ODataMediaTypeFormatter has codes as:

writerSettings.ODataUri = new ODataUri
 {
       ServiceRoot = baseAddress,

        // TODO: 1604 Convert webapi.odata's ODataPath to ODL's ODataPath, or use ODL's ODataPath.
       SelectAndExpand = Request.ODataProperties().SelectExpandClause,
       Path = (path == null || IsOperationPath(path)) ? null : path.ODLPath,
 };

Where IsOperationPath(path) returns true because it's a function call, therefore Path in the writerSettings.ODataUri is null.

While, in ODL, ODataWriterCore need Path in the writerSettings.ODataUri to be set, otherwise it will throw the ODataException. However, it's hard to calculate the path for the returned entity of a function.

Therefore, It's better to set the path by yourself (the developer) in the controller. Below's the workaround and it can work at my side:

[HttpGet]
public IHttpActionResult ByEmailAddress(string address)
 {
            if (address == null)
            {
                return BadRequest();
            }

            var user = _users.FirstOrDefault(u =>
                u.EmailAddresses.Any(a =>
                    a.Address.Equals(address, StringComparison.InvariantCulture)));

            if (user == null)
            {
                return NotFound();
            }

            IEdmModel model = Request.ODataProperties().Model;

            ODataPath path = new DefaultODataPathHandler().Parse(model, "http://localhost:44567/odata/", "Users(1)");

            Request.ODataProperties().Path = path;

            return Ok(user);
        }

Please replace the service root and odataPath parameter values as your own string.

Please re-open it if you have further concern.

xuzhg avatar Apr 29 '15 08:04 xuzhg

Using the new libraries would require something like:

var path = new DefaultODataPathHandler().Parse("http://localhost:44567/odata/v1", "MyEntityRoute", Request.GetRequestContainer());
           
Request.ODataProperties().Path = path;

patrickmichalina avatar Jan 05 '17 22:01 patrickmichalina

+1 for a fix for this.

I run into this issue whenever I try to return a newly created entity set record from an unbound action.

noah-freitas avatar Jul 31 '17 23:07 noah-freitas

+1. Is there an ETA for a fix? This issue is repro'ing for me despite the Path being set in Request.ODataProperties() with the correct value.

mttwc avatar Aug 30 '17 22:08 mttwc

+1. is there any ETA for this fix?

pareman avatar May 02 '18 19:05 pareman

Thanks for the workaround.

Setting Request.ODataProperties().Path worked for me.

+1 for fixing the issue or providing some documentation so engineers are aware of this behavior.

balent avatar Jul 31 '18 19:07 balent

I am also failing into this issue :( @xuzhg, can I ask your attention please.

@vojtechvit created a pull request for fixing this issue in odata.net repo https://github.com/OData/odata.net/pull/111

I don't know why this pull request is not merged and just closed, I saw the code and it looks good for me. Can you spent some time on this issue? This issue is still actual in Microsoft.AspNetCore.OData 7.0.1.

maximpashuk avatar Aug 08 '18 09:08 maximpashuk

Im also facing this issue

jannikbuschke avatar Aug 11 '18 22:08 jannikbuschke

also still facing this bug on Microsoft.AspNetCore.OData 7.0.1.

nathan-swannet avatar Sep 05 '18 10:09 nathan-swannet

But still workaround works for me in Microsoft.AspNetCore.OData 7.0.1

var path = Request.GetPathHandler().Parse("http://localhost/odata/", "users(1)", request.GetRequestContainer());
Request.ODataFeature().Path = path;

maximpashuk avatar Sep 05 '18 10:09 maximpashuk

Also facing this one here!

Mortana89 avatar Mar 01 '19 15:03 Mortana89

also facing this!

emturner avatar Aug 02 '19 09:08 emturner

Whow. 3 years open and still not fixed. Meanwhile anyone has a decent solution for dotnet core? I just added a contained entity set and boom - "unrelated" functionality blowing.

NetTecture avatar Sep 12 '19 21:09 NetTecture

Request.ODataProperties().Path does not seem to be available in Microsoft.AspNetCore.OData. Could you please advise?

cfauchere avatar Jan 09 '20 22:01 cfauchere

As above, the workaround for .net core is:

var path = Request.GetPathHandler().Parse("http://localhost/odata/", "users(1)", request.GetRequestContainer());
Request.ODataFeature().Path = path;

crackalak avatar Jan 09 '20 23:01 crackalak

Running now also into this. A higher priorisation of this bug would be greatly appreciated.

@crackalak does your workaround still work for you?

My code looks like this, but fails:

 public class ClientsController : ODataController
    {
        private readonly DataContext ctx;

        public ClientsController(
            DataContext ctx
        )
        {
            this.ctx = ctx;
        }

        [EnableQuery]
        public IQueryable<Client> Get(string search)
        {
            return ctx.Clients;
        }

        [EnableQuery]
        public SingleResult<Client> Get([FromODataUri] Guid key)
        {
            IQueryable<Client> result = ctx.Clients.Where(p => p.Id == key);
            return SingleResult.Create(result);
        }

        [EnableQuery]
        [HttpGet]
        public IEnumerable<Client> TestData()
        {
            var path = Request.GetPathHandler().Parse("https://localhost:5001/odata/", "clients/TestData", Request.GetRequestContainer());
            Request.ODataFeature().Path = path;
            return new FakeClients().Get();
        }
    }

jannikbuschke avatar May 09 '20 12:05 jannikbuschke

FYI for anyone coming across this in late 2020, looks like this has been fixed in WebAPI 7.5.0 (https://docs.microsoft.com/en-us/odata/changelog/webapi-7x) by PR 2060

lclamotte avatar Sep 21 '20 14:09 lclamotte