11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Collections . Frozen ;
45using System . ComponentModel ;
56using System . ComponentModel . DataAnnotations ;
67using System . Diagnostics ;
2223using Microsoft . Extensions . DependencyInjection ;
2324using Microsoft . Extensions . Hosting ;
2425using Microsoft . Extensions . Options ;
26+ using Microsoft . Net . Http . Headers ;
2527using Microsoft . OpenApi . Models ;
2628
2729namespace Microsoft . AspNetCore . OpenApi ;
@@ -47,6 +49,8 @@ internal sealed class OpenApiDocumentService(
4749 private readonly Dictionary < string , OpenApiOperationTransformerContext > _operationTransformerContextCache = new ( ) ;
4850 private static readonly ApiResponseType _defaultApiResponseType = new ( ) { StatusCode = StatusCodes . Status200OK } ;
4951
52+ private static readonly FrozenSet < string > _disallowedHeaderParameters = new [ ] { HeaderNames . Accept , HeaderNames . Authorization , HeaderNames . ContentType } . ToFrozenSet ( StringComparer . OrdinalIgnoreCase ) ;
53+
5054 internal bool TryGetCachedOperationTransformerContext ( string descriptionId , [ NotNullWhen ( true ) ] out OpenApiOperationTransformerContext ? context )
5155 => _operationTransformerContextCache . TryGetValue ( descriptionId , out context ) ;
5256
@@ -393,9 +397,7 @@ private async Task<OpenApiResponse> GetResponseAsync(
393397 List < OpenApiParameter > ? parameters = null ;
394398 foreach ( var parameter in description . ParameterDescriptions )
395399 {
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 ) )
399401 {
400402 continue ;
401403 }
@@ -419,6 +421,24 @@ private async Task<OpenApiResponse> GetResponseAsync(
419421 parameters . Add ( openApiParameter ) ;
420422 }
421423 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+ }
422442 }
423443
424444 private static bool IsRequired ( ApiParameterDescription parameter )
0 commit comments