Skip to content

Fixed: only run custom path cutting logic when RouteAttribute is used #769

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

Merged
merged 1 commit into from
May 22, 2020
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
22 changes: 15 additions & 7 deletions src/JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using JsonApiDotNetCore.Managers.Contracts;
using JsonApiDotNetCore.Models.JsonApiDocuments;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
Expand Down Expand Up @@ -181,7 +182,7 @@ private static string GetBasePath(string resourceName, IJsonApiOptions options,
builder.Append(httpRequest.Host);
}

string customRoute = GetCustomRoute(httpRequest.Path.Value, resourceName, options.Namespace);
string customRoute = GetCustomRoute(resourceName, options.Namespace, httpRequest.HttpContext);
if (!string.IsNullOrEmpty(customRoute))
{
builder.Append('/');
Expand All @@ -196,13 +197,20 @@ private static string GetBasePath(string resourceName, IJsonApiOptions options,
return builder.ToString();
}

private static string GetCustomRoute(string path, string resourceName, string apiNamespace)
private static string GetCustomRoute(string resourceName, string apiNamespace, HttpContext httpContext)
{
var trimmedComponents = path.Trim('/').Split('/').ToList();
var resourceNameIndex = trimmedComponents.FindIndex(c => c == resourceName);
var newComponents = trimmedComponents.Take(resourceNameIndex).ToArray();
var customRoute = string.Join('/', newComponents);
return customRoute == apiNamespace ? null : customRoute;
var endpoint = httpContext.GetEndpoint();
var routeAttribute = endpoint.Metadata.GetMetadata<RouteAttribute>();
if (routeAttribute != null)
{
var trimmedComponents = httpContext.Request.Path.Value.Trim('/').Split('/').ToList();
var resourceNameIndex = trimmedComponents.FindIndex(c => c == resourceName);
var newComponents = trimmedComponents.Take(resourceNameIndex).ToArray();
var customRoute = string.Join('/', newComponents);
return customRoute == apiNamespace ? null : customRoute;
}

return null;
}

private static bool GetIsRelationshipPath(RouteValueDictionary routeValues)
Expand Down
1 change: 1 addition & 0 deletions test/UnitTests/Middleware/JsonApiMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private static DefaultHttpContext CreateHttpContext(string path, bool isRelation
feature.RouteValues["id"] = id;
}
context.Features.Set<IRouteValuesFeature>(feature);
context.SetEndpoint(new Endpoint(null, new EndpointMetadataCollection(), null));
return context;
}

Expand Down