Skip to content

Fixed: crash when building ModelState error response #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ private static IReadOnlyCollection<Error> FromModelState(ModelStateDictionary mo

foreach (var (propertyName, entry) in modelState.Where(x => x.Value.Errors.Any()))
{
PropertyInfo property = resourceType.GetProperty(propertyName);

string attributeName =
property.GetCustomAttribute<AttrAttribute>().PublicName ?? namingStrategy.GetPropertyName(property.Name, false);
string attributeName = GetDisplayNameForProperty(propertyName, resourceType, namingStrategy);

foreach (var modelError in entry.Errors)
{
Expand All @@ -55,6 +52,19 @@ private static IReadOnlyCollection<Error> FromModelState(ModelStateDictionary mo
return errors;
}

private static string GetDisplayNameForProperty(string propertyName, Type resourceType,
NamingStrategy namingStrategy)
{
PropertyInfo property = resourceType.GetProperty(propertyName);
if (property != null)
{
var attrAttribute = property.GetCustomAttribute<AttrAttribute>();
return attrAttribute?.PublicName ?? namingStrategy.GetPropertyName(property.Name, false);
}

return propertyName;
}

private static Error FromModelError(ModelError modelError, string attributeName,
bool includeExceptionStackTraceInErrors)
{
Expand Down