Skip to content

Let JsonException bubble through problem details in minimal APIs#66519

Open
Youssef1313 wants to merge 3 commits intomainfrom
dev/ygerges/minimal-jsonexception
Open

Let JsonException bubble through problem details in minimal APIs#66519
Youssef1313 wants to merge 3 commits intomainfrom
dev/ygerges/minimal-jsonexception

Conversation

@Youssef1313
Copy link
Copy Markdown
Member

Fixes #64139

Copilot AI review requested due to automatic review settings April 28, 2026 17:40
@Youssef1313 Youssef1313 requested review from a team, BrennanConroy and halter73 as code owners April 28, 2026 17:40
@github-actions github-actions Bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label Apr 28, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates minimal API JSON body binding to emit validation-style Problem Details when System.Text.Json throws a JsonException (e.g., missing required properties), aligning the behavior more closely with MVC/controller model binding.

Changes:

  • Write HttpValidationProblemDetails via IProblemDetailsService when JSON body deserialization throws JsonException in RequestDelegateFactory.
  • Add a functional test validating consistent Problem Details output for both a minimal endpoint and an MVC [ApiController] endpoint.
  • Extend the SimpleWebSiteWithWebApplicationBuilder test app with endpoints/models to reproduce the required property scenario.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/Mvc/test/WebSites/SimpleWebSiteWithWebApplicationBuilder/Program.cs Adds ProblemDetails services + new endpoints/model to exercise missing required property behavior.
src/Mvc/test/Mvc.FunctionalTests/SimpleWithWebApplicationBuilderTests.cs Adds a functional test asserting Problem Details JSON for both minimal + MVC endpoints.
src/Http/Http.Extensions/src/RequestDelegateFactory.cs Emits HttpValidationProblemDetails on JsonException during minimal API JSON body binding.

Comment thread src/Http/Http.Extensions/src/RequestDelegateFactory.cs Outdated
Copy link
Copy Markdown
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

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

@Youssef1313 Thank you for this contribution!

I will let the engineering team weigh in on the details of this change but I fully support the effort to solve this problem.

@Youssef1313 Youssef1313 added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions labels Apr 29, 2026
@Youssef1313
Copy link
Copy Markdown
Member Author

@BrennanConroy @halter73 @javiercn Can I get a review here please? Thanks.

Exception = ex,
ProblemDetails = new HttpValidationProblemDetails(errors)
{
Status = StatusCodes.Status400BadRequest,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While looking around, I noticed we don't set the Status on the problem details in the validation filter
https://source.dot.net/#Microsoft.AspNetCore.Routing/[ValidationEndpointFilterFactory.cs](https://source.dot.net/#Microsoft.AspNetCore.Routing/ValidationEndpointFilterFactory.cs,99),99
Should probably do that, either here or a separate change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, looks like this will eventually get applied anyways when it reaches to:

ProblemDetailsDefaults.Apply(context.ProblemDetails, httpContext.Response.StatusCode);

problemDetails.Status = statusCode;

This is assuming that the DefaultProblemDetailsWriter is used and not any other custom writer that's written and registered by the user. For the case of a custom implementation, I'm not sure yet, I'll try to come up with a test to demonstrate how the behavior can be different. I assume we will need to also have some consistency there between what MVC does and what minimal API does.

Thanks for pointing that out @BrennanConroy!

}

[Theory]
[InlineData("/post-required-minimal")]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should keep RDF tests in the RDF area https://github.com/dotnet/aspnetcore/tree/main/src/Http/Http.Extensions/test

Should also check that RDG behavior matches probably would put the test in
https://github.com/dotnet/aspnetcore/blob/main/src/Http/Http.Extensions/test/RequestDelegateGenerator/RequestDelegateCreationTests.JsonBody.cs
so it tests both RDF and RDG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make required keyword handling produce validation error in minimal APIs

4 participants