Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odata8.x #4

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ODatalizer.EFCore.Tests/SimpleTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json.Linq;
using ODatalizer.EFCore.Tests.Host;
using Sample.EFCore;
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -34,7 +35,11 @@ public async Task Get()

Assert.Equal(HttpStatusCode.OK, response.StatusCode);

var result = JObject.Parse(await response.Content.ReadAsStringAsync());
var json = await response.Content.ReadAsStringAsync();

Console.WriteLine(json);

var result = JObject.Parse(json);

var total = (int)result.SelectToken("$['@odata.count']");
var products = result.SelectTokens("$.value[:]");
Expand Down
3 changes: 2 additions & 1 deletion src/ODatalizer.EFCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.OData.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.DependencyInjection;
using ODatalizer.EFCore.Builders;
using Microsoft.AspNetCore.OData;

namespace ODatalizer.EFCore
{
Expand Down
4 changes: 2 additions & 2 deletions src/ODatalizer.EFCore/Builders/EdmBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNet.OData.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using System;
using System.Linq;

Expand Down
69 changes: 32 additions & 37 deletions src/ODatalizer.EFCore/EndpointRouteBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
using Microsoft.AspNet.OData.Batch;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Formatter.Deserialization;
using Microsoft.AspNet.OData.Routing.Conventions;
using Microsoft.AspNetCore.Routing;
using Microsoft.OData;
using Microsoft.OData.Json;
using ODatalizer.Batch;
using ODatalizer.EFCore.Routing.Conventions;
using System.Collections.Generic;
//using Microsoft.AspNetCore.Routing;
//using Microsoft.OData;
//using Microsoft.OData.Json;
//using ODatalizer.EFCore.Routing.Conventions;
//using System.Collections.Generic;

namespace ODatalizer.EFCore
{
public static class EndpointRouteBuilderExtensions
{
public static void MapODatalizer(this IEndpointRouteBuilder builder, params ODatalizerEndpoint[] endpoints)
{
builder.Select().Expand().Filter().OrderBy().MaxTop(ODatalizerEndpoint.DefaultPageSize).Count().SkipToken();
foreach (var ep in endpoints)
{
builder.MapODataRoute(ep.RouteName, ep.RoutePrefix, b => {
b.AddService<IJsonWriterFactory>(ServiceLifetime.Singleton, sp => new DefaultJsonWriterFactory(ODataStringEscapeOption.EscapeOnlyControls));
b.AddService(ServiceLifetime.Singleton, sp => ep.EdmModel);
b.AddService<ODataBatchHandler>(ServiceLifetime.Singleton, sp => new ODatalizerBatchHandler());
b.AddService<ODataDeserializerProvider>(ServiceLifetime.Singleton, sp => new DefaultODataDeserializerProvider(sp));
b.AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => {
var conventions = ODataRoutingConventions.CreateDefaultWithAttributeRouting(ep.RouteName, builder.ServiceProvider);
if (string.IsNullOrEmpty(ep.ODatalizerController) == false)
{
conventions.Add(new ODatalizerDynamicConvention(ep.ODatalizerController.Replace("Controller", string.Empty)));
}
return conventions;
});
});
}
}
}
}
//namespace ODatalizer.EFCore
//{
// public static class EndpointRouteBuilderExtensions
// {
// public static void MapODatalizer(this IEndpointRouteBuilder builder, params ODatalizerEndpoint[] endpoints)
// {
// builder.Select().Expand().Filter().OrderBy().MaxTop(ODatalizerEndpoint.DefaultPageSize).Count().SkipToken();
// foreach (var ep in endpoints)
// {
// builder.MapODataRoute(ep.RouteName, ep.RoutePrefix, b => {
// b.AddService<IJsonWriterFactory>(ServiceLifetime.Singleton, sp => new DefaultJsonWriterFactory(ODataStringEscapeOption.EscapeOnlyControls));
// b.AddService(ServiceLifetime.Singleton, sp => ep.EdmModel);
// b.AddService<ODataBatchHandler>(ServiceLifetime.Singleton, sp => new ODatalizerBatchHandler());
// b.AddService<ODataDeserializerProvider>(ServiceLifetime.Singleton, sp => new DefaultODataDeserializerProvider(sp));
// b.AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => {
// var conventions = ODataRoutingConventions.CreateDefaultWithAttributeRouting(ep.RouteName, builder.ServiceProvider);
// if (string.IsNullOrEmpty(ep.ODatalizerController) == false)
// {
// conventions.Add(new ODatalizerDynamicConvention(ep.ODatalizerController.Replace("Controller", string.Empty)));
// }
// return conventions;
// });
// });
// }
// }
// }
//}
6 changes: 4 additions & 2 deletions src/ODatalizer.EFCore/ODatalizer.EFCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.6.*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.12" />
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.12" />
<PackageReference Include="Microsoft.AspNetCore.OData.NewtonsoftJson" Version="8.0.12" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.*" />
<PackageReference Include="System.CodeDom" Version="7.0.0" />
<PackageReference Include="System.CodeDom" Version="6.0.*" />
</ItemGroup>

<ItemGroup>
Expand Down
54 changes: 36 additions & 18 deletions src/ODatalizer.EFCore/ODatalizerController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.OData.Extensions;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
Expand All @@ -14,6 +14,9 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Deltas;
using ODatalizer.EFCore.Routing;

