ODATA: unbound function returning a complex type with a collection

1.2k views Asked by At

I have been trying to implement something with odata and ASP.NET Core 3 that just does not want to work properly and I cannot seem to figure out what's wrong. I created a small sample application to demonstrate.

I have an odata service I can use to query for nodes. Nodes can be type1 or type2 nodes, and these are open types, with dynamic properties. Querying them works perfectly. What I want to do is calculate paths between nodes. Paths are not entities - they don't have an identity. So I do not believe it would be correct to create a resource for that. They are just results of path calculations, containing lists of nodes that are along the path, so I think that a function is a better way to tell the API what I want.

So I created an odata function to do the calculation and return the available paths, and it works, except I cannot get it to return the list of nodes the path is traversing, which is the one information I actually need.

I created some sample code demonstrating the issue:

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();
    }
}

Sorry for the ugliness, I tried to compact it as much as I could.

Now http://host:port/odata/CalculatePaths?source=A&target=B should return 2 paths, and it does. But only the two string properties are there, the collection property ain't:

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

I tried messing around with it a lot of different ways without joy. The only time I got close to what I want was when I changed the Path to have just node IDs (string) instead of nodes. But that's not ideal, as I would need to then query for the individual nodes, even though I already have all the information required.

What should I change so that in the response the nodes appear as well?

1

There are 1 answers

2
Michael Wang On BEST ANSWER

I tried your codes and got same result failed to include Nodes in result. I made a little change with EntitySet<Path> to get Nodes well but no clue whether it suitable for u or not.

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

Need to add key Id to Path entity.

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; }
}

The result you expect

enter image description here

Related Links: Navigation property in complex type