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

Not serialize expanded property for function #1204

Open genusP opened 6 years ago

genusP commented 6 years ago

I request this uri https://localhost:44346/api/Tasks/Satellite.AWT.Assigned?%24top=20&%24expand=ActualStage(%24select%3DStageId)&%24count=true

Executed expression contains Expand logic. In Respose No property ActualStage.

// https://localhost:44346/api/Tasks/Satellite.AWT.Assigned?%24top=20&%24expand=ActualStage(%24select%3DStageId)&%24count=true

{
  "@odata.context": "https://localhost:44346/api/$metadata#Collection(Satellite.AWT.Models.Task)",
  "@odata.count": 14,
  "value": [
    {
      "Id": 1,
      "DateBegin": "2017-11-23T14:32:40.577+03:00",
      "DateEnd": null,
      "DirectionId": "c5c0a778-694e-49d1-b1a0-f8ef5569c673",
      "Text": "Подготовить документы",
      "TargetName": "bda.Requests",
      "TargetId": 1,
      "ParentId": null
    },
......
  ]
}
genusP commented 6 years ago

Workaround, call ReturnsFromEntitySet when register function in EdmModel

robward-ms commented 6 years ago

@genusP - Can you provide a sample configuration, model, and controller?

genusP commented 6 years ago
//Build EDM 
builder.EntityType<Task>().Collection.Function(nameof(Controllers.TasksController.Assigned));
assignedTaskFunc.ReturnsCollection<Task>();
assignedTaskFunc.Namespace = pluginNS;

//Entity
public partial class Task : ITargetAware
{
    public int       Id          { get; set; } 
    public DateTime  DateBegin   { get; set; } 
    public DateTime? DateEnd     { get; set; } 
    public Guid      DirectionId { get; set; } 
    public string    Text        { get; set; } 
    public string    TargetName  { get; set; } 
    public int       TargetId    { get; set; } 
    public int?      ParentId    { get; set; }

    public IEnumerable<Task> ChildTasks { get; set; }
    public Task ParentTask { get; set; }
    public IEnumerable<TaskStage> TaskStages { get; set; }
    public IEnumerable<WorkReport> WorkReports { get; set; }
    public IEnumerable<TaskStage> ActualStage { get; set; }
}

public partial class TaskStage
{
    public int      Id           { get; set; } 
    public int      TaskId       { get; set; }
    public int      StageId      { get; set; }
    public DateTime SwitchDate   { get; set; } 
    public bool     Actual       { get; set; } 
    public int?     WorkReportId { get; set; } 
    public int?     UserId       { get; set; }
}

//Controller
public class TasksController:ControllerBase
    {
        [EnableQuery(HandleNullPropagation = Microsoft.AspNet.OData.Query.HandleNullPropagationOption.False)]
        public IActionResult Assigned([FromServices] Services.ITasksStore store)
        {
            var employeeId = HttpContext.GetEmployee(e => e.Id);
            var res = store.AssignedTasks(employeeId);
            return new ObjectResult(res);
        }
    }
robward-ms commented 6 years ago

@genusP - Have you tried this with an earlier version of WebApi OData? I traced the code and repo'ed the issue but I'm not sure if it's a NetCore issue. Just trying to classify it properly.

AlanWong-MS commented 6 years ago

@genusP we're currently reviewing open issues in preparation for our upcoming WebAPI 7.x release. Per Rob's comment, just so we can trace the problem down better, do you know if it's a problem existent in a WebAPI 6.x?

genusP commented 6 years ago

This issue exits on 6.x. I don't understand, why properties expand when build expression tree and not serialize to response. If this query option not support need error else serialize data.

AlanWong-MS commented 6 years ago

Thanks for the feedback and the issue description. We will take this into consideration in further investigation.

genusP commented 6 years ago

If for EntityType not register EntitySet expand not work. For request

https://localhost:44346/api/Requests?$expand=CurrentTaskAssignment($expand=Assignment($expand=Employee($select=ShortFIO);$select=Employee);$select=Assignment)

If entity CurrentTaskAssignment not register as EntitySet property Assignment not expand else work fine. Data selected from DB contains values for this properties.