Skip to content

MVC: Correct metadata type for formdata enum parameters #61399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Mvc/Mvc.ApiExplorer/src/EndpointModelMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static Type GetDisplayType(Type type)
|| underlyingType == typeof(TimeSpan)
|| underlyingType == typeof(decimal)
|| underlyingType == typeof(Guid)
|| underlyingType == typeof(Uri) ? type : typeof(string);
|| underlyingType == typeof(Uri)
|| underlyingType.IsEnum ? type : typeof(string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Constraints;
Expand Down Expand Up @@ -235,8 +236,13 @@ public ControllerActionDescriptor CreateActionDescriptor(string methodName = nul
};
action.RouteValues.Add("controller", "Test");
action.RouteValues.Add("action", action.MethodInfo.Name);
action.ActionConstraints = [new HttpMethodActionConstraint(["GET"])];
action.EndpointMetadata = [..action.MethodInfo.GetCustomAttributes()];
action.ActionConstraints = [new HttpMethodActionConstraint(action
.EndpointMetadata
.OfType<IActionHttpMethodProvider>()
.SelectMany(a => a.HttpMethods)
.DefaultIfEmpty("GET")
)];
if (controllerType is not null)
{
foreach (var attribute in controllerType.GetCustomAttributes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,51 @@ public class Parent
public IDictionary<string, Parent> SelfReferenceDictionary { get; set; } = new Dictionary<string, Parent>();
}

/// <remarks>
/// Regression test for https://github.com/dotnet/aspnetcore/issues/61327
/// </remarks>
[Fact]
public async Task RespectsEnumDefaultValueInControllerFormParameters()
{
// Arrange
var actionDescriptor = CreateActionDescriptor(nameof(TestBodyController.FormPostWithOptionalEnumParam), typeof(TestBodyController));

// Assert
await VerifyOpenApiDocument(actionDescriptor, VerifyOptionalEnum);
}

[Fact]
public async Task RespectsEnumDefaultValueInMinimalApiFormParameters()
{
// Arrange
var builder = CreateBuilder();

// Act
builder.MapPost("/optionalEnum", ([FromForm(Name = "status")] Status status = Status.Approved) => { });

// Assert
await VerifyOpenApiDocument(builder, VerifyOptionalEnum);
}

private void VerifyOptionalEnum(OpenApiDocument document)
{
var operation = document.Paths["/optionalEnum"].Operations[OperationType.Post];
var properties = operation.RequestBody.Content["application/x-www-form-urlencoded"].Schema.Properties;
var property = properties["status"];

Assert.NotNull(property);
Assert.Equal(3, property.Enum.Count);
Assert.Equal("Approved", property.Default.GetValue<string>());
}

[ApiController]
[Produces("application/json")]
public class TestBodyController
{
[Route("/optionalEnum")]
[HttpPost]
internal Status FormPostWithOptionalEnumParam(
[FromForm(Name = "status")] Status status = Status.Approved
) => status;
}
}
Loading