1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Collections . Frozen ;
4
5
using System . ComponentModel ;
5
6
using System . ComponentModel . DataAnnotations ;
6
7
using System . Diagnostics ;
22
23
using Microsoft . Extensions . DependencyInjection ;
23
24
using Microsoft . Extensions . Hosting ;
24
25
using Microsoft . Extensions . Options ;
26
+ using Microsoft . Net . Http . Headers ;
25
27
using Microsoft . OpenApi . Models ;
26
28
27
29
namespace Microsoft . AspNetCore . OpenApi ;
@@ -47,6 +49,8 @@ internal sealed class OpenApiDocumentService(
47
49
private readonly Dictionary < string , OpenApiOperationTransformerContext > _operationTransformerContextCache = new ( ) ;
48
50
private static readonly ApiResponseType _defaultApiResponseType = new ( ) { StatusCode = StatusCodes . Status200OK } ;
49
51
52
+ private static readonly FrozenSet < string > _disallowedHeaderParameters = new [ ] { HeaderNames . Accept , HeaderNames . Authorization , HeaderNames . ContentType } . ToFrozenSet ( StringComparer . OrdinalIgnoreCase ) ;
53
+
50
54
internal bool TryGetCachedOperationTransformerContext ( string descriptionId , [ NotNullWhen ( true ) ] out OpenApiOperationTransformerContext ? context )
51
55
=> _operationTransformerContextCache . TryGetValue ( descriptionId , out context ) ;
52
56
@@ -393,9 +397,7 @@ private async Task<OpenApiResponse> GetResponseAsync(
393
397
List < OpenApiParameter > ? parameters = null ;
394
398
foreach ( var parameter in description . ParameterDescriptions )
395
399
{
396
- // Parameters that should be in the request body should not be
397
- // populated in the parameters list.
398
- if ( parameter . IsRequestBodyParameter ( ) )
400
+ if ( ShouldIgnoreParameter ( parameter ) )
399
401
{
400
402
continue ;
401
403
}
@@ -419,6 +421,24 @@ private async Task<OpenApiResponse> GetResponseAsync(
419
421
parameters . Add ( openApiParameter ) ;
420
422
}
421
423
return parameters ;
424
+
425
+ static bool ShouldIgnoreParameter ( ApiParameterDescription parameter )
426
+ {
427
+ if ( parameter . IsRequestBodyParameter ( ) )
428
+ {
429
+ // Parameters that should be in the request body should not be
430
+ // populated in the parameters list.
431
+ return true ;
432
+ }
433
+ else if ( parameter . Source == BindingSource . Header && _disallowedHeaderParameters . Contains ( parameter . Name ) )
434
+ {
435
+ // OpenAPI 3.0 states certain headers are "not allowed" to be defined as parameters.
436
+ // See https://github.com/dotnet/aspnetcore/issues/57305 for more context.
437
+ return true ;
438
+ }
439
+
440
+ return false ;
441
+ }
422
442
}
423
443
424
444
private static bool IsRequired ( ApiParameterDescription parameter )
0 commit comments