diff --git a/src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs b/src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs index 038dee1166..d663a9bad8 100644 --- a/src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs +++ b/src/JsonApiDotNetCore/Errors/InvalidModelStateException.cs @@ -34,10 +34,7 @@ private static IReadOnlyCollection FromModelState(ModelStateDictionary mo foreach (var (propertyName, entry) in modelState.Where(x => x.Value.Errors.Any())) { - PropertyInfo property = resourceType.GetProperty(propertyName); - - string attributeName = - property.GetCustomAttribute().PublicName ?? namingStrategy.GetPropertyName(property.Name, false); + string attributeName = GetDisplayNameForProperty(propertyName, resourceType, namingStrategy); foreach (var modelError in entry.Errors) { @@ -55,6 +52,19 @@ private static IReadOnlyCollection 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(); + return attrAttribute?.PublicName ?? namingStrategy.GetPropertyName(property.Name, false); + } + + return propertyName; + } + private static Error FromModelError(ModelError modelError, string attributeName, bool includeExceptionStackTraceInErrors) {