|
| 1 | +using System.Collections.ObjectModel; |
| 2 | +using JsonApiDotNetCore.Configuration; |
| 3 | +using JsonApiDotNetCore.Controllers; |
| 4 | +using JsonApiDotNetCore.Resources.Annotations; |
| 5 | + |
| 6 | +#pragma warning disable AV1008 // Class should not be static |
| 7 | + |
| 8 | +namespace OpenApiTests; |
| 9 | + |
| 10 | +internal static class JsonPathBuilder |
| 11 | +{ |
| 12 | + public static readonly IReadOnlyCollection<JsonApiEndpoints> KnownEndpoints = |
| 13 | + [ |
| 14 | + JsonApiEndpoints.GetCollection, |
| 15 | + JsonApiEndpoints.GetSingle, |
| 16 | + JsonApiEndpoints.GetSecondary, |
| 17 | + JsonApiEndpoints.GetRelationship, |
| 18 | + JsonApiEndpoints.Post, |
| 19 | + JsonApiEndpoints.PostRelationship, |
| 20 | + JsonApiEndpoints.Patch, |
| 21 | + JsonApiEndpoints.PatchRelationship, |
| 22 | + JsonApiEndpoints.Delete, |
| 23 | + JsonApiEndpoints.DeleteRelationship |
| 24 | + ]; |
| 25 | + |
| 26 | + public static IReadOnlyDictionary<JsonApiEndpoints, ReadOnlyCollection<string>> GetEndpointPaths(ResourceType resourceType) |
| 27 | + { |
| 28 | + var endpointToPathMap = new Dictionary<JsonApiEndpoints, List<string>> |
| 29 | + { |
| 30 | + [JsonApiEndpoints.GetCollection] = |
| 31 | + [ |
| 32 | + $"paths./{resourceType.PublicName}.get", |
| 33 | + $"paths./{resourceType.PublicName}.head" |
| 34 | + ], |
| 35 | + [JsonApiEndpoints.GetSingle] = |
| 36 | + [ |
| 37 | + $"paths./{resourceType.PublicName}/{{id}}.get", |
| 38 | + $"paths./{resourceType.PublicName}/{{id}}.head" |
| 39 | + ], |
| 40 | + [JsonApiEndpoints.GetSecondary] = [], |
| 41 | + [JsonApiEndpoints.GetRelationship] = [], |
| 42 | + [JsonApiEndpoints.Post] = [$"paths./{resourceType.PublicName}.post"], |
| 43 | + [JsonApiEndpoints.PostRelationship] = [], |
| 44 | + [JsonApiEndpoints.Patch] = [$"paths./{resourceType.PublicName}/{{id}}.patch"], |
| 45 | + [JsonApiEndpoints.PatchRelationship] = [], |
| 46 | + [JsonApiEndpoints.Delete] = [$"paths./{resourceType.PublicName}/{{id}}.delete"], |
| 47 | + [JsonApiEndpoints.DeleteRelationship] = [] |
| 48 | + }; |
| 49 | + |
| 50 | + foreach (RelationshipAttribute relationship in resourceType.Relationships) |
| 51 | + { |
| 52 | + endpointToPathMap[JsonApiEndpoints.GetSecondary].AddRange([ |
| 53 | + $"paths./{resourceType.PublicName}/{{id}}/{relationship.PublicName}.get", |
| 54 | + $"paths./{resourceType.PublicName}/{{id}}/{relationship.PublicName}.head" |
| 55 | + ]); |
| 56 | + |
| 57 | + endpointToPathMap[JsonApiEndpoints.GetRelationship].AddRange([ |
| 58 | + $"paths./{resourceType.PublicName}/{{id}}/relationships/{relationship.PublicName}.get", |
| 59 | + $"paths./{resourceType.PublicName}/{{id}}/relationships/{relationship.PublicName}.head" |
| 60 | + ]); |
| 61 | + |
| 62 | + endpointToPathMap[JsonApiEndpoints.PatchRelationship].Add($"paths./{resourceType.PublicName}/{{id}}/relationships/{relationship.PublicName}.patch"); |
| 63 | + |
| 64 | + if (relationship is HasManyAttribute) |
| 65 | + { |
| 66 | + endpointToPathMap[JsonApiEndpoints.PostRelationship].Add( |
| 67 | + $"paths./{resourceType.PublicName}/{{id}}/relationships/{relationship.PublicName}.post"); |
| 68 | + |
| 69 | + endpointToPathMap[JsonApiEndpoints.DeleteRelationship].Add( |
| 70 | + $"paths./{resourceType.PublicName}/{{id}}/relationships/{relationship.PublicName}.delete"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return endpointToPathMap.ToDictionary(pair => pair.Key, pair => pair.Value.AsReadOnly()).AsReadOnly(); |
| 75 | + } |
| 76 | +} |
0 commit comments