-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resource inheritance: sorting on derived fields
- Loading branch information
Bart Koelman
committed
Mar 21, 2022
1 parent
afa13c0
commit 5b9a64d
Showing
20 changed files
with
1,285 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/JsonApiDotNetCore/Queries/Internal/Parsing/FieldChainInheritanceRequirement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace JsonApiDotNetCore.Queries.Internal.Parsing; | ||
|
||
/// <summary> | ||
/// Indicates how to handle derived types when resolving resource field chains. | ||
/// </summary> | ||
internal enum FieldChainInheritanceRequirement | ||
{ | ||
/// <summary> | ||
/// Do not consider derived types when resolving attributes or relationships. | ||
/// </summary> | ||
Disabled, | ||
|
||
/// <summary> | ||
/// Consider derived types when resolving attributes or relationships, but fail when multiple matches are found. | ||
/// </summary> | ||
RequireSingleMatch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/JsonApiDotNetCore/Queries/Internal/Parsing/ResourceFieldCategory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace JsonApiDotNetCore.Queries.Internal.Parsing; | ||
|
||
internal enum ResourceFieldCategory | ||
{ | ||
Field, | ||
Attribute, | ||
Relationship | ||
} |
83 changes: 83 additions & 0 deletions
83
src/JsonApiDotNetCore/Queries/Internal/Parsing/ResourceFieldChainErrorFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Text; | ||
using JsonApiDotNetCore.Configuration; | ||
|
||
namespace JsonApiDotNetCore.Queries.Internal.Parsing; | ||
|
||
internal sealed class ResourceFieldChainErrorFormatter | ||
{ | ||
public string GetForNotFound(ResourceFieldCategory category, string publicName, string path, ResourceType resourceType, | ||
FieldChainInheritanceRequirement inheritanceRequirement) | ||
{ | ||
var builder = new StringBuilder(); | ||
WriteSource(category, publicName, builder); | ||
WritePath(path, publicName, builder); | ||
|
||
builder.Append($" does not exist on resource type '{resourceType.PublicName}'"); | ||
|
||
if (inheritanceRequirement != FieldChainInheritanceRequirement.Disabled && resourceType.DirectlyDerivedTypes.Any()) | ||
{ | ||
builder.Append(" or any of its derived types"); | ||
} | ||
|
||
builder.Append('.'); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public string GetForMultipleMatches(ResourceFieldCategory category, string publicName, string path) | ||
{ | ||
var builder = new StringBuilder(); | ||
WriteSource(category, publicName, builder); | ||
WritePath(path, publicName, builder); | ||
|
||
builder.Append(" is defined on multiple derived types."); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public string GetForWrongFieldType(ResourceFieldCategory category, string publicName, string path, ResourceType resourceType, string expected) | ||
{ | ||
var builder = new StringBuilder(); | ||
WriteSource(category, publicName, builder); | ||
WritePath(path, publicName, builder); | ||
|
||
builder.Append($" must be {expected} on resource type '{resourceType.PublicName}'."); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
public string GetForNoneFound(ResourceFieldCategory category, string publicName, string path, ICollection<ResourceType> parentResourceTypes, | ||
bool hasDerivedTypes) | ||
{ | ||
var builder = new StringBuilder(); | ||
WriteSource(category, publicName, builder); | ||
WritePath(path, publicName, builder); | ||
|
||
if (parentResourceTypes.Count == 1) | ||
{ | ||
builder.Append($" does not exist on resource type '{parentResourceTypes.First().PublicName}'"); | ||
} | ||
else | ||
{ | ||
string typeNames = string.Join(", ", parentResourceTypes.Select(type => $"'{type.PublicName}'")); | ||
builder.Append($" does not exist on any of the resource types {typeNames}"); | ||
} | ||
|
||
builder.Append(hasDerivedTypes ? " or any of its derived types." : "."); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
private static void WriteSource(ResourceFieldCategory category, string publicName, StringBuilder builder) | ||
{ | ||
builder.Append($"{category} '{publicName}'"); | ||
} | ||
|
||
private static void WritePath(string path, string publicName, StringBuilder builder) | ||
{ | ||
if (path != publicName) | ||
{ | ||
builder.Append($" in '{path}'"); | ||
} | ||
} | ||
} |
Oops, something went wrong.