1
1
using System . Reflection ;
2
2
using JsonApiDotNetCore . Configuration ;
3
+ using JsonApiDotNetCore . Controllers ;
3
4
using JsonApiDotNetCore . Middleware ;
4
5
using JsonApiDotNetCore . OpenApi . Swashbuckle . JsonApiObjects . Documents ;
5
6
using JsonApiDotNetCore . Resources . Annotations ;
@@ -12,18 +13,14 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
12
13
/// </summary>
13
14
internal sealed class JsonApiEndpointMetadataProvider
14
15
{
15
- private readonly EndpointResolver _endpointResolver ;
16
16
private readonly IControllerResourceMapping _controllerResourceMapping ;
17
17
private readonly NonPrimaryDocumentTypeFactory _nonPrimaryDocumentTypeFactory ;
18
18
19
- public JsonApiEndpointMetadataProvider ( EndpointResolver endpointResolver , IControllerResourceMapping controllerResourceMapping ,
20
- NonPrimaryDocumentTypeFactory nonPrimaryDocumentTypeFactory )
19
+ public JsonApiEndpointMetadataProvider ( IControllerResourceMapping controllerResourceMapping , NonPrimaryDocumentTypeFactory nonPrimaryDocumentTypeFactory )
21
20
{
22
- ArgumentGuard . NotNull ( endpointResolver ) ;
23
21
ArgumentGuard . NotNull ( controllerResourceMapping ) ;
24
22
ArgumentGuard . NotNull ( nonPrimaryDocumentTypeFactory ) ;
25
23
26
- _endpointResolver = endpointResolver ;
27
24
_controllerResourceMapping = controllerResourceMapping ;
28
25
_nonPrimaryDocumentTypeFactory = nonPrimaryDocumentTypeFactory ;
29
26
}
@@ -32,16 +29,16 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
32
29
{
33
30
ArgumentGuard . NotNull ( controllerAction ) ;
34
31
35
- JsonApiEndpoint ? endpoint = _endpointResolver . Get ( controllerAction ) ;
36
-
37
- if ( endpoint == null )
32
+ if ( EndpointResolver . Instance . IsAtomicOperationsController ( controllerAction ) )
38
33
{
39
- throw new NotSupportedException ( $ "Unable to provide metadata for non-JSON:API endpoint ' { controllerAction . ReflectedType ! . FullName } '." ) ;
34
+ return new JsonApiEndpointMetadataContainer ( AtomicOperationsRequestMetadata . Instance , AtomicOperationsResponseMetadata . Instance ) ;
40
35
}
41
36
42
- if ( endpoint == JsonApiEndpoint . PostOperations )
37
+ JsonApiEndpoints endpoint = EndpointResolver . Instance . GetEndpoint ( controllerAction ) ;
38
+
39
+ if ( endpoint == JsonApiEndpoints . None )
43
40
{
44
- return new JsonApiEndpointMetadataContainer ( AtomicOperationsRequestMetadata . Instance , AtomicOperationsResponseMetadata . Instance ) ;
41
+ throw new NotSupportedException ( $ "Unable to provide metadata for non-JSON:API endpoint ' { controllerAction . ReflectedType ! . FullName } '." ) ;
45
42
}
46
43
47
44
ResourceType ? primaryResourceType = _controllerResourceMapping . GetResourceTypeForController ( controllerAction . ReflectedType ) ;
@@ -51,19 +48,19 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
51
48
throw new UnreachableCodeException ( ) ;
52
49
}
53
50
54
- IJsonApiRequestMetadata ? requestMetadata = GetRequestMetadata ( endpoint . Value , primaryResourceType ) ;
55
- IJsonApiResponseMetadata ? responseMetadata = GetResponseMetadata ( endpoint . Value , primaryResourceType ) ;
51
+ IJsonApiRequestMetadata ? requestMetadata = GetRequestMetadata ( endpoint , primaryResourceType ) ;
52
+ IJsonApiResponseMetadata ? responseMetadata = GetResponseMetadata ( endpoint , primaryResourceType ) ;
56
53
return new JsonApiEndpointMetadataContainer ( requestMetadata , responseMetadata ) ;
57
54
}
58
55
59
- private IJsonApiRequestMetadata ? GetRequestMetadata ( JsonApiEndpoint endpoint , ResourceType primaryResourceType )
56
+ private IJsonApiRequestMetadata ? GetRequestMetadata ( JsonApiEndpoints endpoint , ResourceType primaryResourceType )
60
57
{
61
58
return endpoint switch
62
59
{
63
- JsonApiEndpoint . PostResource => GetPostResourceRequestMetadata ( primaryResourceType . ClrType ) ,
64
- JsonApiEndpoint . PatchResource => GetPatchResourceRequestMetadata ( primaryResourceType . ClrType ) ,
65
- JsonApiEndpoint . PostRelationship or JsonApiEndpoint . PatchRelationship or JsonApiEndpoint . DeleteRelationship => GetRelationshipRequestMetadata (
66
- primaryResourceType . Relationships , endpoint != JsonApiEndpoint . PatchRelationship ) ,
60
+ JsonApiEndpoints . Post => GetPostResourceRequestMetadata ( primaryResourceType . ClrType ) ,
61
+ JsonApiEndpoints . Patch => GetPatchResourceRequestMetadata ( primaryResourceType . ClrType ) ,
62
+ JsonApiEndpoints . PostRelationship or JsonApiEndpoints . PatchRelationship or JsonApiEndpoints . DeleteRelationship => GetRelationshipRequestMetadata (
63
+ primaryResourceType . Relationships , endpoint != JsonApiEndpoints . PatchRelationship ) ,
67
64
_ => null
68
65
} ;
69
66
}
@@ -92,14 +89,14 @@ private RelationshipRequestMetadata GetRelationshipRequestMetadata(IEnumerable<R
92
89
return new RelationshipRequestMetadata ( requestDocumentTypesByRelationshipName ) ;
93
90
}
94
91
95
- private IJsonApiResponseMetadata ? GetResponseMetadata ( JsonApiEndpoint endpoint , ResourceType primaryResourceType )
92
+ private IJsonApiResponseMetadata ? GetResponseMetadata ( JsonApiEndpoints endpoint , ResourceType primaryResourceType )
96
93
{
97
94
return endpoint switch
98
95
{
99
- JsonApiEndpoint . GetCollection or JsonApiEndpoint . GetSingle or JsonApiEndpoint . PostResource or JsonApiEndpoint . PatchResource =>
100
- GetPrimaryResponseMetadata ( primaryResourceType . ClrType , endpoint == JsonApiEndpoint . GetCollection ) ,
101
- JsonApiEndpoint . GetSecondary => GetSecondaryResponseMetadata ( primaryResourceType . Relationships ) ,
102
- JsonApiEndpoint . GetRelationship => GetRelationshipResponseMetadata ( primaryResourceType . Relationships ) ,
96
+ JsonApiEndpoints . GetCollection or JsonApiEndpoints . GetSingle or JsonApiEndpoints . Post or JsonApiEndpoints . Patch => GetPrimaryResponseMetadata (
97
+ primaryResourceType . ClrType , endpoint == JsonApiEndpoints . GetCollection ) ,
98
+ JsonApiEndpoints . GetSecondary => GetSecondaryResponseMetadata ( primaryResourceType . Relationships ) ,
99
+ JsonApiEndpoints . GetRelationship => GetRelationshipResponseMetadata ( primaryResourceType . Relationships ) ,
103
100
_ => null
104
101
} ;
105
102
}
0 commit comments