Skip to content

Commit 09561bb

Browse files
Fix NotSupportedException in AoT project
Fix `NotSupportedException` being thrown with .NET 9 when accessing `ModelMetadata.ElementType` in a native AoT application due to `Microsoft.AspNetCore.Mvc.ApiExplorer.IsEnhancedModelMetadataSupported` being disabled.
1 parent 00890c9 commit 09561bb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiParameterDescriptionExtensions.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,17 @@ internal static bool IsFromBody(this ApiParameterDescription apiParameter)
113113

114114
internal static bool IsFromForm(this ApiParameterDescription apiParameter)
115115
{
116+
bool isEnhancedModelMetadataSupported = true;
117+
118+
#if NET9_0_OR_GREATER
119+
if (AppContext.TryGetSwitch("Microsoft.AspNetCore.Mvc.ApiExplorer.IsEnhancedModelMetadataSupported", out var isEnabled))
120+
{
121+
isEnhancedModelMetadataSupported = isEnabled;
122+
}
123+
#endif
124+
116125
var source = apiParameter.Source;
117-
var elementType = apiParameter.ModelMetadata?.ElementType;
126+
var elementType = isEnhancedModelMetadataSupported ? apiParameter.ModelMetadata?.ElementType : null;
118127

119128
return (source == BindingSource.Form || source == BindingSource.FormFile)
120129
|| (elementType != null && typeof(IFormFile).IsAssignableFrom(elementType));

0 commit comments

Comments
 (0)