Document compatiblity switch for input formatter exceptions
This commit is contained in:
parent
7d64990a69
commit
bf61ce2b8f
|
|
@ -6,7 +6,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
|||
/// <summary>
|
||||
/// A policy which <see cref="IInputFormatter"/>s can implement to indicate if they want the body model binder
|
||||
/// to handle all exceptions. By default, all default <see cref="IInputFormatter"/>s implement this interface and
|
||||
/// have a default value of <see cref="InputFormatterExceptionModelStatePolicy.MalformedInputExceptions"/>.
|
||||
/// have a default value of <see cref="InputFormatterExceptionPolicy.MalformedInputExceptions"/>.
|
||||
/// </summary>
|
||||
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.
|
||||
/// </summary>
|
||||
InputFormatterExceptionModelStatePolicy ExceptionPolicy { get; }
|
||||
InputFormatterExceptionPolicy ExceptionPolicy { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the set of policies that determine how the model binding system interprets exceptions
|
||||
/// thrown by an <see cref="IInputFormatter"/>. Applications should set
|
||||
/// <c>MvcOptions.InputFormatterExceptionPolicy</c> to configure this setting.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// An <see cref="IInputFormatter"/> could throw an exception for several reasons, including:
|
||||
/// <list type="bullet">
|
||||
/// <item><description>malformed input</description></item>
|
||||
/// <item><description>client disconnect or other I/O problem</description></item>
|
||||
/// <item><description>
|
||||
/// application configuration problems such as <see cref="TypeLoadException"/>
|
||||
/// </description></item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The policy associated with <see cref="InputFormatterExceptionPolicy.AllExceptions"/> 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.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The policy associated with <see cref="InputFormatterExceptionPolicy.MalformedInputExceptions"/>
|
||||
/// treats only <see cref="InputFormatterException"/> 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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public enum InputFormatterExceptionPolicy
|
||||
{
|
||||
/// <summary>
|
||||
/// This value indicates that all exceptions thrown by an <see cref="IInputFormatter"/> will be treated
|
||||
/// as model state errors.
|
||||
/// </summary>
|
||||
AllExceptions = 0,
|
||||
|
||||
/// <summary>
|
||||
/// This value indicates that only <see cref="InputFormatterException"/> 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.
|
||||
/// </summary>
|
||||
MalformedInputExceptions = 1,
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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> _inputFormatterExceptionModelStatePolicy;
|
||||
private readonly CompatibilitySwitch<InputFormatterExceptionPolicy> _inputFormatterExceptionPolicy;
|
||||
private readonly CompatibilitySwitch<bool> _suppressBindingUndefinedValueToEnumType;
|
||||
private readonly CompatibilitySwitch<bool> _suppressJsonDeserializationExceptionMessagesInModelState;
|
||||
private readonly ICompatibilitySwitch[] _switches;
|
||||
|
|
@ -41,12 +41,12 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
ModelValidatorProviders = new List<IModelValidatorProvider>();
|
||||
ValueProviderFactories = new List<IValueProviderFactory>();
|
||||
|
||||
_inputFormatterExceptionModelStatePolicy = new CompatibilitySwitch<InputFormatterExceptionModelStatePolicy>(nameof(InputFormatterExceptionModelStatePolicy), InputFormatterExceptionModelStatePolicy.AllExceptions);
|
||||
_inputFormatterExceptionPolicy = new CompatibilitySwitch<InputFormatterExceptionPolicy>(nameof(InputFormatterExceptionPolicy), InputFormatterExceptionPolicy.AllExceptions);
|
||||
_suppressBindingUndefinedValueToEnumType = new CompatibilitySwitch<bool>(nameof(SuppressBindingUndefinedValueToEnumType));
|
||||
_suppressJsonDeserializationExceptionMessagesInModelState = new CompatibilitySwitch<bool>(nameof(SuppressJsonDeserializationExceptionMessagesInModelState));
|
||||
_switches = new ICompatibilitySwitch[]
|
||||
{
|
||||
_inputFormatterExceptionModelStatePolicy,
|
||||
_inputFormatterExceptionPolicy,
|
||||
_suppressBindingUndefinedValueToEnumType,
|
||||
_suppressJsonDeserializationExceptionMessagesInModelState,
|
||||
};
|
||||
|
|
@ -87,6 +87,37 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
/// </summary>
|
||||
public FormatterMappings FormatterMappings { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value which determines how the model binding system interprets exceptions thrown by an <see cref="IInputFormatter"/>.
|
||||
/// The default value of the property is <see cref="InputFormatterExceptionPolicy.AllExceptions"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This property is associated with a compatibility switch and can provide a different behavior depending on
|
||||
/// the configured compatibility version for the application. See <see cref="CompatibilityVersion"/> for
|
||||
/// guidance and examples of setting the application's compatibility version.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// 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 <see cref="CompatibilityVersion"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the application's compatibility version is set to <see cref="CompatibilityVersion.Version_2_0"/> then
|
||||
/// this setting will have the value <see cref="InputFormatterExceptionPolicy.AllExceptions"/> if
|
||||
/// not explicitly configured.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the application's compatibility version is set to <see cref="CompatibilityVersion.Version_2_1"/> or
|
||||
/// higher then this setting will have the value
|
||||
/// <see cref="InputFormatterExceptionPolicy.MalformedInputExceptions"/> if not explicitly configured.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public InputFormatterExceptionPolicy InputFormatterExceptionPolicy
|
||||
{
|
||||
get => _inputFormatterExceptionPolicy.Value;
|
||||
set => _inputFormatterExceptionPolicy.Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of <see cref="IInputFormatter"/>s that are used by this application.
|
||||
/// </summary>
|
||||
|
|
@ -103,16 +134,16 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
/// guidance and examples of setting the application's compatibility version.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// 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 <see cref="CompatibilityVersion"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the application's compatibility version is set to <see cref="CompatibilityVersion.Version_2_0"/> then
|
||||
/// this setting will have value <c>false</c> if not explicitly configured.
|
||||
/// this setting will have the value <c>false</c> if not explicitly configured.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// If the application's compatibility version is set to <see cref="CompatibilityVersion.Version_2_1"/> or
|
||||
/// higher then this setting will have value <c>true</c> if not explicitly configured.
|
||||
/// higher then this setting will have the value <c>true</c> if not explicitly configured.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public bool SuppressBindingUndefinedValueToEnumType
|
||||
|
|
@ -211,18 +242,6 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
public bool RequireHttpsPermanent { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="IInputFormatter"/>s into model state errors.
|
||||
/// This option applies only to custom <see cref="IInputFormatter"/>s.
|
||||
/// Default is <see cref="InputFormatterExceptionModelStatePolicy.AllExceptions"/>.
|
||||
/// </summary>
|
||||
public InputFormatterExceptionModelStatePolicy InputFormatterExceptionModelStatePolicy
|
||||
{
|
||||
get => _inputFormatterExceptionModelStatePolicy.Value;
|
||||
set => _inputFormatterExceptionModelStatePolicy.Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
|
|
|
|||
|
|
@ -191,15 +191,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
|||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -119,15 +119,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
|||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -113,15 +113,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
|||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -92,15 +92,15 @@ namespace Microsoft.AspNetCore.Mvc.Formatters
|
|||
public XmlDictionaryReaderQuotas XmlDictionaryReaderQuotas => _readerQuotas;
|
||||
|
||||
/// <inheritdoc />
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<IInputFormatter, InputFormatterExceptionModelStatePolicy> BuiltInFormattersThrowingInputFormatterException
|
||||
public static TheoryData<IInputFormatter, InputFormatterExceptionPolicy> BuiltInFormattersThrowingInputFormatterException
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<IInputFormatter, InputFormatterExceptionModelStatePolicy>()
|
||||
return new TheoryData<IInputFormatter, InputFormatterExceptionPolicy>()
|
||||
{
|
||||
{ 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<IInputFormatter, InputFormatterExceptionModelStatePolicy> DerivedFormattersThrowingInputFormatterException
|
||||
public static TheoryData<IInputFormatter, InputFormatterExceptionPolicy> DerivedFormattersThrowingInputFormatterException
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<IInputFormatter, InputFormatterExceptionModelStatePolicy>()
|
||||
return new TheoryData<IInputFormatter, InputFormatterExceptionPolicy>()
|
||||
{
|
||||
{ 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<IInputFormatter, string, InputFormatterExceptionModelStatePolicy> BuiltInFormattersThrowingNonInputFormatterException
|
||||
public static TheoryData<IInputFormatter, string, InputFormatterExceptionPolicy> BuiltInFormattersThrowingNonInputFormatterException
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<IInputFormatter, string, InputFormatterExceptionModelStatePolicy>()
|
||||
return new TheoryData<IInputFormatter, string, InputFormatterExceptionPolicy>()
|
||||
{
|
||||
{ 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<IInputFormatter, string, InputFormatterExceptionModelStatePolicy> DerivedInputFormattersThrowingNonInputFormatterException
|
||||
public static TheoryData<IInputFormatter, string, InputFormatterExceptionPolicy> DerivedInputFormattersThrowingNonInputFormatterException
|
||||
{
|
||||
get
|
||||
{
|
||||
return new TheoryData<IInputFormatter, string, InputFormatterExceptionModelStatePolicy>()
|
||||
return new TheoryData<IInputFormatter, string, InputFormatterExceptionPolicy>()
|
||||
{
|
||||
{ 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<InputFormatterResult> 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<InputFormatterResult> 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<InputFormatterResult> 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<InputFormatterResult> 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<InputFormatterResult> 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<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue