@@ -102,16 +102,24 @@ private static void UpdateProducesResponseTypeAttribute(ActionDescriptor endpoin
102
102
{
103
103
if ( ProducesJsonApiResponseBody ( endpoint ) )
104
104
{
105
- var producesResponse = endpoint . GetFilterMetadata < ProducesResponseTypeAttribute > ( ) ! ;
106
- producesResponse . Type = responseTypeToSet ;
105
+ var producesResponse = endpoint . GetFilterMetadata < ProducesResponseTypeAttribute > ( ) ;
106
+
107
+ if ( producesResponse != null )
108
+ {
109
+ producesResponse . Type = responseTypeToSet ;
110
+
111
+ return ;
112
+ }
107
113
}
114
+
115
+ throw new UnreachableCodeException ( ) ;
108
116
}
109
117
110
118
private static bool ProducesJsonApiResponseBody ( ActionDescriptor endpoint )
111
119
{
112
- var produces = endpoint . GetFilterMetadata < ProducesAttribute > ( ) ! ;
120
+ var produces = endpoint . GetFilterMetadata < ProducesAttribute > ( ) ;
113
121
114
- return produces . ContentTypes . Any ( contentType => contentType == HeaderConstants . MediaType ) ;
122
+ return produces != null && produces . ContentTypes . Any ( contentType => contentType == HeaderConstants . MediaType ) ;
115
123
}
116
124
117
125
private static IList < ActionDescriptor > Expand ( ActionDescriptor genericEndpoint , ExpansibleEndpointMetadata metadata ,
@@ -123,7 +131,13 @@ private static IList<ActionDescriptor> Expand(ActionDescriptor genericEndpoint,
123
131
{
124
132
ActionDescriptor expandedEndpoint = Clone ( genericEndpoint ) ;
125
133
RemovePathParameter ( expandedEndpoint . Parameters , JsonApiPathParameter . RelationshipName ) ;
126
- ExpandTemplate ( expandedEndpoint . AttributeRouteInfo ! , relationshipName ) ;
134
+
135
+ if ( expandedEndpoint . AttributeRouteInfo == null )
136
+ {
137
+ throw new NotSupportedException ( "Only attribute based routing is supported for JsonApiDotNetCore endpoints" ) ;
138
+ }
139
+
140
+ ExpandTemplate ( expandedEndpoint . AttributeRouteInfo , relationshipName ) ;
127
141
128
142
expansionCallback ( expandedEndpoint , relationshipType , relationshipName ) ;
129
143
@@ -135,7 +149,14 @@ private static IList<ActionDescriptor> Expand(ActionDescriptor genericEndpoint,
135
149
136
150
private static void UpdateBodyParameterDescriptor ( ActionDescriptor endpoint , Type bodyType , string ? parameterName = null )
137
151
{
138
- ControllerParameterDescriptor requestBodyDescriptor = endpoint . GetBodyParameterDescriptor ( ) ! ;
152
+ ControllerParameterDescriptor ? requestBodyDescriptor = endpoint . GetBodyParameterDescriptor ( ) ;
153
+
154
+ if ( requestBodyDescriptor == null )
155
+ {
156
+ // ASP.NET model binding picks up on [FromBody] in base classes, so even when it is left out in an override, this should not be reachable.
157
+ throw new UnreachableCodeException ( ) ;
158
+ }
159
+
139
160
requestBodyDescriptor . ParameterType = bodyType ;
140
161
ParameterInfo replacementParameterInfo = requestBodyDescriptor . ParameterInfo . WithParameterType ( bodyType ) ;
141
162
0 commit comments