namespace ODatalizer.EFCore
{
Expand Down Expand Up @@ -42,16 +45,19 @@ public virtual async Task<IActionResult> Get()
{
var odataPath = Request.ODataFeature().Path;

if (odataPath.Segments.Any(segment => segment is Microsoft.AspNet.OData.Routing.UnresolvedPathSegment))
if (odataPath == null)
return NotFound();

if (odataPath.Any(segment => segment is UnrecognizedPathSegment))
return BadRequest("Invalid URI Path");

try
{
await _visitor.VisitAsync(odataPath.Path);
await _visitor.VisitAsync(odataPath);
}
catch (NullReferenceException nrex)
{
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index} of {@Template}", Request.Path, _visitor.Index, odataPath.PathTemplate);
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index}", Request.Path, _visitor.Index);
return NotFound();
}
catch (NotSupportedException)
Expand Down Expand Up @@ -85,16 +91,19 @@ public virtual async Task<IActionResult> Post()
{
var odataPath = Request.ODataFeature().Path;

if (odataPath.Segments.Any(segment => segment is Microsoft.AspNet.OData.Routing.UnresolvedPathSegment))
if (odataPath == null)
return NotFound();

if (odataPath.Any(segment => segment is UnrecognizedPathSegment))
return BadRequest("Invalid URI Path");

try
{
await _visitor.VisitAsync(odataPath.Path);
await _visitor.VisitAsync(odataPath);
}
catch (NullReferenceException nrex)
{
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index} of {@Template}", Request.Path, _visitor.Index, odataPath.PathTemplate);
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index}", Request.Path, _visitor.Index);
return NotFound();
}
catch (NotSupportedException)
Expand Down Expand Up @@ -144,16 +153,19 @@ public virtual async Task<IActionResult> Put()
{
var odataPath = Request.ODataFeature().Path;

if (odataPath.Segments.Any(segment => segment is Microsoft.AspNet.OData.Routing.UnresolvedPathSegment))
if (odataPath == null)
return NotFound();

if (odataPath.Any(segment => segment is UnrecognizedPathSegment))
return BadRequest("Invalid URI Path");

try
{
await _visitor.VisitAsync(odataPath.Path);
await _visitor.VisitAsync(odataPath);
}
catch (NullReferenceException nrex)
{
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index} of {@Template}", Request.Path, _visitor.Index, odataPath.PathTemplate);
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index} of", Request.Path, _visitor.Index);
return NotFound();
}
catch (NotSupportedException)
Expand Down Expand Up @@ -186,7 +198,7 @@ public virtual async Task<IActionResult> Put()
return BadRequest(this.CreateSerializableErrorFromModelState());


if (odataPath.Path.LastSegment is PropertySegment || odataPath.Path.LastSegment is ValueSegment)
if (odataPath.LastSegment is PropertySegment || odataPath.LastSegment is ValueSegment)
{
_visitor.PropertySetter.Invoke(result.Model);
}
Expand All @@ -205,16 +217,19 @@ public virtual async Task<IActionResult> Patch()
{
var odataPath = Request.ODataFeature().Path;

if (odataPath.Segments.Any(segment => segment is Microsoft.AspNet.OData.Routing.UnresolvedPathSegment))
if (odataPath == null)
return NotFound();

if (odataPath.Any(segment => segment is UnrecognizedPathSegment))
return BadRequest("Invalid URI Path");

try
{
await _visitor.VisitAsync(odataPath.Path);
await _visitor.VisitAsync(odataPath);
}
catch (NullReferenceException nrex)
{
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index} of {@Template}", Request.Path, _visitor.Index, odataPath.PathTemplate);
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index}", Request.Path, _visitor.Index);
return NotFound();
}
catch (NotSupportedException)
Expand Down Expand Up @@ -260,16 +275,19 @@ public virtual async Task<IActionResult> Delete()
{
var odataPath = Request.ODataFeature().Path;

if (odataPath.Segments.Any(segment => segment is Microsoft.AspNet.OData.Routing.UnresolvedPathSegment))
if (odataPath == null)
return NotFound();

if (odataPath.Any(segment => segment is UnrecognizedPathSegment))
return BadRequest("Invalid URI Path");

try
{
await _visitor.VisitAsync(odataPath.Path);
await _visitor.VisitAsync(odataPath);
}
catch (NullReferenceException nrex)
{
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index} of {@Template}", Request.Path, _visitor.Index, odataPath.PathTemplate);
_logger.LogInformation(ODatalizerLogEvents.DynamicVisitorNullReferenced, nrex, "Null on {@Path} at {@Index}", Request.Path, _visitor.Index);
return NotFound();
}
catch (NotSupportedException)
Expand Down
10 changes: 10 additions & 0 deletions src/ODatalizer.EFCore/ODatalizerEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData.Edm;
using ODatalizer.EFCore.Builders;
using System;

namespace ODatalizer.EFCore
{
Expand Down Expand Up @@ -34,4 +36,12 @@ public ODatalizerEndpoint(DbContext db, string routeName = null, string routePre
EdmModel = EdmBuilder.Build(db);
}
}

public class ODatalizerEndpoint<TDbContext> : ODatalizerEndpoint where TDbContext : DbContext
{
public ODatalizerEndpoint(IServiceProvider sp, string routeName = null, string routePrefix = null, string controller = null, string @namespace = null, bool authorize = false) : base(sp.GetRequiredService<TDbContext>(), routeName, routePrefix, controller, @namespace, authorize)
{
}
}

}

This file was deleted.

12 changes: 12 additions & 0 deletions src/ODatalizer.EFCore/Routing/ODatalizerControllerNameAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ODatalizer.EFCore.Routing
{
public class ODatalizerControllerNameAccessor
{
public ODatalizerControllerNameAccessor(string name)
{
ControllerName = name;
}

public string ControllerName { get; private set; }
}
}
Loading
Loading