-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1646 from json-api-dotnet/endpoint-filter
IJsonApiEndpointFilter: remove controller action methods at runtime
- Loading branch information
Showing
11 changed files
with
272 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/JsonApiDotNetCore/Middleware/AlwaysEnabledJsonApiEndpointFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Controllers; | ||
|
||
namespace JsonApiDotNetCore.Middleware; | ||
|
||
internal sealed class AlwaysEnabledJsonApiEndpointFilter : IJsonApiEndpointFilter | ||
{ | ||
/// <inheritdoc /> | ||
public bool IsEnabled(ResourceType resourceType, JsonApiEndpoints endpoint) | ||
{ | ||
return true; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/JsonApiDotNetCore/Middleware/HttpMethodAttributeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using JsonApiDotNetCore.Controllers; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Routing; | ||
|
||
namespace JsonApiDotNetCore.Middleware; | ||
|
||
internal static class HttpMethodAttributeExtensions | ||
{ | ||
private const string IdTemplate = "{id}"; | ||
private const string RelationshipNameTemplate = "{relationshipName}"; | ||
private const string SecondaryEndpointTemplate = $"{IdTemplate}/{RelationshipNameTemplate}"; | ||
private const string RelationshipEndpointTemplate = $"{IdTemplate}/relationships/{RelationshipNameTemplate}"; | ||
|
||
public static JsonApiEndpoints GetJsonApiEndpoint(this IEnumerable<HttpMethodAttribute> httpMethods) | ||
{ | ||
ArgumentGuard.NotNull(httpMethods); | ||
|
||
HttpMethodAttribute[] nonHeadAttributes = httpMethods.Where(attribute => attribute is not HttpHeadAttribute).ToArray(); | ||
|
||
return nonHeadAttributes.Length == 1 ? ResolveJsonApiEndpoint(nonHeadAttributes[0]) : JsonApiEndpoints.None; | ||
} | ||
|
||
private static JsonApiEndpoints ResolveJsonApiEndpoint(HttpMethodAttribute httpMethod) | ||
{ | ||
return httpMethod switch | ||
{ | ||
HttpGetAttribute httpGet => httpGet.Template switch | ||
{ | ||
null => JsonApiEndpoints.GetCollection, | ||
IdTemplate => JsonApiEndpoints.GetSingle, | ||
SecondaryEndpointTemplate => JsonApiEndpoints.GetSecondary, | ||
RelationshipEndpointTemplate => JsonApiEndpoints.GetRelationship, | ||
_ => JsonApiEndpoints.None | ||
}, | ||
HttpPostAttribute httpPost => httpPost.Template switch | ||
{ | ||
null => JsonApiEndpoints.Post, | ||
RelationshipEndpointTemplate => JsonApiEndpoints.PostRelationship, | ||
_ => JsonApiEndpoints.None | ||
}, | ||
HttpPatchAttribute httpPatch => httpPatch.Template switch | ||
{ | ||
IdTemplate => JsonApiEndpoints.Patch, | ||
RelationshipEndpointTemplate => JsonApiEndpoints.PatchRelationship, | ||
_ => JsonApiEndpoints.None | ||
}, | ||
HttpDeleteAttribute httpDelete => httpDelete.Template switch | ||
{ | ||
IdTemplate => JsonApiEndpoints.Delete, | ||
RelationshipEndpointTemplate => JsonApiEndpoints.DeleteRelationship, | ||
_ => JsonApiEndpoints.None | ||
}, | ||
_ => JsonApiEndpoints.None | ||
}; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/JsonApiDotNetCore/Middleware/IJsonApiEndpointFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using JetBrains.Annotations; | ||
using JsonApiDotNetCore.AtomicOperations; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Controllers; | ||
|
||
namespace JsonApiDotNetCore.Middleware; | ||
|
||
/// <summary> | ||
/// Enables to remove JSON:API controller action methods at startup. For atomic:operation requests, see <see cref="IAtomicOperationFilter" />. | ||
/// </summary> | ||
[PublicAPI] | ||
public interface IJsonApiEndpointFilter | ||
{ | ||
/// <summary> | ||
/// Determines whether to remove the associated controller action method. | ||
/// </summary> | ||
/// <param name="resourceType"> | ||
/// The primary resource type of the endpoint. | ||
/// </param> | ||
/// <param name="endpoint"> | ||
/// The JSON:API endpoint. Despite <see cref="JsonApiEndpoints" /> being a <see cref="FlagsAttribute" /> enum, a single value is always passed here. | ||
/// </param> | ||
bool IsEnabled(ResourceType resourceType, JsonApiEndpoints endpoint); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.