diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ValidationProblemDetails.cs b/src/Microsoft.AspNetCore.Mvc.Core/ValidationProblemDetails.cs
index da104c9a9b..56fdd49e9d 100644
--- a/src/Microsoft.AspNetCore.Mvc.Core/ValidationProblemDetails.cs
+++ b/src/Microsoft.AspNetCore.Mvc.Core/ValidationProblemDetails.cs
@@ -22,6 +22,10 @@ public ValidationProblemDetails()
Title = Resources.ValidationProblemDescription_Title;
}
+ ///
+ /// Initializes a new instance of using the specified .
+ ///
+ /// containing the validation errors.
public ValidationProblemDetails(ModelStateDictionary modelState)
: this()
{
@@ -63,7 +67,22 @@ string GetErrorMessage(ModelError error)
}
///
- /// Gets or sets the validation errors associated with this instance of .
+ /// Initializes a new instance of using the specified .
+ ///
+ /// The validation errors.
+ public ValidationProblemDetails(IDictionary errors)
+ : this()
+ {
+ if (errors == null)
+ {
+ throw new ArgumentNullException(nameof(errors));
+ }
+
+ Errors = new Dictionary(errors, StringComparer.Ordinal);
+ }
+
+ ///
+ /// Gets the validation errors associated with this instance of .
///
[JsonProperty(PropertyName = "errors")]
public IDictionary Errors { get; } = new Dictionary(StringComparer.Ordinal);
diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/ValidationProblemDetailsTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/ValidationProblemDetailsTest.cs
index af4f0d9a34..9c8f296d60 100644
--- a/test/Microsoft.AspNetCore.Mvc.Core.Test/ValidationProblemDetailsTest.cs
+++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/ValidationProblemDetailsTest.cs
@@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
+using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Xunit;
@@ -73,5 +74,34 @@ public void Constructor_SerializesErrorsFromModelStateDictionary_AddsDefaultMess
Assert.Equal(new[] { "The input was not valid." }, item.Value);
});
}
+
+ [Fact]
+ public void Constructor_CopiesPassedInDictionary()
+ {
+ // Arrange
+ var errors = new Dictionary
+ {
+ ["key1"] = new[] { "error1", "error2" },
+ ["key2"] = new[] { "error3", },
+ };
+
+ // Act
+ var problemDescription = new ValidationProblemDetails(errors);
+
+ // Assert
+ Assert.Equal("One or more validation errors occurred.", problemDescription.Title);
+ Assert.Collection(
+ problemDescription.Errors,
+ item =>
+ {
+ Assert.Equal("key1", item.Key);
+ Assert.Equal(new[] { "error1", "error2" }, item.Value);
+ },
+ item =>
+ {
+ Assert.Equal("key2", item.Key);
+ Assert.Equal(new[] { "error3" }, item.Value);
+ });
+ }
}
}