|
| 1 | +using System.Reflection; |
1 | 2 | using JsonApiDotNetCore.Configuration;
|
| 3 | +using JsonApiDotNetCore.Controllers; |
2 | 4 | using JsonApiDotNetCore.Middleware;
|
3 | 5 | using JsonApiDotNetCore.OpenApi.JsonApiMetadata;
|
4 | 6 | using JsonApiDotNetCore.Resources.Annotations;
|
@@ -29,48 +31,88 @@ public void Apply(ActionModel action)
|
29 | 31 |
|
30 | 32 | JsonApiEndpoint? endpoint = _endpointResolver.Get(action.ActionMethod);
|
31 | 33 |
|
32 |
| - if (endpoint == null || ShouldSuppressEndpoint(endpoint.Value, action.Controller.ControllerType)) |
| 34 | + if (endpoint == null) |
33 | 35 | {
|
| 36 | + // Not a JSON:API controller, or a non-standard action method in a JSON:API controller, or an atomic:operations |
| 37 | + // controller. None of these are yet implemented, so hide them to avoid downstream crashes. |
34 | 38 | action.ApiExplorer.IsVisible = false;
|
| 39 | + return; |
| 40 | + } |
35 | 41 |
|
| 42 | + if (ShouldSuppressEndpoint(endpoint.Value, action.Controller.ControllerType)) |
| 43 | + { |
| 44 | + action.ApiExplorer.IsVisible = false; |
36 | 45 | return;
|
37 | 46 | }
|
38 | 47 |
|
39 | 48 | SetResponseMetadata(action, endpoint.Value);
|
40 |
| - |
41 | 49 | SetRequestMetadata(action, endpoint.Value);
|
42 | 50 | }
|
43 | 51 |
|
44 | 52 | private bool ShouldSuppressEndpoint(JsonApiEndpoint endpoint, Type controllerType)
|
45 | 53 | {
|
46 |
| - if (IsSecondaryOrRelationshipEndpoint(endpoint)) |
| 54 | + ResourceType? resourceType = _controllerResourceMapping.GetResourceTypeForController(controllerType); |
| 55 | + |
| 56 | + if (resourceType == null) |
| 57 | + { |
| 58 | + throw new UnreachableCodeException(); |
| 59 | + } |
| 60 | + |
| 61 | + if (!IsEndpointAvailable(endpoint, resourceType)) |
47 | 62 | {
|
48 |
| - IReadOnlyCollection<RelationshipAttribute> relationships = GetRelationshipsOfPrimaryResource(controllerType); |
| 63 | + return true; |
| 64 | + } |
49 | 65 |
|
50 |
| - if (!relationships.Any()) |
| 66 | + if (IsSecondaryOrRelationshipEndpoint(endpoint)) |
| 67 | + { |
| 68 | + if (!resourceType.Relationships.Any()) |
51 | 69 | {
|
52 | 70 | return true;
|
53 | 71 | }
|
54 | 72 |
|
55 | 73 | if (endpoint is JsonApiEndpoint.DeleteRelationship or JsonApiEndpoint.PostRelationship)
|
56 | 74 | {
|
57 |
| - return !relationships.OfType<HasManyAttribute>().Any(); |
| 75 | + return !resourceType.Relationships.OfType<HasManyAttribute>().Any(); |
58 | 76 | }
|
59 | 77 | }
|
60 | 78 |
|
61 | 79 | return false;
|
62 | 80 | }
|
63 | 81 |
|
64 |
| - private IReadOnlyCollection<RelationshipAttribute> GetRelationshipsOfPrimaryResource(Type controllerType) |
| 82 | + private static bool IsEndpointAvailable(JsonApiEndpoint endpoint, ResourceType resourceType) |
65 | 83 | {
|
66 |
| - ResourceType? primaryResourceType = _controllerResourceMapping.GetResourceTypeForController(controllerType); |
| 84 | + JsonApiEndpoints availableEndpoints = GetGeneratedControllerEndpoints(resourceType); |
67 | 85 |
|
68 |
| - if (primaryResourceType == null) |
| 86 | + if (availableEndpoints == JsonApiEndpoints.None) |
69 | 87 | {
|
70 |
| - throw new UnreachableCodeException(); |
| 88 | + // Auto-generated controllers are disabled, so we can't know what to hide. |
| 89 | + // It is assumed that a handwritten JSON:API controller only provides action methods for what it supports. |
| 90 | + // To accomplish that, derive from BaseJsonApiController instead of JsonApiController. |
| 91 | + return true; |
71 | 92 | }
|
72 | 93 |
|
73 |
| - return primaryResourceType.Relationships; |
| 94 | + // For an overridden JSON:API action method in a partial class to show up, it's flag must be turned on in [Resource]. |
| 95 | + // Otherwise, it is considered to be an action method that throws because the endpoint is unavailable. |
| 96 | + return endpoint switch |
| 97 | + { |
| 98 | + JsonApiEndpoint.GetCollection => availableEndpoints.HasFlag(JsonApiEndpoints.GetCollection), |
| 99 | + JsonApiEndpoint.GetSingle => availableEndpoints.HasFlag(JsonApiEndpoints.GetSingle), |
| 100 | + JsonApiEndpoint.GetSecondary => availableEndpoints.HasFlag(JsonApiEndpoints.GetSecondary), |
| 101 | + JsonApiEndpoint.GetRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.GetRelationship), |
| 102 | + JsonApiEndpoint.Post => availableEndpoints.HasFlag(JsonApiEndpoints.Post), |
| 103 | + JsonApiEndpoint.PostRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.PostRelationship), |
| 104 | + JsonApiEndpoint.Patch => availableEndpoints.HasFlag(JsonApiEndpoints.Patch), |
| 105 | + JsonApiEndpoint.PatchRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.PatchRelationship), |
| 106 | + JsonApiEndpoint.Delete => availableEndpoints.HasFlag(JsonApiEndpoints.Delete), |
| 107 | + JsonApiEndpoint.DeleteRelationship => availableEndpoints.HasFlag(JsonApiEndpoints.DeleteRelationship), |
| 108 | + _ => throw new UnreachableCodeException() |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + private static JsonApiEndpoints GetGeneratedControllerEndpoints(ResourceType resourceType) |
| 113 | + { |
| 114 | + var resourceAttribute = resourceType.ClrType.GetCustomAttribute<ResourceAttribute>(); |
| 115 | + return resourceAttribute?.GenerateControllerEndpoints ?? JsonApiEndpoints.None; |
74 | 116 | }
|
75 | 117 |
|
76 | 118 | private static bool IsSecondaryOrRelationshipEndpoint(JsonApiEndpoint endpoint)
|
|
0 commit comments