-
-
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 branch 'master' into merge-master-into-openapi
- Loading branch information
Showing
20 changed files
with
398 additions
and
91 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
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
32 changes: 32 additions & 0 deletions
32
src/JsonApiDotNetCore/AtomicOperations/DefaultOperationFilter.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,32 @@ | ||
using System.Reflection; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Controllers; | ||
using JsonApiDotNetCore.Middleware; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace JsonApiDotNetCore.AtomicOperations; | ||
|
||
/// <inheritdoc /> | ||
internal sealed class DefaultOperationFilter : IAtomicOperationFilter | ||
{ | ||
/// <inheritdoc /> | ||
public bool IsEnabled(ResourceType resourceType, WriteOperationKind writeOperation) | ||
{ | ||
var resourceAttribute = resourceType.ClrType.GetCustomAttribute<ResourceAttribute>(); | ||
return resourceAttribute != null && Contains(resourceAttribute.GenerateControllerEndpoints, writeOperation); | ||
} | ||
|
||
private static bool Contains(JsonApiEndpoints endpoints, WriteOperationKind writeOperation) | ||
{ | ||
return writeOperation switch | ||
{ | ||
WriteOperationKind.CreateResource => endpoints.HasFlag(JsonApiEndpoints.Post), | ||
WriteOperationKind.UpdateResource => endpoints.HasFlag(JsonApiEndpoints.Patch), | ||
WriteOperationKind.DeleteResource => endpoints.HasFlag(JsonApiEndpoints.Delete), | ||
WriteOperationKind.SetRelationship => endpoints.HasFlag(JsonApiEndpoints.PatchRelationship), | ||
WriteOperationKind.AddToRelationship => endpoints.HasFlag(JsonApiEndpoints.PostRelationship), | ||
WriteOperationKind.RemoveFromRelationship => endpoints.HasFlag(JsonApiEndpoints.DeleteRelationship), | ||
_ => false | ||
}; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/JsonApiDotNetCore/AtomicOperations/IAtomicOperationFilter.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,42 @@ | ||
using JetBrains.Annotations; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Middleware; | ||
using JsonApiDotNetCore.Resources.Annotations; | ||
|
||
namespace JsonApiDotNetCore.AtomicOperations; | ||
|
||
/// <summary> | ||
/// Determines whether an operation in an atomic:operations request can be used. | ||
/// </summary> | ||
/// <remarks> | ||
/// The default implementation relies on the usage of <see cref="ResourceAttribute.GenerateControllerEndpoints" />. If you're using explicit | ||
/// (non-generated) controllers, register your own implementation to indicate which operations are accessible. | ||
/// </remarks> | ||
[PublicAPI] | ||
public interface IAtomicOperationFilter | ||
{ | ||
/// <summary> | ||
/// An <see cref="IAtomicOperationFilter" /> that always returns <c>true</c>. Provided for convenience, to revert to the original behavior from before | ||
/// filtering was introduced. | ||
/// </summary> | ||
public static IAtomicOperationFilter AlwaysEnabled { get; } = new AlwaysEnabledOperationFilter(); | ||
|
||
/// <summary> | ||
/// Determines whether the specified operation can be used in an atomic:operations request. | ||
/// </summary> | ||
/// <param name="resourceType"> | ||
/// The targeted primary resource type of the operation. | ||
/// </param> | ||
/// <param name="writeOperation"> | ||
/// The operation kind. | ||
/// </param> | ||
bool IsEnabled(ResourceType resourceType, WriteOperationKind writeOperation); | ||
|
||
private sealed class AlwaysEnabledOperationFilter : IAtomicOperationFilter | ||
{ | ||
public bool IsEnabled(ResourceType resourceType, WriteOperationKind writeOperation) | ||
{ | ||
return true; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.