@@ -102,8 +102,6 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
102
102
} ,
103
103
} ;
104
104
105
- var hasJsonBody = false ;
106
-
107
105
foreach ( var parameter in methodInfo . GetParameters ( ) )
108
106
{
109
107
var parameterDescription = CreateApiParameterDescription ( parameter , routeEndpoint . RoutePattern ) ;
@@ -113,33 +111,37 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
113
111
continue ;
114
112
}
115
113
116
- if ( parameterDescription . Source == BindingSource . Body )
117
- {
118
- hasJsonBody = true ;
119
- }
120
-
121
114
apiDescription . ParameterDescriptions . Add ( parameterDescription ) ;
122
115
}
123
116
124
- // Get custom attributes for the handler. ConsumesAttribute is one of the examples .
125
- var acceptsRequestType = routeEndpoint . Metadata . GetMetadata < IAcceptsMetadata > ( ) ? . RequestType ;
126
- if ( acceptsRequestType is not null )
117
+ // Get IAcceptsMetadata .
118
+ var acceptsMetadata = routeEndpoint . Metadata . GetMetadata < IAcceptsMetadata > ( ) ;
119
+ if ( acceptsMetadata is not null )
127
120
{
121
+ var acceptsRequestType = acceptsMetadata . RequestType ;
122
+ var isOptional = acceptsMetadata . IsOptional ;
128
123
var parameterDescription = new ApiParameterDescription
129
124
{
130
- Name = acceptsRequestType . Name ,
131
- ModelMetadata = CreateModelMetadata ( acceptsRequestType ) ,
125
+ Name = acceptsRequestType is not null ? acceptsRequestType . Name : typeof ( void ) . Name ,
126
+ ModelMetadata = CreateModelMetadata ( acceptsRequestType ?? typeof ( void ) ) ,
132
127
Source = BindingSource . Body ,
133
- Type = acceptsRequestType ,
134
- IsRequired = true ,
128
+ Type = acceptsRequestType ?? typeof ( void ) ,
129
+ IsRequired = ! isOptional ,
135
130
} ;
136
-
137
131
apiDescription . ParameterDescriptions . Add ( parameterDescription ) ;
132
+
133
+ var supportedRequestFormats = apiDescription . SupportedRequestFormats ;
134
+
135
+ foreach ( var contentType in acceptsMetadata . ContentTypes )
136
+ {
137
+ supportedRequestFormats . Add ( new ApiRequestFormat
138
+ {
139
+ MediaType = contentType
140
+ } ) ;
141
+ }
138
142
}
139
143
140
- AddSupportedRequestFormats ( apiDescription . SupportedRequestFormats , hasJsonBody , routeEndpoint . Metadata ) ;
141
144
AddSupportedResponseTypes ( apiDescription . SupportedResponseTypes , methodInfo . ReturnType , routeEndpoint . Metadata ) ;
142
-
143
145
AddActionDescriptorEndpointMetadata ( apiDescription . ActionDescriptor , routeEndpoint . Metadata ) ;
144
146
145
147
return apiDescription ;
@@ -150,7 +152,8 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
150
152
var ( source , name , allowEmpty ) = GetBindingSourceAndName ( parameter , pattern ) ;
151
153
152
154
// Services are ignored because they are not request parameters.
153
- if ( source == BindingSource . Services )
155
+ // We ignore/skip body parameter because the value will be retrieved from the IAcceptsMetadata.
156
+ if ( source == BindingSource . Services || source == BindingSource . Body )
154
157
{
155
158
return null ;
156
159
}
@@ -222,33 +225,6 @@ private ApiDescription CreateApiDescription(RouteEndpoint routeEndpoint, string
222
225
}
223
226
}
224
227
225
- private static void AddSupportedRequestFormats (
226
- IList < ApiRequestFormat > supportedRequestFormats ,
227
- bool hasJsonBody ,
228
- EndpointMetadataCollection endpointMetadata )
229
- {
230
- var requestMetadata = endpointMetadata . GetOrderedMetadata < IApiRequestMetadataProvider > ( ) ;
231
- var declaredContentTypes = DefaultApiDescriptionProvider . GetDeclaredContentTypes ( requestMetadata ) ;
232
-
233
- if ( declaredContentTypes . Count > 0 )
234
- {
235
- foreach ( var contentType in declaredContentTypes )
236
- {
237
- supportedRequestFormats . Add ( new ApiRequestFormat
238
- {
239
- MediaType = contentType ,
240
- } ) ;
241
- }
242
- }
243
- else if ( hasJsonBody )
244
- {
245
- supportedRequestFormats . Add ( new ApiRequestFormat
246
- {
247
- MediaType = "application/json" ,
248
- } ) ;
249
- }
250
- }
251
-
252
228
private static void AddSupportedResponseTypes (
253
229
IList < ApiResponseType > supportedResponseTypes ,
254
230
Type returnType ,
0 commit comments