4
4
using System . Reflection ;
5
5
using JsonApiDotNetCore . Configuration ;
6
6
using JsonApiDotNetCore . Middleware ;
7
+ using JsonApiDotNetCore . OpenApi . JsonApiObjects ;
7
8
using JsonApiDotNetCore . OpenApi . JsonApiObjects . Documents ;
8
- using JsonApiDotNetCore . OpenApi . JsonApiObjects . RelationshipData ;
9
9
using JsonApiDotNetCore . Resources . Annotations ;
10
10
11
11
namespace JsonApiDotNetCore . OpenApi . JsonApiMetadata
@@ -16,16 +16,14 @@ namespace JsonApiDotNetCore.OpenApi.JsonApiMetadata
16
16
/// </summary>
17
17
internal sealed class JsonApiEndpointMetadataProvider
18
18
{
19
- private readonly IResourceGraph _resourceGraph ;
20
19
private readonly IControllerResourceMapping _controllerResourceMapping ;
21
20
private readonly EndpointResolver _endpointResolver = new ( ) ;
21
+ private readonly NonPrimaryDocumentTypeFactory _nonPrimaryDocumentTypeFactory = new ( ) ;
22
22
23
- public JsonApiEndpointMetadataProvider ( IResourceGraph resourceGraph , IControllerResourceMapping controllerResourceMapping )
23
+ public JsonApiEndpointMetadataProvider ( IControllerResourceMapping controllerResourceMapping )
24
24
{
25
- ArgumentGuard . NotNull ( resourceGraph , nameof ( resourceGraph ) ) ;
26
25
ArgumentGuard . NotNull ( controllerResourceMapping , nameof ( controllerResourceMapping ) ) ;
27
26
28
- _resourceGraph = resourceGraph ;
29
27
_controllerResourceMapping = controllerResourceMapping ;
30
28
}
31
29
@@ -47,28 +45,28 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
47
45
throw new UnreachableCodeException ( ) ;
48
46
}
49
47
50
- IJsonApiRequestMetadata ? requestMetadata = GetRequestMetadata ( endpoint . Value , primaryResourceType . ClrType ) ;
51
- IJsonApiResponseMetadata ? responseMetadata = GetResponseMetadata ( endpoint . Value , primaryResourceType . ClrType ) ;
48
+ IJsonApiRequestMetadata ? requestMetadata = GetRequestMetadata ( endpoint . Value , primaryResourceType ) ;
49
+ IJsonApiResponseMetadata ? responseMetadata = GetResponseMetadata ( endpoint . Value , primaryResourceType ) ;
52
50
return new JsonApiEndpointMetadataContainer ( requestMetadata , responseMetadata ) ;
53
51
}
54
52
55
- private IJsonApiRequestMetadata ? GetRequestMetadata ( JsonApiEndpoint endpoint , Type primaryResourceType )
53
+ private IJsonApiRequestMetadata ? GetRequestMetadata ( JsonApiEndpoint endpoint , ResourceType primaryResourceType )
56
54
{
57
55
switch ( endpoint )
58
56
{
59
57
case JsonApiEndpoint . Post :
60
58
{
61
- return GetPostRequestMetadata ( primaryResourceType ) ;
59
+ return GetPostRequestMetadata ( primaryResourceType . ClrType ) ;
62
60
}
63
61
case JsonApiEndpoint . Patch :
64
62
{
65
- return GetPatchRequestMetadata ( primaryResourceType ) ;
63
+ return GetPatchRequestMetadata ( primaryResourceType . ClrType ) ;
66
64
}
67
65
case JsonApiEndpoint . PostRelationship :
68
66
case JsonApiEndpoint . PatchRelationship :
69
67
case JsonApiEndpoint . DeleteRelationship :
70
68
{
71
- return GetRelationshipRequestMetadata ( primaryResourceType , endpoint != JsonApiEndpoint . PatchRelationship ) ;
69
+ return GetRelationshipRequestMetadata ( primaryResourceType . Relationships , endpoint != JsonApiEndpoint . PatchRelationship ) ;
72
70
}
73
71
default :
74
72
{
@@ -77,38 +75,31 @@ public JsonApiEndpointMetadataContainer Get(MethodInfo controllerAction)
77
75
}
78
76
}
79
77
80
- private static PrimaryRequestMetadata GetPostRequestMetadata ( Type primaryResourceType )
78
+ private static PrimaryRequestMetadata GetPostRequestMetadata ( Type resourceClrType )
81
79
{
82
- Type documentType = typeof ( ResourcePostRequestDocument < > ) . MakeGenericType ( primaryResourceType ) ;
80
+ Type documentType = typeof ( ResourcePostRequestDocument < > ) . MakeGenericType ( resourceClrType ) ;
83
81
84
82
return new PrimaryRequestMetadata ( documentType ) ;
85
83
}
86
84
87
- private static PrimaryRequestMetadata GetPatchRequestMetadata ( Type primaryResourceType )
85
+ private static PrimaryRequestMetadata GetPatchRequestMetadata ( Type resourceClrType )
88
86
{
89
- Type documentType = typeof ( ResourcePatchRequestDocument < > ) . MakeGenericType ( primaryResourceType ) ;
87
+ Type documentType = typeof ( ResourcePatchRequestDocument < > ) . MakeGenericType ( resourceClrType ) ;
90
88
91
89
return new PrimaryRequestMetadata ( documentType ) ;
92
90
}
93
91
94
- private RelationshipRequestMetadata GetRelationshipRequestMetadata ( Type primaryResourceType , bool ignoreHasOneRelationships )
92
+ private RelationshipRequestMetadata GetRelationshipRequestMetadata ( IEnumerable < RelationshipAttribute > relationships , bool ignoreHasOneRelationships )
95
93
{
96
- IEnumerable < RelationshipAttribute > relationships = _resourceGraph . GetResourceType ( primaryResourceType ) . Relationships ;
94
+ IEnumerable < RelationshipAttribute > relationshipsOfEndpoint = ignoreHasOneRelationships ? relationships . OfType < HasManyAttribute > ( ) : relationships ;
97
95
98
- if ( ignoreHasOneRelationships )
99
- {
100
- relationships = relationships . OfType < HasManyAttribute > ( ) ;
101
- }
102
-
103
- IDictionary < string , Type > resourceTypesByRelationshipName = relationships . ToDictionary ( relationship => relationship . PublicName ,
104
- relationship => relationship is HasManyAttribute
105
- ? typeof ( ToManyRelationshipRequestData < > ) . MakeGenericType ( relationship . RightType . ClrType )
106
- : typeof ( ToOneRelationshipRequestData < > ) . MakeGenericType ( relationship . RightType . ClrType ) ) ;
96
+ IDictionary < string , Type > requestDocumentTypesByRelationshipName = relationshipsOfEndpoint . ToDictionary ( relationship => relationship . PublicName ,
97
+ _nonPrimaryDocumentTypeFactory . GetForRelationshipRequest ) ;
107
98
108
- return new RelationshipRequestMetadata ( resourceTypesByRelationshipName ) ;
99
+ return new RelationshipRequestMetadata ( requestDocumentTypesByRelationshipName ) ;
109
100
}
110
101
111
- private IJsonApiResponseMetadata ? GetResponseMetadata ( JsonApiEndpoint endpoint , Type primaryResourceType )
102
+ private IJsonApiResponseMetadata ? GetResponseMetadata ( JsonApiEndpoint endpoint , ResourceType primaryResourceType )
112
103
{
113
104
switch ( endpoint )
114
105
{
@@ -117,15 +108,15 @@ private RelationshipRequestMetadata GetRelationshipRequestMetadata(Type primaryR
117
108
case JsonApiEndpoint . Post :
118
109
case JsonApiEndpoint . Patch :
119
110
{
120
- return GetPrimaryResponseMetadata ( primaryResourceType , endpoint == JsonApiEndpoint . GetCollection ) ;
111
+ return GetPrimaryResponseMetadata ( primaryResourceType . ClrType , endpoint == JsonApiEndpoint . GetCollection ) ;
121
112
}
122
113
case JsonApiEndpoint . GetSecondary :
123
114
{
124
- return GetSecondaryResponseMetadata ( primaryResourceType ) ;
115
+ return GetSecondaryResponseMetadata ( primaryResourceType . Relationships ) ;
125
116
}
126
117
case JsonApiEndpoint . GetRelationship :
127
118
{
128
- return GetRelationshipResponseMetadata ( primaryResourceType ) ;
119
+ return GetRelationshipResponseMetadata ( primaryResourceType . Relationships ) ;
129
120
}
130
121
default :
131
122
{
@@ -134,44 +125,28 @@ private RelationshipRequestMetadata GetRelationshipRequestMetadata(Type primaryR
134
125
}
135
126
}
136
127
137
- private static PrimaryResponseMetadata GetPrimaryResponseMetadata ( Type primaryResourceType , bool endpointReturnsCollection )
128
+ private static PrimaryResponseMetadata GetPrimaryResponseMetadata ( Type resourceClrType , bool endpointReturnsCollection )
138
129
{
139
130
Type documentOpenType = endpointReturnsCollection ? typeof ( ResourceCollectionResponseDocument < > ) : typeof ( PrimaryResourceResponseDocument < > ) ;
140
- Type documentType = documentOpenType . MakeGenericType ( primaryResourceType ) ;
131
+ Type documentType = documentOpenType . MakeGenericType ( resourceClrType ) ;
141
132
142
133
return new PrimaryResponseMetadata ( documentType ) ;
143
134
}
144
135
145
- private SecondaryResponseMetadata GetSecondaryResponseMetadata ( Type primaryResourceType )
146
- {
147
- IDictionary < string , Type > responseTypesByRelationshipName = GetMetadataByRelationshipName ( primaryResourceType , relationship =>
148
- {
149
- Type documentType = relationship is HasManyAttribute
150
- ? typeof ( ResourceCollectionResponseDocument < > )
151
- : typeof ( SecondaryResourceResponseDocument < > ) ;
152
-
153
- return documentType . MakeGenericType ( relationship . RightType . ClrType ) ;
154
- } ) ;
155
-
156
- return new SecondaryResponseMetadata ( responseTypesByRelationshipName ) ;
157
- }
158
-
159
- private IDictionary < string , Type > GetMetadataByRelationshipName ( Type primaryResourceType ,
160
- Func < RelationshipAttribute , Type > extractRelationshipMetadataCallback )
136
+ private SecondaryResponseMetadata GetSecondaryResponseMetadata ( IEnumerable < RelationshipAttribute > relationships )
161
137
{
162
- IReadOnlyCollection < RelationshipAttribute > relationships = _resourceGraph . GetResourceType ( primaryResourceType ) . Relationships ;
138
+ IDictionary < string , Type > responseDocumentTypesByRelationshipName = relationships . ToDictionary ( relationship => relationship . PublicName ,
139
+ _nonPrimaryDocumentTypeFactory . GetForSecondaryResponse ) ;
163
140
164
- return relationships . ToDictionary ( relationship => relationship . PublicName , extractRelationshipMetadataCallback ) ;
141
+ return new SecondaryResponseMetadata ( responseDocumentTypesByRelationshipName ) ;
165
142
}
166
143
167
- private RelationshipResponseMetadata GetRelationshipResponseMetadata ( Type primaryResourceType )
144
+ private RelationshipResponseMetadata GetRelationshipResponseMetadata ( IEnumerable < RelationshipAttribute > relationships )
168
145
{
169
- IDictionary < string , Type > responseTypesByRelationshipName = GetMetadataByRelationshipName ( primaryResourceType ,
170
- relationship => relationship is HasManyAttribute
171
- ? typeof ( ResourceIdentifierCollectionResponseDocument < > ) . MakeGenericType ( relationship . RightType . ClrType )
172
- : typeof ( ResourceIdentifierResponseDocument < > ) . MakeGenericType ( relationship . RightType . ClrType ) ) ;
146
+ IDictionary < string , Type > responseDocumentTypesByRelationshipName = relationships . ToDictionary ( relationship => relationship . PublicName ,
147
+ _nonPrimaryDocumentTypeFactory . GetForRelationshipResponse ) ;
173
148
174
- return new RelationshipResponseMetadata ( responseTypesByRelationshipName ) ;
149
+ return new RelationshipResponseMetadata ( responseDocumentTypesByRelationshipName ) ;
175
150
}
176
151
}
177
152
}
0 commit comments