Cleanup
This commit is contained in:
parent
8f99ca3fad
commit
f6f1923e2b
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue