Skip to content

Commit 842a71b

Browse files
committed
Added tests for pascal and camelcase and introduced fixes to make them work.
1 parent 9f9f1ab commit 842a71b

21 files changed

+5928
-312
lines changed

src/JsonApiDotNetCore.OpenApi/JsonApiOperationIdSelector.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ internal sealed class JsonApiOperationIdSelector
3636

3737
private readonly IControllerResourceMapping _controllerResourceMapping;
3838
private readonly JsonNamingPolicy? _namingPolicy;
39-
private readonly ResourceNameFormatter _formatter;
4039

4140
public JsonApiOperationIdSelector(IControllerResourceMapping controllerResourceMapping, JsonNamingPolicy? namingPolicy)
4241
{
4342
ArgumentGuard.NotNull(controllerResourceMapping, nameof(controllerResourceMapping));
4443

4544
_controllerResourceMapping = controllerResourceMapping;
4645
_namingPolicy = namingPolicy;
47-
_formatter = new ResourceNameFormatter(namingPolicy);
4846
}
4947

5048
public string GetOperationId(ApiDescription endpoint)
@@ -60,7 +58,7 @@ public string GetOperationId(ApiDescription endpoint)
6058

6159
string template = GetTemplate(primaryResourceType.ClrType, endpoint);
6260

63-
return ApplyTemplate(template, primaryResourceType.ClrType, endpoint);
61+
return ApplyTemplate(template, primaryResourceType, endpoint);
6462
}
6563

6664
private static string GetTemplate(Type resourceClrType, ApiDescription endpoint)
@@ -107,28 +105,28 @@ private static Type GetDocumentType(Type primaryResourceClrType, ApiDescription
107105
return type.IsConstructedGenericType ? type.GetGenericTypeDefinition() : null;
108106
}
109107

110-
private string ApplyTemplate(string operationIdTemplate, Type resourceClrType, ApiDescription endpoint)
108+
private string ApplyTemplate(string operationIdTemplate, ResourceType resourceType, ApiDescription endpoint)
111109
{
112110
if (endpoint.RelativePath == null || endpoint.HttpMethod == null)
113111
{
114112
throw new UnreachableCodeException();
115113
}
116114

117-
string method = endpoint.HttpMethod.ToLowerInvariant();
118-
string primaryResourceName = _formatter.FormatResourceName(resourceClrType).Singularize();
115+
string method = endpoint.HttpMethod!.ToLowerInvariant();
119116
string relationshipName = operationIdTemplate.Contains("[RelationshipName]") ? endpoint.RelativePath.Split("/").Last() : string.Empty;
120117

121118
// @formatter:wrap_chained_method_calls chop_always
122119
// @formatter:keep_existing_linebreaks true
123120

124-
string pascalCaseId = operationIdTemplate
121+
string pascalCaseOperationId = operationIdTemplate
125122
.Replace("[Method]", method)
126-
.Replace("[PrimaryResourceName]", primaryResourceName)
127-
.Replace("[RelationshipName]", relationshipName);
123+
.Replace("[PrimaryResourceName]", resourceType.PublicName.Singularize())
124+
.Replace("[RelationshipName]", relationshipName)
125+
.Pascalize();
128126

129127
// @formatter:keep_existing_linebreaks restore
130128
// @formatter:wrap_chained_method_calls restore
131129

132-
return _namingPolicy != null ? _namingPolicy.ConvertName(pascalCaseId) : pascalCaseId;
130+
return _namingPolicy != null ? _namingPolicy.ConvertName(pascalCaseOperationId) : pascalCaseOperationId;
133131
}
134132
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace JsonApiDotNetCore.OpenApi;
4+
5+
internal static class StringExtensions
6+
{
7+
public static string Pascalize(this string source)
8+
{
9+
return Regex.Replace(source, "(?:^|-|_| +)(.)", match => match.Groups[1].Value.ToUpper());
10+
}
11+
}

