diff --git a/src/Mvc/Mvc.Core/ref/Microsoft.AspNetCore.Mvc.Core.netcoreapp3.0.cs b/src/Mvc/Mvc.Core/ref/Microsoft.AspNetCore.Mvc.Core.netcoreapp3.0.cs index 2af1c78d22..b270c45725 100644 --- a/src/Mvc/Mvc.Core/ref/Microsoft.AspNetCore.Mvc.Core.netcoreapp3.0.cs +++ b/src/Mvc/Mvc.Core/ref/Microsoft.AspNetCore.Mvc.Core.netcoreapp3.0.cs @@ -2320,7 +2320,6 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding public static string CreateIndexModelName(string parentName, int index) { throw null; } public static string CreateIndexModelName(string parentName, string index) { throw null; } public static string CreatePropertyModelName(string prefix, string propertyName) { throw null; } - public static Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata GetPathMetadata(Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata metadata, string path) { throw null; } } public abstract partial class ObjectModelValidator : Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidator { diff --git a/src/Mvc/Mvc.Core/src/Formatters/SystemTextJsonInputFormatter.cs b/src/Mvc/Mvc.Core/src/Formatters/SystemTextJsonInputFormatter.cs index 63dadc82b2..d584f47f82 100644 --- a/src/Mvc/Mvc.Core/src/Formatters/SystemTextJsonInputFormatter.cs +++ b/src/Mvc/Mvc.Core/src/Formatters/SystemTextJsonInputFormatter.cs @@ -81,8 +81,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters var formatterException = new InputFormatterException(jsonException.Message, jsonException); - var metadata = ModelNames.GetPathMetadata(context.Metadata, path); - context.ModelState.TryAddModelError(path, formatterException, metadata); + context.ModelState.TryAddModelError(path, formatterException, context.Metadata); Log.JsonInputException(_logger, jsonException); diff --git a/src/Mvc/Mvc.Core/src/ModelBinding/ModelNames.cs b/src/Mvc/Mvc.Core/src/ModelBinding/ModelNames.cs index 4f366e2a70..185c8345e0 100644 --- a/src/Mvc/Mvc.Core/src/ModelBinding/ModelNames.cs +++ b/src/Mvc/Mvc.Core/src/ModelBinding/ModelNames.cs @@ -39,51 +39,5 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding return prefix + "." + propertyName; } - - public static ModelMetadata GetPathMetadata(ModelMetadata metadata, string path) - { - var index = 0; - while (index >= 0 && index < path.Length) - { - if (path[index] == '[') - { - // At start of "[0]". - if (metadata.ElementMetadata == null) - { - // Odd case but don't throw just because ErrorContext had an odd-looking path. - break; - } - - metadata = metadata.ElementMetadata; - index = path.IndexOf(']', index); - } - else if (path[index] == '.' || path[index] == ']') - { - // Skip '.' in "prefix.property" or "[0].property" or ']' in "[0]". - index++; - } - else - { - // At start of "property", "property." or "property[0]". - var endIndex = path.IndexOfAny(new[] { '.', '[' }, index); - if (endIndex == -1) - { - endIndex = path.Length; - } - - var propertyName = path.Substring(index, endIndex - index); - if (metadata.Properties[propertyName] == null) - { - // Odd case but don't throw just because ErrorContext had an odd-looking path. - break; - } - - metadata = metadata.Properties[propertyName]; - index = endIndex; - } - } - - return metadata; - } } } diff --git a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs index 49a5581b87..8975f9f426 100644 --- a/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs +++ b/src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs @@ -243,7 +243,7 @@ namespace Microsoft.AspNetCore.Mvc.Formatters exception = eventArgs.ErrorContext.Error; - var metadata = ModelNames.GetPathMetadata(context.Metadata, path); + var metadata = GetPathMetadata(context.Metadata, path); var modelStateException = WrapExceptionForModelState(exception); context.ModelState.TryAddModelError(key, modelStateException, metadata); @@ -301,6 +301,52 @@ namespace Microsoft.AspNetCore.Mvc.Formatters protected virtual void ReleaseJsonSerializer(JsonSerializer serializer) => _jsonSerializerPool.Return(serializer); + private ModelMetadata GetPathMetadata(ModelMetadata metadata, string path) + { + var index = 0; + while (index >= 0 && index < path.Length) + { + if (path[index] == '[') + { + // At start of "[0]". + if (metadata.ElementMetadata == null) + { + // Odd case but don't throw just because ErrorContext had an odd-looking path. + break; + } + + metadata = metadata.ElementMetadata; + index = path.IndexOf(']', index); + } + else if (path[index] == '.' || path[index] == ']') + { + // Skip '.' in "prefix.property" or "[0].property" or ']' in "[0]". + index++; + } + else + { + // At start of "property", "property." or "property[0]". + var endIndex = path.IndexOfAny(new[] { '.', '[' }, index); + if (endIndex == -1) + { + endIndex = path.Length; + } + + var propertyName = path.Substring(index, endIndex - index); + if (metadata.Properties[propertyName] == null) + { + // Odd case but don't throw just because ErrorContext had an odd-looking path. + break; + } + + metadata = metadata.Properties[propertyName]; + index = endIndex; + } + } + + return metadata; + } + private Exception WrapExceptionForModelState(Exception exception) { // In 2.0 and earlier we always gave a generic error message for errors that come from JSON.NET