Skip to content

Avoid using invalid content type for ValidationProblemDetails #36617

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 1 commit into from
Sep 16, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public static DelegateEndpointConventionBuilder ProducesProblem(this DelegateEnd
/// </summary>
/// <param name="builder">The <see cref="DelegateEndpointConventionBuilder"/>.</param>
/// <param name="statusCode">The response status code. Defaults to StatusCodes.Status400BadRequest.</param>
/// <param name="contentType">The response content type. Defaults to "application/validationproblem+json".</param>
/// <param name="contentType">The response content type. Defaults to "application/problem+json".</param>
/// <returns>A <see cref="DelegateEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
public static DelegateEndpointConventionBuilder ProducesValidationProblem(this DelegateEndpointConventionBuilder builder,
int statusCode = StatusCodes.Status400BadRequest,
string? contentType = null)
{
if (string.IsNullOrEmpty(contentType))
{
contentType = "application/validationproblem+json";
contentType = "application/problem+json";
Copy link
Contributor

Choose a reason for hiding this comment

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

Is MVC also affected by this? Could we also fix it while we're addressing this?

Copy link
Member Author

@captainsafia captainsafia Sep 16, 2021

Choose a reason for hiding this comment

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

AFAICT, MVC isn't affected by this. The ControllerBase.ValidationProblem method uses the application/problem+json content type and the ProducesResponseType attribute expects that users pass the content type for when they specify a typeof(ValidationProblemDetails) hence the fix for the test case below.

}

return Produces<HttpValidationProblemDetails>(builder, statusCode, contentType);
Expand Down
4 changes: 2 additions & 2 deletions src/Mvc/Mvc.ApiExplorer/test/ApiResponseTypeProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ public void GetApiResponseTypes_HandlesActionWithMultipleContentTypesAndProduces
// Arrange
var actionDescriptor = GetControllerActionDescriptor(typeof(TestController), nameof(TestController.GetUser));
actionDescriptor.FilterDescriptors.Add(new FilterDescriptor(new ProducesAttribute("text/xml") { Type = typeof(BaseModel) }, FilterScope.Action));
actionDescriptor.FilterDescriptors.Add(new FilterDescriptor(new ProducesResponseTypeAttribute(typeof(ValidationProblemDetails), 400, "application/validationproblem+json"), FilterScope.Action));
actionDescriptor.FilterDescriptors.Add(new FilterDescriptor(new ProducesResponseTypeAttribute(typeof(ValidationProblemDetails), 400, "application/problem+json"), FilterScope.Action));
actionDescriptor.FilterDescriptors.Add(new FilterDescriptor(new ProducesResponseTypeAttribute(typeof(ProblemDetails), 404, "application/problem+json"), FilterScope.Action));
actionDescriptor.FilterDescriptors.Add(new FilterDescriptor(new ProducesResponseTypeAttribute(409), FilterScope.Action));

Expand All @@ -738,7 +738,7 @@ public void GetApiResponseTypes_HandlesActionWithMultipleContentTypesAndProduces
{
Assert.Equal(typeof(ValidationProblemDetails), responseType.Type);
Assert.Equal(400, responseType.StatusCode);
Assert.Equal(new[] { "application/validationproblem+json" }, GetSortedMediaTypes(responseType));
Assert.Equal(new[] { "application/problem+json" }, GetSortedMediaTypes(responseType));
},
responseType =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public void HandlesProducesWithProducesProblem()
{
Assert.Equal(typeof(HttpValidationProblemDetails), responseType.Type);
Assert.Equal(400, responseType.StatusCode);
Assert.Equal(new[] { "application/validationproblem+json" }, GetSortedMediaTypes(responseType));
Assert.Equal(new[] { "application/problem+json" }, GetSortedMediaTypes(responseType));
},
responseType =>
{
Expand Down