-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Optional [FromBody] Model Binding #6878
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
Comments
Setting MvcOptions.AllowEmptyInputInBodyModelBinding should continue to work for this scenario. Are you seeing otherwise? |
@pranavkm On a per-endpoint basis similar to the example, not globally for the entire web application. |
@douglasg14b there isn't a feature to enable this on a per endpoint basis. One option might be to remove \ ignore specific that model validation error in your action. |
I suppose this is a feature request then! I can work around this for now, but it's a bit awkward. |
I would also like to see a per-request option as mentioned by @hmvs here aspnet/Mvc#6920 (comment) |
Thanks for contacting us, @douglasg14b. |
Why close(+wontfix) this issue if it's just a matter of priority? There is no reasonably scoped workaround available and this problem won't go away in the foreseeable future. |
@mkArtakMSFT Why close as If it's been decided that this will never be a feature in Asp.Net Core, that should probably be documented somewhere for clarification. |
@pentp the reason is that keeping issues like this around just keeps accumulating our backlog without any good reason. If this is really something which community will benefit from a lot, we will hear similar feedback in the future again and we can reconsider then. There's just no point in accumulating backlog as it becomes unmanageable. @douglasg14b I agree this is not really a "won't fix". It's a matter of priority and given our current priorities this doesn't and won't hit the bar in the near future. |
Not to sound snide, but that sounds like your team needs an "On Hold" queue. It's what we use to manage items that won't be considered for a long time, or are blocked by a number of factors and need to sit for months. |
Why is the proposed solution of using the |
By the way, here is a working sample @pranavkm has put together with the proposed alternative: https://github.com/pranavkm/OptionalBodyBinding |
@douglasg14b if you're willing to send a PR for this, we'll happily consider it. |
I have to agree, even if this is low priority I hate to see this simply be closed because that means it completely leaves the radar. |
I thought the scoping of the two parts of the workaround would mismatch, but actually it looks fine. The actual filter probably needs some tuning to avoid excessive reflection. |
My team experienced this same issue and we are also interested in a per-endpoint solution. public class SomeClassModelBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
var stream = bindingContext.HttpContext.Request.Body;
string body;
using (var reader = new StreamReader(stream))
{
body = await reader.ReadToEndAsync();
}
var someClass = JsonConvert.DeserializeObject<SomeClass>(body);
bindingContext.Result = ModelBindingResult.Success(someClass);
}
}
[FromBody, ModelBinder(BinderType = typeof(SomeClassModelBinder))] SomeClass request, |
This is a 7th? revision of an MVC framework for .net and there's still workarounds required for common scenarios. This IMO violates the principle of least surprise. If object is declared nullable, doesn't that imply optional? Another unfortunate side-effect is that it produces the ugly validation problem response:
|
* Add support for optional FromBody parameters Fixes #6878 * Fixup nullable * Changes per API review
* Add support for optional FromBody parameters Fixes #6878 * Fixup nullable * Changes per API review
This should be addressed in 5.0-preview7. FromBody attributes accepts a property that allows configuring individual parameters to be optional: public IActionResult Post([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] MyModel model)
... |
Is your feature request related to a problem? Please describe.
I am trying to use optional
[FromBody]
model binding on a per-endpoint basis.Example:
An empty request body results in the following being returned:
Describe the solution you'd like
For properties that have default value ie
[FromBody] bool expandRelations = true
to not be required in the body of the request.Additional context
Similar issue here: aspnet/Mvc#6920
The text was updated successfully, but these errors were encountered: