11using System . Reflection ;
22using JsonApiDotNetCore . Configuration ;
3+ using JsonApiDotNetCore . Controllers ;
34using JsonApiDotNetCore . Middleware ;
45using JsonApiDotNetCore . OpenApi . Swashbuckle . JsonApiObjects . Documents ;
56using JsonApiDotNetCore . Resources . Annotations ;
@@ -12,18 +13,14 @@ namespace JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiMetadata;
1213/// </summary>
1314internal sealed class JsonApiEndpointMetadataProvider
1415{
15- private readonly EndpointResolver _endpointResolver ;
1616 private readonly IControllerResourceMapping _controllerResourceMapping ;
1717 private readonly NonPrimaryDocumentTypeFactory _nonPrimaryDocumentTypeFactory ;
1818
19- public JsonApiEndpointMetadataProvider ( EndpointResolver endpointResolver , IControllerResourceMapping controllerResourceMapping ,
20- NonPrimaryDocumentTypeFactory nonPrimaryDocumentTypeFactory )
19+ public JsonApiEndpointMetadataProvider ( IControllerResourceMapping controllerResourceMapping , NonPrimaryDocumentTypeFactory nonPrimaryDocumentTypeFactory )
2120 {
22- ArgumentGuard . NotNull ( endpointResolver ) ;
2321 ArgumentGuard . NotNull ( controllerResourceMapping ) ;
2422 ArgumentGuard . NotNull ( nonPrimaryDocumentTypeFactory ) ;
2523
26- _endpointResolver = endpointResolver ;
2724 _controllerResourceMapping = controllerResourceMapping ;
2825 _nonPrimaryDocumentTypeFactory = nonPrimaryDocumentTypeFactory ;
2926 }
@@ -32,16 +29,16 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
3229 {
3330 ArgumentGuard . NotNull ( controllerAction ) ;
3431
35- JsonApiEndpoint ? endpoint = _endpointResolver . Get ( controllerAction ) ;
36-
37- if ( endpoint == null )
32+ if ( EndpointResolver . Instance . IsAtomicOperationsController ( controllerAction ) )
3833 {
39- throw new NotSupportedException ( $ "Unable to provide metadata for non-JSON:API endpoint ' { controllerAction . ReflectedType ! . FullName } '." ) ;
34+ return new JsonApiEndpointMetadataContainer ( AtomicOperationsRequestMetadata . Instance , AtomicOperationsResponseMetadata . Instance ) ;
4035 }
4136
42- if ( endpoint == JsonApiEndpoint . PostOperations )
37+ JsonApiEndpoints endpoint = EndpointResolver . Instance . GetEndpoint ( controllerAction ) ;
38+
39+ if ( endpoint == JsonApiEndpoints . None )
4340 {
44- return new JsonApiEndpointMetadataContainer ( AtomicOperationsRequestMetadata . Instance , AtomicOperationsResponseMetadata . Instance ) ;
41+ throw new NotSupportedException ( $ "Unable to provide metadata for non-JSON:API endpoint ' { controllerAction . ReflectedType ! . FullName } '." ) ;
4542 }
4643
4744 ResourceType ? primaryResourceType = _controllerResourceMapping . GetResourceTypeForController ( controllerAction . ReflectedType ) ;
@@ -51,19 +48,19 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
5148 throw new UnreachableCodeException ( ) ;
5249 }
5350
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 ) ;
5653 return new JsonApiEndpointMetadataContainer ( requestMetadata , responseMetadata ) ;
5754 }
5855
59- private IJsonApiRequestMetadata ? GetRequestMetadata ( JsonApiEndpoint endpoint , ResourceType primaryResourceType )
56+ private IJsonApiRequestMetadata ? GetRequestMetadata ( JsonApiEndpoints endpoint , ResourceType primaryResourceType )
6057 {
6158 return endpoint switch
6259 {
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 ) ,
6764 _ => null
6865 } ;
6966 }
@@ -92,14 +89,14 @@ private RelationshipRequestMetadata GetRelationshipRequestMetadata(IEnumerable<R
9289 return new RelationshipRequestMetadata ( requestDocumentTypesByRelationshipName ) ;
9390 }
9491
95- private IJsonApiResponseMetadata ? GetResponseMetadata ( JsonApiEndpoint endpoint , ResourceType primaryResourceType )
92+ private IJsonApiResponseMetadata ? GetResponseMetadata ( JsonApiEndpoints endpoint , ResourceType primaryResourceType )
9693 {
9794 return endpoint switch
9895 {
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 ) ,
103100 _ => null
104101 } ;
105102 }
0 commit comments