Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 4015c98

Browse files
authored
Merge pull request #8664 from dotnet-maestro-bot/merge/release/2.2-to-master
[automated] Merge branch 'release/2.2' => 'master'
2 parents 13cf754 + 35d2ab3 commit 4015c98

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Microsoft.AspNetCore.Mvc.Core/ValidationProblemDetails.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public ValidationProblemDetails()
2222
Title = Resources.ValidationProblemDescription_Title;
2323
}
2424

25+
/// <summary>
26+
/// Initializes a new instance of <see cref="ValidationProblemDetails"/> using the specified <paramref name="modelState"/>.
27+
/// </summary>
28+
/// <param name="modelState"><see cref="ModelStateDictionary"/> containing the validation errors.</param>
2529
public ValidationProblemDetails(ModelStateDictionary modelState)
2630
: this()
2731
{
@@ -63,7 +67,22 @@ string GetErrorMessage(ModelError error)
6367
}
6468

6569
/// <summary>
66-
/// Gets or sets the validation errors associated with this instance of <see cref="ValidationProblemDetails"/>.
70+
/// Initializes a new instance of <see cref="ValidationProblemDetails"/> using the specified <paramref name="errors"/>.
71+
/// </summary>
72+
/// <param name="errors">The validation errors.</param>
73+
public ValidationProblemDetails(IDictionary<string, string[]> errors)
74+
: this()
75+
{
76+
if (errors == null)
77+
{
78+
throw new ArgumentNullException(nameof(errors));
79+
}
80+
81+
Errors = new Dictionary<string, string[]>(errors, StringComparer.Ordinal);
82+
}
83+
84+
/// <summary>
85+
/// Gets the validation errors associated with this instance of <see cref="ValidationProblemDetails"/>.
6786
/// </summary>
6887
[JsonProperty(PropertyName = "errors")]
6988
public IDictionary<string, string[]> Errors { get; } = new Dictionary<string, string[]>(StringComparer.Ordinal);

test/Microsoft.AspNetCore.Mvc.Core.Test/ValidationProblemDetailsTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using Microsoft.AspNetCore.Mvc.ModelBinding;
67
using Xunit;
78

@@ -73,5 +74,34 @@ public void Constructor_SerializesErrorsFromModelStateDictionary_AddsDefaultMess
7374
Assert.Equal(new[] { "The input was not valid." }, item.Value);
7475
});
7576
}
77+
78+
[Fact]
79+
public void Constructor_CopiesPassedInDictionary()
80+
{
81+
// Arrange
82+
var errors = new Dictionary<string, string[]>
83+
{
84+
["key1"] = new[] { "error1", "error2" },
85+
["key2"] = new[] { "error3", },
86+
};
87+
88+
// Act
89+
var problemDescription = new ValidationProblemDetails(errors);
90+
91+
// Assert
92+
Assert.Equal("One or more validation errors occurred.", problemDescription.Title);
93+
Assert.Collection(
94+
problemDescription.Errors,
95+
item =>
96+
{
97+
Assert.Equal("key1", item.Key);
98+
Assert.Equal(new[] { "error1", "error2" }, item.Value);
99+
},
100+
item =>
101+
{
102+
Assert.Equal("key2", item.Key);
103+
Assert.Equal(new[] { "error3" }, item.Value);
104+
});
105+
}
76106
}
77107
}

0 commit comments

Comments
 (0)