Skip to content
This repository was archived by the owner on Aug 30, 2025. It is now read-only.

Commit fddd15f

Browse files
committed
fix: Property handler in IProblemFactory
1 parent de6ad18 commit fddd15f

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

samples/PowerUtils.AspNetCore.ErrorHandler.Samples/Controllers/ProblemFactoryController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,19 @@ public IActionResult CreateProblem()
4242
["Key100"] = new("Error114", "description fake"),
4343
["Key114"] = new("Error11124", "description 1444"),
4444
["me"] = new("ti", "111"),
45+
["MyKey"] = new("MyCode", "MyDisc")
4546
}
4647
));
48+
49+
[HttpGet("null-errors")]
50+
public IActionResult NullErrors()
51+
=> new ObjectResult(_problemFactory.CreateProblem(
52+
detail: "fake detail",
53+
instance: "fake instance",
54+
statusCode: (int)HttpStatusCode.BadRequest,
55+
title: "fake title",
56+
type: "fake type",
57+
errors: null
58+
));
4759
}
4860
}

src/ApiProblemDetailsFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,19 @@ public ErrorProblemDetails CreateProblem(
5454
IDictionary<string, ErrorDetails> errors = null
5555
)
5656
{
57+
errors ??= new Dictionary<string, ErrorDetails>();
58+
5759
var problemDetails = new ErrorProblemDetails
5860
{
5961
Status = statusCode,
6062
Title = title,
6163
Type = type,
6264
Detail = detail,
6365
Instance = instance,
64-
Errors = errors
66+
Errors = errors.ToDictionary(
67+
k => _errorHandlerOptions.Value.PropertyHandler(k.Key),
68+
v => v.Value
69+
)
6570
};
6671

6772
if(string.IsNullOrWhiteSpace(problemDetails.Detail))

tests/PowerUtils.AspNetCore.ErrorHandler.Tests/Tests/Controllers/ProblemFactoryControllerTests.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public async Task ObjectResult_CreateProblemResult_403()
5252

5353
content.ValidateContent(new Dictionary<string, ErrorDetails>()
5454
{
55-
["Key4"] = new("Error4", "description 111"),
56-
["Key14"] = new("Error124", "description 423423")
55+
["key4"] = new("Error4", "description 111"),
56+
["key14"] = new("Error124", "description 423423")
5757
});
5858
}
5959

@@ -91,10 +91,46 @@ public async Task Problem_CreateProblem_429()
9191

9292
content.ValidateContent(new Dictionary<string, ErrorDetails>()
9393
{
94-
["Key100"] = new("Error114", "description fake"),
95-
["Key114"] = new("Error11124", "description 1444"),
94+
["key100"] = new("Error114", "description fake"),
95+
["key114"] = new("Error11124", "description 1444"),
9696
["me"] = new("ti", "111"),
97+
["my_key"] = new("MyCode", "MyDisc")
9798
});
9899
}
100+
101+
[Fact]
102+
public async Task WithoutErrors_CreateProblem_400()
103+
{
104+
// Arrange
105+
var requestUri = "/problem-factory/null-errors";
106+
107+
108+
// Act
109+
(var response, var content) = await _testsFixture.Client.SendGetAsync(requestUri);
110+
111+
112+
// Assert
113+
response.ValidateResponse(HttpStatusCode.BadRequest);
114+
115+
content.Status.Should()
116+
.Be((int)HttpStatusCode.BadRequest);
117+
118+
content.Type.Should()
119+
.Be("fake type");
120+
121+
content.Title.Should()
122+
.Be("fake title");
123+
124+
content.Instance.Should()
125+
.Be("fake instance");
126+
127+
content.Detail.Should()
128+
.Be("fake detail");
129+
130+
content.TraceId.Should()
131+
.NotBeNullOrWhiteSpace();
132+
133+
content.ValidateContent(new Dictionary<string, ErrorDetails>());
134+
}
99135
}
100136
}

0 commit comments

Comments
 (0)