Skip to content

Commit b841433

Browse files
committed
fix: handle 'GetPublicAttributeName' null and mock for tests
1 parent 0e17059 commit b841433

File tree

3 files changed

+356
-348
lines changed

3 files changed

+356
-348
lines changed

src/JsonApiDotNetCore/Extensions/ModelStateExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public static ErrorCollection ConvertToErrorCollection<T>(this ModelStateDiction
1414
if (entry.Value.Errors.Any() == false)
1515
continue;
1616

17+
var attrName = contextGraph.GetPublicAttributeName<T>(entry.Key);
18+
1719
foreach (var modelError in entry.Value.Errors)
1820
{
19-
var attrName =contextGraph.GetPublicAttributeName<T>(entry.Key);
20-
2121
if (modelError.Exception is JsonApiException jex)
2222
collection.Errors.AddRange(jex.GetError().Errors);
2323
else
@@ -26,7 +26,7 @@ public static ErrorCollection ConvertToErrorCollection<T>(this ModelStateDiction
2626
title: entry.Key,
2727
detail: modelError.ErrorMessage,
2828
meta: modelError.Exception != null ? ErrorMeta.FromException(modelError.Exception) : null,
29-
source: new {
29+
source: attrName == null ? null : new {
3030
pointer = $"/data/attributes/{attrName}"
3131
}));
3232
}

src/JsonApiDotNetCore/Internal/ContextGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public string GetPublicAttributeName<TParent>(string internalAttributeName)
122122
{
123123
return GetContextEntity(typeof(TParent))
124124
.Attributes
125-
.Single(a => a.InternalAttributeName == internalAttributeName)
125+
.SingleOrDefault(a => a.InternalAttributeName == internalAttributeName)?
126126
.PublicAttributeName;
127127
}
128128
}

0 commit comments

Comments
 (0)