test/OpenApiClientTests/LegacyClient/ClientAttributeRegistrationLifeTimeTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public async Task Disposed_attribute_registration_for_document_does_not_affect_r
2222
Data = new AirplaneDataInPatchRequest
2323
{
2424
Id = airplaneId,
25-
Type = AirplanesResourceType.Airplanes,
25+
Type = AirplaneResourceType.Airplanes,
2626
Attributes = new AirplaneAttributesInPatchRequest()
2727
}
2828
};
@@ -64,7 +64,7 @@ public async Task Attribute_registration_can_be_used_for_multiple_requests()
6464
Data = new AirplaneDataInPatchRequest
6565
{
6666
Id = airplaneId,
67-
Type = AirplanesResourceType.Airplanes,
67+
Type = AirplaneResourceType.Airplanes,
6868
Attributes = new AirplaneAttributesInPatchRequest
6969
{
7070
AirtimeInHours = 100
@@ -111,7 +111,7 @@ public async Task Request_is_unaffected_by_attribute_registration_for_different_
111111
Data = new AirplaneDataInPatchRequest
112112
{
113113
Id = airplaneId1,
114-
Type = AirplanesResourceType.Airplanes,
114+
Type = AirplaneResourceType.Airplanes,
115115
Attributes = new AirplaneAttributesInPatchRequest()
116116
}
117117
};
@@ -123,7 +123,7 @@ public async Task Request_is_unaffected_by_attribute_registration_for_different_
123123
Data = new AirplaneDataInPatchRequest
124124
{
125125
Id = airplaneId2,
126-
Type = AirplanesResourceType.Airplanes,
126+
Type = AirplaneResourceType.Airplanes,
127127
Attributes = new AirplaneAttributesInPatchRequest()
128128
}
129129
};
@@ -166,7 +166,7 @@ public async Task Attribute_values_can_be_changed_after_attribute_registration()
166166
Data = new AirplaneDataInPatchRequest
167167
{
168168
Id = airplaneId,
169-
Type = AirplanesResourceType.Airplanes,
169+
Type = AirplaneResourceType.Airplanes,
170170
Attributes = new AirplaneAttributesInPatchRequest
171171
{
172172
IsInMaintenance = true
@@ -209,7 +209,7 @@ public async Task Attribute_registration_is_unaffected_by_successive_attribute_r
209209
Data = new AirplaneDataInPatchRequest
210210
{
211211
Id = airplaneId1,
212-
Type = AirplanesResourceType.Airplanes,
212+
Type = AirplaneResourceType.Airplanes,
213213
Attributes = new AirplaneAttributesInPatchRequest()
214214
}
215215
};
@@ -218,7 +218,7 @@ public async Task Attribute_registration_is_unaffected_by_successive_attribute_r
218218
{
219219
Data = new AirplaneDataInPostRequest
220220
{
221-
Type = AirplanesResourceType.Airplanes,
221+
Type = AirplaneResourceType.Airplanes,
222222
Attributes = new AirplaneAttributesInPostRequest()
223223
}
224224
};
@@ -260,7 +260,7 @@ public async Task Attribute_registration_is_unaffected_by_preceding_disposed_att
260260
Data = new AirplaneDataInPatchRequest
261261
{
262262
Id = airplaneId1,
263-
Type = AirplanesResourceType.Airplanes,
263+
Type = AirplaneResourceType.Airplanes,
264264
Attributes = new AirplaneAttributesInPatchRequest()
265265
}
266266
};
@@ -278,7 +278,7 @@ public async Task Attribute_registration_is_unaffected_by_preceding_disposed_att
278278
Data = new AirplaneDataInPatchRequest
279279
{
280280
Id = airplaneId2,
281-
Type = AirplanesResourceType.Airplanes,
281+
Type = AirplaneResourceType.Airplanes,
282282
Attributes = new AirplaneAttributesInPatchRequest
283283
{
284284
ManufacturedInCity = "Everett"
@@ -319,7 +319,7 @@ public async Task Attribute_registration_is_unaffected_by_preceding_disposed_att
319319
{
320320
Data = new AirplaneDataInPostRequest
321321
{
322-
Type = AirplanesResourceType.Airplanes,
322+
Type = AirplaneResourceType.Airplanes,
323323
Attributes = new AirplaneAttributesInPostRequest()
324324
}
325325
};
@@ -337,7 +337,7 @@ public async Task Attribute_registration_is_unaffected_by_preceding_disposed_att
337337
Data = new AirplaneDataInPatchRequest
338338
{
339339
Id = airplaneId,
340-
Type = AirplanesResourceType.Airplanes,
340+
Type = AirplaneResourceType.Airplanes,
341341
Attributes = new AirplaneAttributesInPatchRequest
342342
{
343343
ManufacturedInCity = "Everett"
@@ -381,7 +381,7 @@ public async Task Attribute_registration_is_unaffected_by_preceding_attribute_re
381381
Data = new AirplaneDataInPatchRequest
382382
{
383383
Id = airplaneId1,
384-
Type = AirplanesResourceType.Airplanes,
384+
Type = AirplaneResourceType.Airplanes,
385385
Attributes = new AirplaneAttributesInPatchRequest()
386386
}
387387
};
@@ -393,7 +393,7 @@ public async Task Attribute_registration_is_unaffected_by_preceding_attribute_re
393393
Data = new AirplaneDataInPatchRequest
394394
{
395395
Id = airplaneId2,
396-
Type = AirplanesResourceType.Airplanes,
396+
Type = AirplaneResourceType.Airplanes,
397397
Attributes = new AirplaneAttributesInPatchRequest()
398398
}
399399
};

test/OpenApiClientTests/LegacyClient/RequestTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ public async Task Partial_posting_resource_with_selected_relationships_produces_
6363
{
6464
Data = new FlightDataInPostRequest
6565
{
66-
Type = FlightsResourceType.Flights,
66+
Type = FlightResourceType.Flights,
6767
Relationships = new FlightRelationshipsInPostRequest
6868
{
6969
Purser = new ToOneFlightAttendantInRequest
7070
{
7171
Data = new FlightAttendantIdentifier
7272
{
7373
Id = "bBJHu",
74-
Type = FlightAttendantsResourceType.FlightAttendants
74+
Type = FlightAttendantResourceType.FlightAttendants
7575
}
7676
},
7777
BackupPurser = new NullableToOneFlightAttendantInRequest
7878
{
7979
Data = new FlightAttendantIdentifier
8080
{
8181
Id = "NInmX",
82-
Type = FlightAttendantsResourceType.FlightAttendants
82+
Type = FlightAttendantResourceType.FlightAttendants
8383
}
8484
}
8585
}
@@ -143,7 +143,7 @@ public async Task Partial_posting_resource_produces_expected_request()
143143
{
144144
Data = new AirplaneDataInPostRequest
145145
{
146-
Type = AirplanesResourceType.Airplanes,
146+
Type = AirplaneResourceType.Airplanes,
147147
Attributes = new AirplaneAttributesInPostRequest
148148
{
149149
Name = name,
@@ -195,7 +195,7 @@ public async Task Partial_patching_resource_produces_expected_request()
195195
Data = new AirplaneDataInPatchRequest
196196
{
197197
Id = airplaneId,
198-
Type = AirplanesResourceType.Airplanes,
198+
Type = AirplaneResourceType.Airplanes,
199199
Attributes = new AirplaneAttributesInPatchRequest
200200
{
201201
LastServicedAt = lastServicedAt
@@ -326,7 +326,7 @@ public async Task Patching_ToOne_relationship_produces_expected_request()
326326
Data = new FlightAttendantIdentifier
327327
{
328328
Id = "bBJHu",
329-
Type = FlightAttendantsResourceType.FlightAttendants
329+
Type = FlightAttendantResourceType.FlightAttendants
330330
}
331331
};
332332

@@ -384,12 +384,12 @@ public async Task Posting_ToMany_relationship_produces_expected_request()
384384
{
385385
new()
386386
{
387-
Type = FlightAttendantsResourceType.FlightAttendants,
387+
Type = FlightAttendantResourceType.FlightAttendants,
388388
Id = "bBJHu"
389389
},
390390
new()
391391
{
392-
Type = FlightAttendantsResourceType.FlightAttendants,
392+
Type = FlightAttendantResourceType.FlightAttendants,
393393
Id = "NInmX"
394394
}
395395
}
@@ -436,12 +436,12 @@ public async Task Patching_ToMany_relationship_produces_expected_request()
436436
new()
437437
{
438438
Id = "bBJHu",
439-
Type = FlightAttendantsResourceType.FlightAttendants
439+
Type = FlightAttendantResourceType.FlightAttendants
440440
},
441441
new()
442442
{
443443
Id = "NInmX",
444-
Type = FlightAttendantsResourceType.FlightAttendants
444+
Type = FlightAttendantResourceType.FlightAttendants
445445
}
446446
}
447447
};
@@ -487,12 +487,12 @@ public async Task Deleting_ToMany_relationship_produces_expected_request()
487487
new()
488488
{
489489
Id = "bBJHu",
490-
Type = FlightAttendantsResourceType.FlightAttendants
490+
Type = FlightAttendantResourceType.FlightAttendants
491491
},
492492
new()
493493
{
494494
Id = "NInmX",
495-
Type = FlightAttendantsResourceType.FlightAttendants
495+
Type = FlightAttendantResourceType.FlightAttendants
496496
}
497497
}
498498
};

0 commit comments

Comments
 (0)