-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Closed
Closed
Copy link
Labels
Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.This issue needs the attention of a contributor, typically because the OP has provided an update.feature-model-bindingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
[FromRoute]
does not work when used as model property, not as action parameter (see reproduction).
-
Property generates validation error ("field is required"), if omitted on request (but route value is specified).
-
Swagger generates incorrect body schema for the model - [FromRoute] propery shown as a part of the body schema.
Expected Behavior
- Parameter is mapped from route, not from the body.
- Parameter does not generate 400 Bad Request result with model validation error, when parameter is missing in body.
- Swagger gen schema generated properly - route parameter is not part of the body schema.
Steps To Reproduce
dotnet new webapi
- Create new
.cs
file and paste this code:
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
namespace Controllers;
[ApiController, Route("/Companies/{companyId}/[controller]")]
public class EmployeesController : ControllerBase
{
[HttpPost]
// [HttpPut] // Same result
// [HttpPatch] // Same result
public IActionResult CreateEmployee(CreateEmployeeDto dto)
{
return Ok(dto);
}
}
public record CreateEmployeeDto
{
[FromRoute]
// [FromRoute(Name = "companyId")] // Same result
public string CompanyId { get; init; } = null!;
// [FromBody] // Same result
// [FromForm] // Same result
[Range(1, int.MaxValue)]
public int PhoneNumber { get; init; }
}
- Open swagger and test the action. Use different values for "companyId" for route value and for body parameter (to distinguish them).
Program.cs is untouched. You may remove WeatherController as you wish.
Exceptions (if any)
No response
.NET Version
6.0.403, 7.0.100
Anything else?
VS Code.
.NET SDK:
Version: 7.0.100
Commit: e12b7af219
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19044
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.100\
Host:
Version: 7.0.0
Architecture: x64
Commit: d099f075e4
.NET SDKs installed:
6.0.403 [C:\Program Files\dotnet\sdk]
7.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
Environment variables:
Not set
global.json file:
Not found
Metadata
Metadata
Assignees
Labels
Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.This issue needs the attention of a contributor, typically because the OP has provided an update.feature-model-bindingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels