Skip to content

Action returning a collection of entities with containment silently fails #2056

Closed

Description

@isabekyan

If an entity type has a containment EntityType<ProjectTask>().ContainsMany(pt => pt.ProjectTaskAssignments); then action returning an entity collection of this type would fail. It just stop transmitting data to client somewhere on serialization.

Assemblies affected

AspNetCore.OData 7.3.0
AspNET Core 3.1

Reproduce steps

Have entities

public class Project
{
  public Guid Id {get;set;}
  public virtual ICollection<ProjectTasks> ProjectTasks() {get;set;}
}

public class ProjectTask
{
  public Guid Id {get;set;}

  public Guid ProjectId {get;set;}
  
  public virtual ICollection<ProjectTaskAssignment> ProjectTaskAssignments {get;set;}
}

public class ProjectTaskAssignment
{
  public Guid Id {get;set;}

  public Guid ProjectTaskId{get;set;}
}

Some ODataController with action

public class ProjectsController : ODataController<Project>
{
  public async Task<IActionResult> UpdateProjectTasks([FromODataUri] Guid key,
    [FromBody] ODataActionParameters parameters)
  {
    var projectTasks = ((IEnumerable<ProjectTask>)parameters["projectTasks"]).ToList();
    var updatedProjectTasks = await _someService.UpdateProjectTasksAsync(key, projectTasks);
    return Ok(updatedProjectTasks);
  }
}

config smth like

    var projectsConfig = builder.EntityType<Project>();
    var taskUpdateAction = projectsConfig.Action(nameof(ProjectsController.UpdateProjectTasks));
    taskUpdateAction.CollectionEntityParameter<ProjectTask>("projectTasks");
    taskUpdateAction.ReturnsCollectionFromEntitySet<ProjectTask>(
      "ProjectTasks"));
    builder.EntityType<ProjectTask>().ContainsMany(pt => pt.ProjectTaskAssignments);

and some repository behind.

Expected result

Calling action returns actual tasks after update.

Actual result

Client logs:

* Received 1031 B chunk
* Received 190 B chunk
* transfer closed with outstanding read data remaining
* Closing connection 20

Server has an unhandled exception (outside of aspnet.core error handling in pipeline) that can be seen in events hub of studio:

Microsoft.OData.ODataException: The Path property in ODataMessageWriterSettings.ODataUri must be set when writing contained elements.
   at Microsoft.OData.ODataWriterCore.EnterScope(WriterState newState, ODataItem item)
   at Microsoft.OData.ODataWriterCore.WriteStartNestedResourceInfoImplementation(ODataNestedResourceInfo nestedResourceInfo)
   at Microsoft.OData.ODataWriterCore.WriteStart(ODataNestedResourceInfo nestedResourceInfo)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteNavigationLinks(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteResource(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteResourceSet(IEnumerable enumerable, IEdmTypeReference resourceSetType, ODataWriter writer, ODataSerializerContext writeContext)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)
   at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
   at Microsoft.AspNet.OData.Formatter.ODataOutputFormatterHelper.WriteToStream(Type type, Object value, IEdmModel model, ODataVersion version, Uri baseAddress, MediaTypeHeaderValue contentType, IWebApiUrlHelper internaUrlHelper, IWebApiRequestMessage internalRequest, IWebApiHeaders internalRequestHeaders, Func`2 getODataMessageWrapper, Func`2 getEdmTypeSerializer, Func`2 getODataPayloadSerializer, Func`1 getODataSerializerContext)
   at Microsoft.AspNet.OData.Formatter.ODataOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeResultAsync>g__Logged|21_0(ResourceInvoker invoker, IActionResult result)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)

Additional detail

Can supply info and check things if needed, but after I sleep )))

Activity

  1. gathogojr commented on Feb 18, 2020

    @gathogojr
    Contributor

    @isabekyan Is this only happening with ver. 7.3.0? Was it working with a previous version?

  2. isabekyan commented on Feb 18, 2020

    @isabekyan
    Author

    @gathogojr I don't know.. we are migrating from asp.net classic Microsoft.AspNet.OData 5.9.0 and we did not use containment there.
    As a temporary workaround we replace containment config (ContainsMany) to a simple navigation (HasMany) and add own EntitySet for a contained entity. This configuration works OK but this EntitySets are actually redundant.

  3. xuzhg commented on Feb 18, 2020

    @xuzhg
    Contributor

    I tested this scenario in 7.2.2, it throws the same error message.

    In Web API, we had the following setting for "Path"

    writerSettings.ODataUri = new ODataUri
                {
                    ServiceRoot = baseAddress,
    
                    // TODO: 1604 Convert webapi.odata's ODataPath to ODL's ODataPath, or use ODL's ODataPath.
                    SelectAndExpand = internalRequest.Context.ProcessedSelectExpandClause,
                    Apply = internalRequest.Context.ApplyClause,
                    Path = (path == null || IsOperationPath(path)) ? null : path.Path,
                };

    "IsOperationPath" returns "true" because it's an action call. So, we set the "Path" as null.

    But, In the OData Core, it complains about "Path" is equals to "null" for "Containment Navigation property" at https://github.com/OData/odata.net/blob/18abaa5908f8246cb63f45587948d10eb65d1e73/src/Microsoft.OData.Core/ODataWriterCore.cs#L2355-L2362

    I'd like get more input for this scenario from @mikepizzo

  4. added a commit that references this issue on Feb 19, 2020
    9b34d4b
  5. added a commit that references this issue on Feb 28, 2020
    69420bb
  6. added this to the 7.4 milestone on Mar 10, 2020
  7. added a commit that references this issue on Mar 19, 2020
    dfe29a3
  8. added a commit that references this issue on Apr 13, 2020
    a96ee2c
  9. modified the milestones: 7.4, 7.5 on May 6, 2020
  10. added a commit that references this issue on Apr 12, 2024
    c79634a
  11. added a commit that references this issue on May 29, 2026
    01ca0b6
  12. added a commit that references this issue on May 29, 2026
    e5f50f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions