Skip to content

Fix EmptyBodyBehavior with empty content-type #38092

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

Merged
merged 3 commits into from
Nov 12, 2021
Merged

Conversation

BrennanConroy
Copy link
Member

Fixes #36466

@BrennanConroy BrennanConroy added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-model-binding labels Nov 4, 2021
Copy link
Contributor

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I think there's a new feature we can use instead of checking for Content-Length headers, we should consider updating the code to use that.

@@ -143,6 +143,11 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)

if (formatter == null)
{
if (AllowEmptyBody && httpContext.Request.ContentLength == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tratcher isn't there a better way to check for empty bodies?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -143,6 +143,11 @@ public async Task BindModelAsync(ModelBindingContext bindingContext)

if (formatter == null)
{
if (AllowEmptyBody && httpContext.Request.ContentLength == 0)
{
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might need to do bindingContext.Result = ModelBindingResult.Success(model: null);. See the comment on Line 178.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test I should write for this? I'm not sure how this is visible

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think an actual case where problematic behavior might be observable if if you have a [FromBody] property. But maybe we could just write a unit test that verify IsModelSet == true?

@BrennanConroy BrennanConroy merged commit 783adbf into main Nov 12, 2021
@BrennanConroy BrennanConroy deleted the brecon/emptyBody branch November 12, 2021 21:23
@ghost ghost added this to the 7.0-preview1 milestone Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-model-binding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EmptyBodyBehavior.Allow should allow a missing Content-Type
3 participants