Skip to content

[release/8.0] Fix casing of ProblemDetails for RFC compliance #53792

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 2 commits into from
Feb 7, 2024
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
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;

namespace Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -36,5 +37,6 @@ private HttpValidationProblemDetails(Dictionary<string, string[]> errors)
/// <summary>
/// Gets the validation errors associated with this instance of <see cref="HttpValidationProblemDetails"/>.
/// </summary>
[JsonPropertyName("errors")]
public IDictionary<string, string[]> Errors { get; set; } = new Dictionary<string, string[]>(StringComparer.Ordinal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ProblemDetails
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyOrder(-5)]
[JsonPropertyName("type")]
public string? Type { get; set; }

/// <summary>
Expand All @@ -27,27 +28,31 @@ public class ProblemDetails
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyOrder(-4)]
[JsonPropertyName("title")]
public string? Title { get; set; }

/// <summary>
/// The HTTP status code([RFC7231], Section 6) generated by the origin server for this occurrence of the problem.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyOrder(-3)]
[JsonPropertyName("status")]
public int? Status { get; set; }

/// <summary>
/// A human-readable explanation specific to this occurrence of the problem.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyOrder(-2)]
[JsonPropertyName("detail")]
public string? Detail { get; set; }

/// <summary>
/// A URI reference that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyOrder(-1)]
[JsonPropertyName("instance")]
public string? Instance { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,68 @@ public async Task WriteAsync_Works()
Assert.Equal(expectedProblem.Instance, problemDetails.Instance);
}

[Fact]
public async Task WriteAsync_Works_ProperCasing()
{
// Arrange
var writer = GetWriter();
var stream = new MemoryStream();
var context = CreateContext(stream);
var expectedProblem = new ProblemDetails()
{
Detail = "Custom Bad Request",
Instance = "Custom Bad Request",
Status = StatusCodes.Status400BadRequest,
Type = "https://tools.ietf.org/html/rfc9110#section-15.5.1-custom",
Title = "Custom Bad Request",
Extensions = new Dictionary<string, object>() { { "extensionKey", 1 } }
};
var problemDetailsContext = new ProblemDetailsContext()
{
HttpContext = context,
ProblemDetails = expectedProblem
};

//Act
await writer.WriteAsync(problemDetailsContext);

//Assert
stream.Position = 0;
var result = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(stream, JsonSerializerOptions.Default);
Assert.Equal(result.Keys, new(new() { { "type", 0 }, { "title", 1 }, { "status", 2 }, { "detail", 3 }, { "instance", 4 }, { "extensionKey", 5 } }));
}

[Fact]
public async Task WriteAsync_Works_ProperCasing_ValidationProblemDetails()
{
// Arrange
var writer = GetWriter();
var stream = new MemoryStream();
var context = CreateContext(stream);
var expectedProblem = new ValidationProblemDetails()
{
Detail = "Custom Bad Request",
Instance = "Custom Bad Request",
Status = StatusCodes.Status400BadRequest,
Type = "https://tools.ietf.org/html/rfc9110#section-15.5.1-custom",
Title = "Custom Bad Request",
Errors = new Dictionary<string, string[]>() { { "name", ["Name is invalid."] } }
};
var problemDetailsContext = new ProblemDetailsContext()
{
HttpContext = context,
ProblemDetails = expectedProblem
};

//Act
await writer.WriteAsync(problemDetailsContext);

//Assert
stream.Position = 0;
var result = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(stream, JsonSerializerOptions.Default);
Assert.Equal(result.Keys, new(new() { { "type", 0 }, { "title", 1 }, { "status", 2 }, { "detail", 3 }, { "instance", 4 }, { "errors", 5 } }));
}

[Fact]
public async Task WriteAsync_Works_WhenReplacingProblemDetailsUsingSetter()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/Mvc.Core/src/ValidationProblemDetails.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.ModelBinding;
Expand Down Expand Up @@ -81,5 +82,6 @@ public ValidationProblemDetails(IDictionary<string, string[]> errors)
/// <summary>
/// Gets the validation errors associated with this instance of <see cref="HttpValidationProblemDetails"/>.
/// </summary>
[JsonPropertyName("errors")]
public new IDictionary<string, string[]> Errors { get { return base.Errors; } set { base.Errors = value; } }
}