ODATA:未绑定函数返回带有集合的复杂类型

ODATA: unbound function returning a complex type with a collection

我一直在尝试用 odata 和 ASP.NET Core 3 实现一些东西,但我不想正常工作,我似乎无法弄清楚哪里出了问题。我创建了一个小示例应用程序来演示。

我有一个 odata 服务可以用来查询 nodes。节点可以是 type1type2 节点,这些是开放类型,具有动态属性。查询它们非常有效。 我想要做的是计算节点之间的路径。路径不是实体——它们没有身份。所以我认为为此创建资源是不正确的。它们只是路径计算的结果,包含沿路径的节点列表,所以我认为函数是告诉 API 我想要什么的更好方法。

所以我创建了一个 odata 函数来进行计算和 return 可用路径,它工作正常,除了我无法将它获取到 return 路径遍历的节点列表,这是我真正需要的信息。

我创建了一些示例代码来演示该问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace OdataSample
{
    public static class Program {
        public static void Main(string[] args) {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

    public class Startup {
        public void ConfigureServices(IServiceCollection services) {
            services.AddOData();
            services.AddSingleton<IDataProvider, DataProvider>();
            services.AddMvc(options => options.EnableEndpointRouting = false);
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
            app.UseDeveloperExceptionPage();

            var builder = new ODataConventionModelBuilder(app.ApplicationServices);
            builder.EntitySet<Node>("Nodes");
            builder.ComplexType<Path>()
                .HasMany(x => x.Nodes)
                .HasDerivedTypeConstraints(typeof(Type1Node), typeof(Type2Node));

            var calculatePath = builder.Function("CalculatePaths");
            calculatePath.Parameter<string>("source");
            calculatePath.Parameter<string>("target");
            calculatePath.ReturnsCollection<Path>();
            
            app.UseMvc(routeBuilder =>
            {
                routeBuilder.EnableDependencyInjection();
                routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(10).Count();
                routeBuilder.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());
            });
        }
    }

    public abstract class Node {
        public string Id { get; set; }
        public string Kind { get; set; }
        public IDictionary<string, object> CustomProperties { get; set; }
    }

    public sealed class Type1Node : Node {
    }

    public sealed class Type2Node : Node {
        public string Source { get; set; }
        public string Target { get; set; }
    }

    public sealed class Path {
        public string SourceId { get; set; }
        public string TargetId { get; set; }
        public List<Node> Nodes { get; set; }
    }

    public interface IDataProvider {
        Task<IEnumerable<Node>> GetNodes();
        Task<IEnumerable<Path>> GetPaths(string source, string target);
    }

    public sealed class DataProvider : IDataProvider {
        private static readonly IList<Node> Nodes = new List<Node> {
            new Type1Node{Id = "first", Kind="type1-kind1", CustomProperties = new Dictionary<string, object>()},
            new Type1Node{Id = "second", Kind = "type1-kind2", CustomProperties = new Dictionary<string, object>{{"foo", "bar"}}},
            new Type2Node{Id = "third", Kind="type2-kind1", Source = "first", Target = "second"},
            new Type2Node{Id = "fourth", Kind="type2-kind1", Source = "first", Target = "second", CustomProperties = new Dictionary<string, object>{{"red", "blue"}}}
        };

        public async Task<IEnumerable<Node>> GetNodes() {
            await Task.Yield();
            return Nodes.ToList();
        }

        public async Task<IEnumerable<Path>> GetPaths(string source, string target) {
            await Task.Yield();
            return new List<Path> {
                new Path { SourceId = source, TargetId = target, Nodes = new List<Node> {Nodes[0], Nodes[2], Nodes[1]}},
                new Path { SourceId = source, TargetId = target, Nodes = new List<Node> {Nodes[0], Nodes[3], Nodes[1]}}};
        }
    }

    public class NodesController : ODataController {
        private readonly IDataProvider dataProvider;
        public NodesController(IDataProvider dataProvider) => this.dataProvider = dataProvider;
        [EnableQuery]
        public async Task<List<Node>> Get() => (await dataProvider.GetNodes()).ToList();
    }

    public class PathsController : ODataController {
        private readonly IDataProvider dataProvider;
        public PathsController(IDataProvider dataProvider) => this.dataProvider = dataProvider;
        [EnableQuery]
        [HttpGet]
        [ODataRoute("CalculatePaths")]
        public async Task<List<Path>> Get(string source, string target) =>
            (await dataProvider.GetPaths(source, target)).ToList();
    }
}

抱歉丑陋,我尽量压缩它。

现在 http://host:port/odata/CalculatePaths?source=A&target=B 应该 return 2 条路径,确实如此。但是只有两个字符串属性在那里,集合 属性 不是:

GET host:port/odata/CalculatePaths?source=A&target=B 会 return: {"@odata.context":"http://host:port/odata/$metadata#Collection(OdataSample.Path)","value":[{"SourceId":"A","TargetId":"B"},{"SourceId":"A","TargetId":"B"}]}

我试过很多不同的方式来摆弄它,但并不开心。我唯一一次接近我想要的是当我将路径更改为只有节点 ID(字符串)而不是节点时。但这并不理想,因为我需要查询各个节点,即使我已经拥有所有需要的信息。

我应该更改什么以便在响应中也显示节点?

我尝试了您的代码并得到了相同的结果,但未能在结果中包含 Nodes。我对 EntitySet<Path> 进行了一些改动以获得 Nodes 但不知道它是否适合你。

        builder.EntitySet<Path>("CalculatePaths");
        //builder.ComplexType<Path>()
        //    .HasMany(x => x.Nodes)
        //    .HasDerivedTypeConstraints(typeof(Type1Node), typeof(Type2Node));

需要将键 Id 添加到 Path 实体。

public sealed class Path
{
    public int Id { get; set; }
    public string SourceId { get; set; }
    public string TargetId { get; set; }
    public List<Node> Nodes { get; set; }
}

你期待的结果

相关链接:Navigation property in complex type