-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathJsonApiSchemaIdSelector.cs
More file actions
182 lines (152 loc) · 9.16 KB
/
JsonApiSchemaIdSelector.cs
File metadata and controls
182 lines (152 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System.Text.Json;
using Humanizer;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.AtomicOperations;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.Documents;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.Relationships;
using JsonApiDotNetCore.OpenApi.Swashbuckle.JsonApiObjects.ResourceObjects;
using JsonApiDotNetCore.Resources.Annotations;
using JsonApiDotNetCore.Serialization.Objects;
namespace JsonApiDotNetCore.OpenApi.Swashbuckle;
internal sealed class JsonApiSchemaIdSelector
{
private const string ResourceTypeSchemaIdTemplate = "[ResourceName] Resource Type";
private const string MetaSchemaIdTemplate = "Meta";
private const string UpdateRelationshipAtomicOperationSchemaIdTemplate = "Update [ResourceName] [RelationshipName] Relationship Operation";
private const string AddToRelationshipAtomicOperationSchemaIdTemplate = "Add To [ResourceName] [RelationshipName] Relationship Operation";
private const string RemoveFromRelationshipAtomicOperationSchemaIdTemplate = "Remove From [ResourceName] [RelationshipName] Relationship Operation";
private const string RelationshipIdentifierSchemaIdTemplate = "[ResourceName] [RelationshipName] Relationship Identifier";
private const string RelationshipNameSchemaIdTemplate = "[ResourceName] [RelationshipName] Relationship Name";
private static readonly Dictionary<Type, string> SchemaTypeToTemplateMap = new()
{
[typeof(CreateRequestDocument<>)] = "Create [ResourceName] Request Document",
[typeof(UpdateRequestDocument<>)] = "Update [ResourceName] Request Document",
[typeof(ResourceInCreateRequest)] = "Resource In Create Request",
[typeof(DataInCreateRequest<>)] = "Data In Create [ResourceName] Request",
[typeof(AttributesInCreateRequest)] = "Attributes In Create Request",
[typeof(AttributesInCreateRequest<>)] = "Attributes In Create [ResourceName] Request",
[typeof(RelationshipsInCreateRequest)] = "Relationships In Create Request",
[typeof(RelationshipsInCreateRequest<>)] = "Relationships In Create [ResourceName] Request",
[typeof(ResourceInUpdateRequest)] = "Resource In Update Request",
[typeof(DataInUpdateRequest<>)] = "Data In Update [ResourceName] Request",
[typeof(AttributesInUpdateRequest)] = "Attributes In Update Request",
[typeof(AttributesInUpdateRequest<>)] = "Attributes In Update [ResourceName] Request",
[typeof(RelationshipsInUpdateRequest)] = "Relationships In Update Request",
[typeof(RelationshipsInUpdateRequest<>)] = "Relationships In Update [ResourceName] Request",
[typeof(ToOneInRequest<>)] = "To One [ResourceName] In Request",
[typeof(NullableToOneInRequest<>)] = "Nullable To One [ResourceName] In Request",
[typeof(ToManyInRequest<>)] = "To Many [ResourceName] In Request",
[typeof(PrimaryResponseDocument<>)] = "Primary [ResourceName] Response Document",
[typeof(SecondaryResponseDocument<>)] = "Secondary [ResourceName] Response Document",
[typeof(NullableSecondaryResponseDocument<>)] = "Nullable Secondary [ResourceName] Response Document",
[typeof(CollectionResponseDocument<>)] = "[ResourceName] Collection Response Document",
[typeof(IdentifierResponseDocument<>)] = "[ResourceName] Identifier Response Document",
[typeof(NullableIdentifierResponseDocument<>)] = "Nullable [ResourceName] Identifier Response Document",
[typeof(IdentifierCollectionResponseDocument<>)] = "[ResourceName] Identifier Collection Response Document",
[typeof(ToOneInResponse<>)] = "To One [ResourceName] In Response",
[typeof(NullableToOneInResponse<>)] = "Nullable To One [ResourceName] In Response",
[typeof(ToManyInResponse<>)] = "To Many [ResourceName] In Response",
[typeof(ResourceInResponse)] = "Resource In Response",
[typeof(DataInResponse<>)] = "Data In [ResourceName] Response",
[typeof(AttributesInResponse<>)] = "Attributes In [ResourceName] Response",
[typeof(RelationshipsInResponse<>)] = "Relationships In [ResourceName] Response",
[typeof(IdentifierInRequest)] = "Identifier In Request",
[typeof(IdentifierInRequest<>)] = "[ResourceName] Identifier In Request",
[typeof(IdentifierInResponse<>)] = "[ResourceName] Identifier In Response",
[typeof(CreateOperation<>)] = "Create [ResourceName] Operation",
[typeof(UpdateOperation<>)] = "Update [ResourceName] Operation",
[typeof(DeleteOperation<>)] = "Delete [ResourceName] Operation",
[typeof(UpdateToOneRelationshipOperation<>)] = "Temporary Update [ResourceName] To One Relationship Operation",
[typeof(UpdateToManyRelationshipOperation<>)] = "Temporary Update [ResourceName] To Many Relationship Operation",
[typeof(AddToRelationshipOperation<>)] = "Temporary Add To [ResourceName] Relationship Operation",
[typeof(RemoveFromRelationshipOperation<>)] = "Temporary Remove From [ResourceName] Relationship Operation"
};
private readonly IJsonApiOptions _options;
private readonly IResourceGraph _resourceGraph;
public JsonApiSchemaIdSelector(IJsonApiOptions options, IResourceGraph resourceGraph)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(resourceGraph);
_options = options;
_resourceGraph = resourceGraph;
}
public string GetSchemaId(Type type)
{
ArgumentNullException.ThrowIfNull(type);
ResourceType? resourceType = _resourceGraph.FindResourceType(type);
if (resourceType != null)
{
return resourceType.PublicName.Singularize();
}
Type openType = type.ConstructedToOpenType();
if (openType != type)
{
if (SchemaTypeToTemplateMap.TryGetValue(openType, out string? schemaTemplate))
{
Type resourceClrType = type.GetGenericArguments().First();
resourceType = _resourceGraph.GetResourceType(resourceClrType);
return ApplySchemaTemplate(schemaTemplate, resourceType, null, null);
}
}
else
{
if (SchemaTypeToTemplateMap.TryGetValue(type, out string? schemaTemplate))
{
return ApplySchemaTemplate(schemaTemplate, null, null, null);
}
}
// Used for a fixed set of non-generic types, such as Jsonapi, ResourceCollectionTopLevelLinks etc.
return ApplySchemaTemplate(type.Name, null, null, null);
}
private string ApplySchemaTemplate(string schemaTemplate, ResourceType? resourceType, string? relationshipName, AtomicOperationCode? operationCode)
{
string? schemaId = schemaTemplate;
schemaId = resourceType != null
? schemaId.Replace("[ResourceName]", resourceType.PublicName.Singularize()).Pascalize()
: schemaId.Replace("[ResourceName]", "$$$").Pascalize().Replace("$$$", string.Empty);
if (relationshipName != null)
{
schemaId = schemaId.Replace("[RelationshipName]", relationshipName.Pascalize());
}
if (operationCode != null)
{
schemaId = schemaId.Replace("[OperationCode]", operationCode.Value.ToString().Pascalize());
}
string? pascalCaseSchemaId = schemaId.Pascalize();
JsonNamingPolicy? namingPolicy = _options.SerializerOptions.PropertyNamingPolicy;
return namingPolicy != null ? namingPolicy.ConvertName(pascalCaseSchemaId) : pascalCaseSchemaId;
}
public string GetResourceTypeSchemaId(ResourceType? resourceType)
{
return ApplySchemaTemplate(ResourceTypeSchemaIdTemplate, resourceType, null, null);
}
public string GetMetaSchemaId()
{
return ApplySchemaTemplate(MetaSchemaIdTemplate, null, null, null);
}
public string GetAtomicOperationCodeSchemaId(AtomicOperationCode operationCode)
{
return ApplySchemaTemplate("[OperationCode] Operation Code", null, null, operationCode);
}
public string GetRelationshipAtomicOperationSchemaId(RelationshipAttribute relationship, AtomicOperationCode operationCode)
{
ArgumentNullException.ThrowIfNull(relationship);
string schemaIdTemplate = operationCode switch
{
AtomicOperationCode.Add => AddToRelationshipAtomicOperationSchemaIdTemplate,
AtomicOperationCode.Remove => RemoveFromRelationshipAtomicOperationSchemaIdTemplate,
_ => UpdateRelationshipAtomicOperationSchemaIdTemplate
};
return ApplySchemaTemplate(schemaIdTemplate, relationship.LeftType, relationship.PublicName, null);
}
public string GetRelationshipIdentifierSchemaId(RelationshipAttribute relationship)
{
ArgumentNullException.ThrowIfNull(relationship);
return ApplySchemaTemplate(RelationshipIdentifierSchemaIdTemplate, relationship.LeftType, relationship.PublicName, null);
}
public string GetRelationshipNameSchemaId(RelationshipAttribute relationship)
{
ArgumentNullException.ThrowIfNull(relationship);
return ApplySchemaTemplate(RelationshipNameSchemaIdTemplate, relationship.LeftType, relationship.PublicName, null);
}
}