From bf61ce2b8f21959e56d82a9ec519f2effed5aa75 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Fri, 29 Dec 2017 13:12:01 -0800 Subject: [PATCH] Document compatiblity switch for input formatter exceptions --- .../IInputFormatterExceptionPolicy.cs | 4 +- ...InputFormatterExceptionModelStatePolicy.cs | 11 -- .../InputFormatterExceptionPolicy.cs | 52 ++++++++ ...MvcOptionsConfigureCompatibilityOptions.cs | 2 +- .../ModelBinding/Binders/BodyModelBinder.cs | 4 +- .../MvcOptions.cs | 55 ++++++--- .../JsonInputFormatter.cs | 6 +- .../JsonPatchInputFormatter.cs | 6 +- ...XmlDataContractSerializerInputFormatter.cs | 6 +- .../XmlSerializerInputFormatter.cs | 6 +- .../Binders/BodyModelBinderTests.cs | 112 +++++++++--------- .../CompatibilitySwitchIntegrationTest.cs | 6 +- 12 files changed, 165 insertions(+), 105 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionModelStatePolicy.cs create mode 100644 src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionPolicy.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/IInputFormatterExceptionPolicy.cs b/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/IInputFormatterExceptionPolicy.cs index 2c5694b4c6..c2032c0e05 100644 --- a/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/IInputFormatterExceptionPolicy.cs +++ b/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/IInputFormatterExceptionPolicy.cs @@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters /// /// A policy which s can implement to indicate if they want the body model binder /// to handle all exceptions. By default, all default s implement this interface and - /// have a default value of . + /// have a default value of . /// public interface IInputFormatterExceptionPolicy { @@ -14,6 +14,6 @@ namespace Microsoft.AspNetCore.Mvc.Formatters /// Gets the flag to indicate if the body model binder should handle all exceptions. If an exception is handled, /// the body model binder converts the exception into model state errors, else the exception is allowed to propagate. /// - InputFormatterExceptionModelStatePolicy ExceptionPolicy { get; } + InputFormatterExceptionPolicy ExceptionPolicy { get; } } } diff --git a/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionModelStatePolicy.cs b/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionModelStatePolicy.cs deleted file mode 100644 index 3daf6438e0..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionModelStatePolicy.cs +++ /dev/null @@ -1,11 +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.Formatters -{ - public enum InputFormatterExceptionModelStatePolicy - { - AllExceptions = 0, - MalformedInputExceptions = 1, - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionPolicy.cs b/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionPolicy.cs new file mode 100644 index 0000000000..be64d4d3ac --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Abstractions/Formatters/InputFormatterExceptionPolicy.cs @@ -0,0 +1,52 @@ +// 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; + +namespace Microsoft.AspNetCore.Mvc.Formatters +{ + /// + /// Defines the set of policies that determine how the model binding system interprets exceptions + /// thrown by an . Applications should set + /// MvcOptions.InputFormatterExceptionPolicy to configure this setting. + /// + /// + /// + /// An could throw an exception for several reasons, including: + /// + /// malformed input + /// client disconnect or other I/O problem + /// + /// application configuration problems such as + /// + /// + /// + /// + /// The policy associated with treats + /// all such categories of problems as model state errors, and usually will be reported to the client as + /// an HTTP 400. This was the only policy supported by model binding in ASP.NET Core MVC 1.0, 1.1, and 2.0 + /// and is still the default for historical reasons. + /// + /// + /// The policy associated with + /// treats only and its subclasses as model state errors. This means that + /// exceptions that are not related to the content of the HTTP request (such as a disconnect) will be rethrown, + /// which by default would cause an HTTP 500 response, unless there is exception-handling middleware enabled. + /// + /// + public enum InputFormatterExceptionPolicy + { + /// + /// This value indicates that all exceptions thrown by an will be treated + /// as model state errors. + /// + AllExceptions = 0, + + /// + /// This value indicates that only and subclasses will be treated + /// as model state errors. All other exceptions types will be rethrown and can be handled by a higher + /// level exception handler, such as exception-handling middleware. + /// + MalformedInputExceptions = 1, + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/MvcOptionsConfigureCompatibilityOptions.cs b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/MvcOptionsConfigureCompatibilityOptions.cs index 0121ac86d0..2f26e761ab 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/MvcOptionsConfigureCompatibilityOptions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/MvcOptionsConfigureCompatibilityOptions.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure if (Version >= CompatibilityVersion.Version_2_1) { values[nameof(MvcOptions.SuppressBindingUndefinedValueToEnumType)] = true; - values[nameof(MvcOptions.InputFormatterExceptionModelStatePolicy)] = InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + values[nameof(MvcOptions.InputFormatterExceptionPolicy)] = InputFormatterExceptionPolicy.MalformedInputExceptions; } return values; diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/BodyModelBinder.cs b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/BodyModelBinder.cs index 9d22fd0de3..babd08ce7c 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/BodyModelBinder.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/BodyModelBinder.cs @@ -185,7 +185,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders private bool ShouldHandleException(IInputFormatter formatter) { - var policy = _options.InputFormatterExceptionModelStatePolicy; + var policy = _options.InputFormatterExceptionPolicy; // Any explicit policy on the formatters takes precedence over the global policy on MvcOptions if (formatter is IInputFormatterExceptionPolicy exceptionPolicy) @@ -193,7 +193,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders policy = exceptionPolicy.ExceptionPolicy; } - return policy == InputFormatterExceptionModelStatePolicy.AllExceptions; + return policy == InputFormatterExceptionPolicy.AllExceptions; } } } diff --git a/src/Microsoft.AspNetCore.Mvc.Core/MvcOptions.cs b/src/Microsoft.AspNetCore.Mvc.Core/MvcOptions.cs index 6ae6d5edcf..4e4ca3188a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/MvcOptions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/MvcOptions.cs @@ -22,7 +22,7 @@ namespace Microsoft.AspNetCore.Mvc private int _maxModelStateErrors = ModelStateDictionary.DefaultMaxAllowedErrors; // See CompatibilitySwitch.cs for guide on how to implement these. - private readonly CompatibilitySwitch _inputFormatterExceptionModelStatePolicy; + private readonly CompatibilitySwitch _inputFormatterExceptionPolicy; private readonly CompatibilitySwitch _suppressBindingUndefinedValueToEnumType; private readonly CompatibilitySwitch _suppressJsonDeserializationExceptionMessagesInModelState; private readonly ICompatibilitySwitch[] _switches; @@ -41,12 +41,12 @@ namespace Microsoft.AspNetCore.Mvc ModelValidatorProviders = new List(); ValueProviderFactories = new List(); - _inputFormatterExceptionModelStatePolicy = new CompatibilitySwitch(nameof(InputFormatterExceptionModelStatePolicy), InputFormatterExceptionModelStatePolicy.AllExceptions); + _inputFormatterExceptionPolicy = new CompatibilitySwitch(nameof(InputFormatterExceptionPolicy), InputFormatterExceptionPolicy.AllExceptions); _suppressBindingUndefinedValueToEnumType = new CompatibilitySwitch(nameof(SuppressBindingUndefinedValueToEnumType)); _suppressJsonDeserializationExceptionMessagesInModelState = new CompatibilitySwitch(nameof(SuppressJsonDeserializationExceptionMessagesInModelState)); _switches = new ICompatibilitySwitch[] { - _inputFormatterExceptionModelStatePolicy, + _inputFormatterExceptionPolicy, _suppressBindingUndefinedValueToEnumType, _suppressJsonDeserializationExceptionMessagesInModelState, }; @@ -87,6 +87,37 @@ namespace Microsoft.AspNetCore.Mvc /// public FormatterMappings FormatterMappings { get; } + /// + /// Gets or sets a value which determines how the model binding system interprets exceptions thrown by an . + /// The default value of the property is . + /// + /// + /// + /// This property is associated with a compatibility switch and can provide a different behavior depending on + /// the configured compatibility version for the application. See for + /// guidance and examples of setting the application's compatibility version. + /// + /// + /// Configuring the desired value of the compatibility switch by calling this property's setter will take precedence + /// over the value implied by the application's . + /// + /// + /// If the application's compatibility version is set to then + /// this setting will have the value if + /// not explicitly configured. + /// + /// + /// If the application's compatibility version is set to or + /// higher then this setting will have the value + /// if not explicitly configured. + /// + /// + public InputFormatterExceptionPolicy InputFormatterExceptionPolicy + { + get => _inputFormatterExceptionPolicy.Value; + set => _inputFormatterExceptionPolicy.Value = value; + } + /// /// Gets a list of s that are used by this application. /// @@ -103,16 +134,16 @@ namespace Microsoft.AspNetCore.Mvc /// guidance and examples of setting the application's compatibility version. /// /// - /// Configuring the desired of the value compatibility switch by calling this property's setter will take precedence + /// Configuring the desired value of the compatibility switch by calling this property's setter will take precedence /// over the value implied by the application's . /// /// /// If the application's compatibility version is set to then - /// this setting will have value false if not explicitly configured. + /// this setting will have the value false if not explicitly configured. /// /// /// If the application's compatibility version is set to or - /// higher then this setting will have value true if not explicitly configured. + /// higher then this setting will have the value true if not explicitly configured. /// /// public bool SuppressBindingUndefinedValueToEnumType @@ -211,18 +242,6 @@ namespace Microsoft.AspNetCore.Mvc public bool RequireHttpsPermanent { get; set; } - /// - /// Gets or sets the option to determine if model binding should convert all exceptions (including ones not related to bad input) - /// that occur during deserialization in s into model state errors. - /// This option applies only to custom s. - /// Default is . - /// - public InputFormatterExceptionModelStatePolicy InputFormatterExceptionModelStatePolicy - { - get => _inputFormatterExceptionModelStatePolicy.Value; - set => _inputFormatterExceptionModelStatePolicy.Value = value; - } - /// /// Gets or sets a flag to determine whether, if an action receives invalid JSON in /// the request body, the JSON deserialization exception message should be replaced diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs index 2500296a6a..bb1c251186 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonInputFormatter.cs @@ -191,15 +191,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters } /// - public virtual InputFormatterExceptionModelStatePolicy ExceptionPolicy + public virtual InputFormatterExceptionPolicy ExceptionPolicy { get { if (GetType() == typeof(JsonInputFormatter)) { - return InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + return InputFormatterExceptionPolicy.MalformedInputExceptions; } - return InputFormatterExceptionModelStatePolicy.AllExceptions; + return InputFormatterExceptionPolicy.AllExceptions; } } diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonPatchInputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonPatchInputFormatter.cs index 6e704e8348..58c5b3a4f3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonPatchInputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonPatchInputFormatter.cs @@ -119,15 +119,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters } /// - public override InputFormatterExceptionModelStatePolicy ExceptionPolicy + public override InputFormatterExceptionPolicy ExceptionPolicy { get { if (GetType() == typeof(JsonPatchInputFormatter)) { - return InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + return InputFormatterExceptionPolicy.MalformedInputExceptions; } - return InputFormatterExceptionModelStatePolicy.AllExceptions; + return InputFormatterExceptionPolicy.AllExceptions; } } diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerInputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerInputFormatter.cs index 81f8c92873..204e2a661f 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerInputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlDataContractSerializerInputFormatter.cs @@ -113,15 +113,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters } /// - public virtual InputFormatterExceptionModelStatePolicy ExceptionPolicy + public virtual InputFormatterExceptionPolicy ExceptionPolicy { get { if (GetType() == typeof(XmlDataContractSerializerInputFormatter)) { - return InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + return InputFormatterExceptionPolicy.MalformedInputExceptions; } - return InputFormatterExceptionModelStatePolicy.AllExceptions; + return InputFormatterExceptionPolicy.AllExceptions; } } diff --git a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlSerializerInputFormatter.cs b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlSerializerInputFormatter.cs index 56980ea1ba..b4ada28b05 100644 --- a/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlSerializerInputFormatter.cs +++ b/src/Microsoft.AspNetCore.Mvc.Formatters.Xml/XmlSerializerInputFormatter.cs @@ -92,15 +92,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters public XmlDictionaryReaderQuotas XmlDictionaryReaderQuotas => _readerQuotas; /// - public virtual InputFormatterExceptionModelStatePolicy ExceptionPolicy + public virtual InputFormatterExceptionPolicy ExceptionPolicy { get { if (GetType() == typeof(XmlSerializerInputFormatter)) { - return InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + return InputFormatterExceptionPolicy.MalformedInputExceptions; } - return InputFormatterExceptionModelStatePolicy.AllExceptions; + return InputFormatterExceptionPolicy.AllExceptions; } } diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/Binders/BodyModelBinderTests.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/Binders/BodyModelBinderTests.cs index 79875934f4..10e1641f26 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/Binders/BodyModelBinderTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/ModelBinding/Binders/BodyModelBinderTests.cs @@ -196,10 +196,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders // Throwing InputFormatterException [Theory] - [InlineData(InputFormatterExceptionModelStatePolicy.AllExceptions)] - [InlineData(InputFormatterExceptionModelStatePolicy.MalformedInputExceptions)] + [InlineData(InputFormatterExceptionPolicy.AllExceptions)] + [InlineData(InputFormatterExceptionPolicy.MalformedInputExceptions)] public async Task BindModel_CustomFormatter_ThrowingInputFormatterException_AddsErrorToModelState( - InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) + InputFormatterExceptionPolicy inputFormatterExceptionPolicy) { // Arrange var httpContext = new DefaultHttpContext(); @@ -219,7 +219,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders new[] { formatter }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy + InputFormatterExceptionPolicy = inputFormatterExceptionPolicy }); // Act @@ -237,16 +237,16 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders Assert.Null(entry.Value.Errors[0].Exception); } - public static TheoryData BuiltInFormattersThrowingInputFormatterException + public static TheoryData BuiltInFormattersThrowingInputFormatterException { get { - return new TheoryData() + return new TheoryData() { - { new XmlSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new XmlSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, - { new XmlDataContractSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new XmlDataContractSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, + { new XmlSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionPolicy.AllExceptions }, + { new XmlSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionPolicy.MalformedInputExceptions }, + { new XmlDataContractSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionPolicy.AllExceptions }, + { new XmlDataContractSerializerInputFormatter(new MvcOptions()), InputFormatterExceptionPolicy.MalformedInputExceptions }, }; } } @@ -255,7 +255,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders [MemberData(nameof(BuiltInFormattersThrowingInputFormatterException))] public async Task BindModel_BuiltInXmlInputFormatters_ThrowingInputFormatterException_AddsErrorToModelState( IInputFormatter formatter, - InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) + InputFormatterExceptionPolicy inputFormatterExceptionPolicy) { // Arrange var httpContext = new DefaultHttpContext(); @@ -268,7 +268,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var binder = CreateBinder(new[] { formatter }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy + InputFormatterExceptionPolicy = inputFormatterExceptionPolicy }); // Act @@ -287,10 +287,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders } [Theory] - [InlineData(InputFormatterExceptionModelStatePolicy.AllExceptions)] - [InlineData(InputFormatterExceptionModelStatePolicy.MalformedInputExceptions)] + [InlineData(InputFormatterExceptionPolicy.AllExceptions)] + [InlineData(InputFormatterExceptionPolicy.MalformedInputExceptions)] public async Task BindModel_BuiltInJsonInputFormatter_ThrowingInputFormatterException_AddsErrorToModelState( - InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) + InputFormatterExceptionPolicy inputFormatterExceptionPolicy) { // Arrange var httpContext = new DefaultHttpContext(); @@ -305,7 +305,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders new[] { new TestableJsonInputFormatter(throwNonInputFormatterException: false) }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy + InputFormatterExceptionPolicy = inputFormatterExceptionPolicy }); // Act @@ -321,16 +321,16 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders Assert.NotEmpty(entry.Value.Errors[0].ErrorMessage); } - public static TheoryData DerivedFormattersThrowingInputFormatterException + public static TheoryData DerivedFormattersThrowingInputFormatterException { get { - return new TheoryData() + return new TheoryData() { - { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, - { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, + { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionPolicy.AllExceptions }, + { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionPolicy.MalformedInputExceptions }, + { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionPolicy.AllExceptions }, + { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: false), InputFormatterExceptionPolicy.MalformedInputExceptions }, }; } } @@ -339,7 +339,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders [MemberData(nameof(DerivedFormattersThrowingInputFormatterException))] public async Task BindModel_DerivedXmlInputFormatters_AddsErrorToModelState_( IInputFormatter formatter, - InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) + InputFormatterExceptionPolicy inputFormatterExceptionPolicy) { // Arrange var httpContext = new DefaultHttpContext(); @@ -352,7 +352,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var binder = CreateBinder(new[] { formatter }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy + InputFormatterExceptionPolicy = inputFormatterExceptionPolicy }); // Act @@ -371,10 +371,10 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders } [Theory] - [InlineData(InputFormatterExceptionModelStatePolicy.AllExceptions)] - [InlineData(InputFormatterExceptionModelStatePolicy.MalformedInputExceptions)] + [InlineData(InputFormatterExceptionPolicy.AllExceptions)] + [InlineData(InputFormatterExceptionPolicy.MalformedInputExceptions)] public async Task BindModel_DerivedJsonInputFormatter_AddsErrorToModelState( - InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) + InputFormatterExceptionPolicy inputFormatterExceptionPolicy) { // Arrange var httpContext = new DefaultHttpContext(); @@ -389,7 +389,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders new[] { new DerivedJsonInputFormatter(throwNonInputFormatterException: false) }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy + InputFormatterExceptionPolicy = inputFormatterExceptionPolicy }); // Act @@ -407,18 +407,18 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders } // Throwing Non-InputFormatterException - public static TheoryData BuiltInFormattersThrowingNonInputFormatterException + public static TheoryData BuiltInFormattersThrowingNonInputFormatterException { get { - return new TheoryData() + return new TheoryData() { - { new TestableXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new TestableXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, - { new TestableXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new TestableXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, - { new TestableJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new TestableJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, + { new TestableXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.AllExceptions }, + { new TestableXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.MalformedInputExceptions }, + { new TestableXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.AllExceptions }, + { new TestableXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.MalformedInputExceptions }, + { new TestableJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionPolicy.AllExceptions }, + { new TestableJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionPolicy.MalformedInputExceptions }, }; } } @@ -428,7 +428,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders public async Task BindModel_BuiltInInputFormatters_ThrowingNonInputFormatterException_Throws( IInputFormatter formatter, string contentType, - InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) + InputFormatterExceptionPolicy inputFormatterExceptionPolicy) { // Arrange var httpContext = new DefaultHttpContext(); @@ -441,7 +441,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var binder = CreateBinder(new[] { formatter }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy + InputFormatterExceptionPolicy = inputFormatterExceptionPolicy }); // Act & Assert @@ -449,18 +449,18 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders Assert.Equal("Unable to read input stream!!", exception.Message); } - public static TheoryData DerivedInputFormattersThrowingNonInputFormatterException + public static TheoryData DerivedInputFormattersThrowingNonInputFormatterException { get { - return new TheoryData() + return new TheoryData() { - { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, - { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, - { new DerivedJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionModelStatePolicy.AllExceptions }, - { new DerivedJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionModelStatePolicy.MalformedInputExceptions }, + { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.AllExceptions }, + { new DerivedXmlSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.MalformedInputExceptions }, + { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.AllExceptions }, + { new DerivedXmlDataContractSerializerInputFormatter(throwNonInputFormatterException: true), "text/xml", InputFormatterExceptionPolicy.MalformedInputExceptions }, + { new DerivedJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionPolicy.AllExceptions }, + { new DerivedJsonInputFormatter(throwNonInputFormatterException: true), "text/json", InputFormatterExceptionPolicy.MalformedInputExceptions }, }; } } @@ -470,7 +470,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders public async Task BindModel_DerivedXmlInputFormatters_ThrowingNonInputFormatingException_AddsErrorToModelState( IInputFormatter formatter, string contentType, - InputFormatterExceptionModelStatePolicy inputFormatterExceptionModelStatePolicy) + InputFormatterExceptionPolicy inputFormatterExceptionPolicy) { // Arrange var httpContext = new DefaultHttpContext(); @@ -483,7 +483,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders var bindingContext = GetBindingContext(typeof(Person), httpContext, metadataProvider); var binder = CreateBinder(new[] { formatter }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = inputFormatterExceptionModelStatePolicy + InputFormatterExceptionPolicy = inputFormatterExceptionPolicy }); // Act @@ -521,7 +521,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders new[] { formatter }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = InputFormatterExceptionModelStatePolicy.MalformedInputExceptions + InputFormatterExceptionPolicy = InputFormatterExceptionPolicy.MalformedInputExceptions }); // Act @@ -550,7 +550,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders new[] { formatter }, new MvcOptions() { - InputFormatterExceptionModelStatePolicy = InputFormatterExceptionModelStatePolicy.AllExceptions + InputFormatterExceptionPolicy = InputFormatterExceptionPolicy.AllExceptions }); // Act @@ -804,7 +804,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders _throwNonInputFormatterException = throwNonInputFormatterException; } - public override InputFormatterExceptionModelStatePolicy ExceptionPolicy => InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + public override InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.MalformedInputExceptions; public override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { @@ -826,7 +826,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders _throwNonInputFormatterException = throwNonInputFormatterException; } - public override InputFormatterExceptionModelStatePolicy ExceptionPolicy => InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + public override InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.MalformedInputExceptions; public override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { @@ -848,7 +848,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders _throwNonInputFormatterException = throwNonInputFormatterException; } - public override InputFormatterExceptionModelStatePolicy ExceptionPolicy => InputFormatterExceptionModelStatePolicy.MalformedInputExceptions; + public override InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.MalformedInputExceptions; public override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { @@ -870,7 +870,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders _throwNonInputFormatterException = throwNonInputFormatterException; } - public override InputFormatterExceptionModelStatePolicy ExceptionPolicy => InputFormatterExceptionModelStatePolicy.AllExceptions; + public override InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.AllExceptions; public override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { @@ -892,7 +892,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders _throwNonInputFormatterException = throwNonInputFormatterException; } - public override InputFormatterExceptionModelStatePolicy ExceptionPolicy => InputFormatterExceptionModelStatePolicy.AllExceptions; + public override InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.AllExceptions; public override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { @@ -914,7 +914,7 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding.Binders _throwNonInputFormatterException = throwNonInputFormatterException; } - public override InputFormatterExceptionModelStatePolicy ExceptionPolicy => InputFormatterExceptionModelStatePolicy.AllExceptions; + public override InputFormatterExceptionPolicy ExceptionPolicy => InputFormatterExceptionPolicy.AllExceptions; public override Task ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { diff --git a/test/Microsoft.AspNetCore.Mvc.Test/IntegrationTest/CompatibilitySwitchIntegrationTest.cs b/test/Microsoft.AspNetCore.Mvc.Test/IntegrationTest/CompatibilitySwitchIntegrationTest.cs index 45dabaf9df..c7ca86a35f 100644 --- a/test/Microsoft.AspNetCore.Mvc.Test/IntegrationTest/CompatibilitySwitchIntegrationTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Test/IntegrationTest/CompatibilitySwitchIntegrationTest.cs @@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTest // Assert Assert.False(mvcOptions.SuppressBindingUndefinedValueToEnumType); - Assert.Equal(InputFormatterExceptionModelStatePolicy.AllExceptions, mvcOptions.InputFormatterExceptionModelStatePolicy); + Assert.Equal(InputFormatterExceptionPolicy.AllExceptions, mvcOptions.InputFormatterExceptionPolicy); Assert.False(mvcOptions.SuppressJsonDeserializationExceptionMessagesInModelState); // This name needs to be inverted in #7157 } @@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTest // Assert Assert.True(mvcOptions.SuppressBindingUndefinedValueToEnumType); - Assert.Equal(InputFormatterExceptionModelStatePolicy.MalformedInputExceptions, mvcOptions.InputFormatterExceptionModelStatePolicy); + Assert.Equal(InputFormatterExceptionPolicy.MalformedInputExceptions, mvcOptions.InputFormatterExceptionPolicy); Assert.True(mvcOptions.SuppressJsonDeserializationExceptionMessagesInModelState); // This name needs to be inverted in #7157 } @@ -69,7 +69,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTest // Assert Assert.True(mvcOptions.SuppressBindingUndefinedValueToEnumType); - Assert.Equal(InputFormatterExceptionModelStatePolicy.MalformedInputExceptions, mvcOptions.InputFormatterExceptionModelStatePolicy); + Assert.Equal(InputFormatterExceptionPolicy.MalformedInputExceptions, mvcOptions.InputFormatterExceptionPolicy); Assert.True(mvcOptions.SuppressJsonDeserializationExceptionMessagesInModelState); // This name needs to be inverted in #7157 }