diff --git a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs index aed047c9ea..226b0be44d 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs @@ -214,9 +214,6 @@ namespace Microsoft.Extensions.DependencyInjection services.TryAddTransient(); services.TryAddTransient(); - // Error description - services.TryAddSingleton(); - // // ModelBinding, Validation // diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ErrorDescriptionContext.cs b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ErrorDescriptionContext.cs deleted file mode 100644 index 10370fb514..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/ErrorDescriptionContext.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.AspNetCore.Mvc.Abstractions; - -namespace Microsoft.AspNetCore.Mvc.Infrastructure -{ - public class ErrorDescriptionContext - { - public ErrorDescriptionContext(ActionDescriptor actionDescriptor) - { - ActionDescriptor = actionDescriptor; - } - - public ActionDescriptor ActionDescriptor { get; } - - public object Result { get; set; } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IErrorDescriptionFactory.cs b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IErrorDescriptionFactory.cs deleted file mode 100644 index 7cddc853ff..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IErrorDescriptionFactory.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using Microsoft.AspNetCore.Mvc.Abstractions; - -namespace Microsoft.AspNetCore.Mvc.Infrastructure -{ - /// - /// Defines a contract for creating or modifying an error response. - /// - public interface IErrorDescriptionFactory - { - object CreateErrorDescription(ActionDescriptor actionDescriptor, object result); - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IErrorDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IErrorDescriptorProvider.cs deleted file mode 100644 index 9ca38e8254..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/IErrorDescriptorProvider.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNetCore.Mvc.Infrastructure -{ - public interface IErrorDescriptorProvider - { - int Order { get; } - - void OnProvidersExecuting(ErrorDescriptionContext context); - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorOptionsSetup.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorOptionsSetup.cs index 8398b5e161..48f50f7980 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorOptionsSetup.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/ApiBehaviorOptionsSetup.cs @@ -2,19 +2,15 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using Microsoft.AspNetCore.Mvc.Infrastructure; -using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.Mvc.Internal { public class ApiBehaviorOptionsSetup : IConfigureOptions { - private readonly IErrorDescriptionFactory _errorDescriptionFactory; - public ApiBehaviorOptionsSetup(IErrorDescriptionFactory errorDescriptionFactory) + public ApiBehaviorOptionsSetup() { - _errorDescriptionFactory = errorDescriptionFactory; } public void Configure(ApiBehaviorOptions options) @@ -28,13 +24,7 @@ namespace Microsoft.AspNetCore.Mvc.Internal IActionResult GetInvalidModelStateResponse(ActionContext context) { - var errorDetails = _errorDescriptionFactory.CreateErrorDescription( - context.ActionDescriptor, - context.ModelState); - - var result = (errorDetails is ModelStateDictionary modelState) ? - new BadRequestObjectResult(modelState) : - new BadRequestObjectResult(errorDetails); + var result = new BadRequestObjectResult(context.ModelState); result.ContentTypes.Add("application/json"); result.ContentTypes.Add("application/xml"); diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultErrorDescriptorFactory.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultErrorDescriptorFactory.cs deleted file mode 100644 index 667e3199b1..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultErrorDescriptorFactory.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// 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 System.Linq; -using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.Infrastructure; - -namespace Microsoft.AspNetCore.Mvc.Internal -{ - public class DefaultErrorDescriptorFactory : IErrorDescriptionFactory - { - private readonly IErrorDescriptorProvider[] _providers; - - public DefaultErrorDescriptorFactory(IEnumerable providers) - { - if (providers == null) - { - throw new ArgumentNullException(nameof(providers)); - } - - _providers = providers.OrderBy(p => p.Order).ToArray(); - } - - public object CreateErrorDescription(ActionDescriptor actionDescriptor, object result) - { - if (actionDescriptor == null) - { - throw new ArgumentNullException(nameof(actionDescriptor)); - } - - if (result == null) - { - throw new ArgumentNullException(nameof(result)); - } - - var context = new ErrorDescriptionContext(actionDescriptor) - { - Result = result, - }; - - for (var i = 0; i < _providers.Length; i++) - { - _providers[i].OnProvidersExecuting(context); - } - - return context.Result ?? result; - } - } -} diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs index 7a1d83b9a2..bf0fc0cfa1 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/ApiBehaviorTest.cs @@ -6,7 +6,6 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; -using BasicWebSite; using BasicWebSite.Models; using Newtonsoft.Json; using Xunit; @@ -70,18 +69,10 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests State = "WA", Zip = "Invalid", }; - var expected = new[] + var expected = new Dictionary { - new VndError - { - Path = "Name", - Message = "The field Name must be a string with a minimum length of 5 and a maximum length of 30.", - }, - new VndError - { - Path = "Zip", - Message = @"The field Zip must match the regular expression '\d{5}'.", - }, + {"Name", new string[] {"The field Name must be a string with a minimum length of 5 and a maximum length of 30."}}, + {"Zip", new string[]{ @"The field Zip must match the regular expression '\d{5}'."}} }; var contactString = JsonConvert.SerializeObject(contactModel); @@ -91,14 +82,9 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.Equal("application/vnd.error+json", response.Content.Headers.ContentType.MediaType); - var actual = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); - actual = actual.OrderBy(e => e.Path).ToArray(); - Assert.Equal(expected.Length, actual.Length); - for (var i = 0; i < expected.Length; i++) - { - Assert.Equal(expected[i].Path, expected[i].Path); - Assert.Equal(expected[i].Message, expected[i].Message); - } + var content = await response.Content.ReadAsStringAsync(); + var actual = JsonConvert.DeserializeObject>(content); + Assert.Equal(expected, actual); } [Fact] diff --git a/test/WebSites/BasicWebSite/Startup.cs b/test/WebSites/BasicWebSite/Startup.cs index b313fe8dbf..9439375371 100644 --- a/test/WebSites/BasicWebSite/Startup.cs +++ b/test/WebSites/BasicWebSite/Startup.cs @@ -1,14 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.IO; using System.Linq; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; -using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.DependencyInjection; namespace BasicWebSite @@ -42,7 +38,6 @@ namespace BasicWebSite services.AddSingleton(); services.AddHttpContextAccessor(); services.AddSingleton(); - services.AddSingleton(); services.AddScoped(); } diff --git a/test/WebSites/BasicWebSite/VndError.cs b/test/WebSites/BasicWebSite/VndError.cs deleted file mode 100644 index e134062067..0000000000 --- a/test/WebSites/BasicWebSite/VndError.cs +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// 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 System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Controllers; -using Microsoft.AspNetCore.Mvc.Infrastructure; -using Microsoft.Extensions.DependencyInjection; - -namespace BasicWebSite -{ - public class VndError - { - public string LogRef { get; set; } - - public string Path { get; set; } - - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/test/WebSites/BasicWebSite/VndErrorDescriptionProvider.cs b/test/WebSites/BasicWebSite/VndErrorDescriptionProvider.cs deleted file mode 100644 index e74df13b5b..0000000000 --- a/test/WebSites/BasicWebSite/VndErrorDescriptionProvider.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// 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 System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Controllers; -using Microsoft.AspNetCore.Mvc.Infrastructure; -using Microsoft.AspNetCore.Mvc.ModelBinding; -using Microsoft.Extensions.DependencyInjection; - -namespace BasicWebSite -{ - public class VndErrorDescriptionProvider : IErrorDescriptorProvider - { - public int Order => 0; - - public void OnProvidersExecuting(ErrorDescriptionContext context) - { - if (context.ActionDescriptor.FilterDescriptors.Any(f => f.Filter is VndErrorAttribute) && - context.Result is ModelStateDictionary dictionary) - { - var vndErrors = new List(); - foreach (var item in dictionary) - { - foreach (var modelError in item.Value.Errors) - { - vndErrors.Add(new VndError - { - LogRef = modelError.ErrorMessage, - Path = item.Key, - Message = modelError.ErrorMessage, - }); - } - } - - context.Result = vndErrors; - } - } - } -}