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