Skip to content

Commit 479a3fb

Browse files
committed
fix: add IsOperationNamePluralized2 for now (to handle getDeviceDetailsById)
1 parent 05ed627 commit 479a3fb

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

.claude/settings.local.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(dotnet restore:*)",
5+
"Bash(dotnet build:*)",
6+
"Bash(dotnet clean:*)"
7+
],
8+
"deny": [],
9+
"ask": []
10+
}
11+
}

src/Atc.Rest.ApiGenerator.OpenApi/Extensions/OpenApiOperationExtensions.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,83 @@ public static bool IsAsyncEnumerableEnabled(
248248
this OpenApiOperation operation)
249249
=> operation.Extensions.TryGetValue("x-return-async-enumerable", out var extension) &&
250250
extension is OpenApiBoolean { Value: true };
251+
252+
//// ! This from Atc.OpenApi.OpenApiOperationExtensions - remove this when OpenApi in this project is updated!
253+
private const string RegexPatternUppercase = @"(?<!^)(?=[A-Z])";
254+
255+
//// ! This from Atc.OpenApi.OpenApiOperationExtensions - remove this when OpenApi in this project is updated!
256+
public static bool IsOperationNamePluralized2(
257+
this OpenApiOperation openApiOperation,
258+
OperationType operationType)
259+
{
260+
if (openApiOperation is null)
261+
{
262+
throw new ArgumentNullException(nameof(openApiOperation));
263+
}
264+
265+
var operationName = openApiOperation.GetOperationName();
266+
267+
// Remove Http-verb
268+
if (operationName.StartsWith(operationType.ToString(), StringComparison.Ordinal))
269+
{
270+
operationName = operationName[operationType.ToString().Length..];
271+
}
272+
273+
// Split by uppercase
274+
var sa = System.Text.RegularExpressions.Regex.Split(operationName, RegexPatternUppercase, System.Text.RegularExpressions.RegexOptions.None, TimeSpan.FromSeconds(1));
275+
if (sa.Length > 0)
276+
{
277+
// Test for last-term
278+
var termWord = sa[^1];
279+
if (termWord.EndsWith('s') &&
280+
!(termWord.Equals("Ids", StringComparison.Ordinal) ||
281+
termWord.Equals("Identifiers", StringComparison.Ordinal) ||
282+
termWord.Equals("Details", StringComparison.Ordinal) ||
283+
termWord.Equals("Status", StringComparison.Ordinal)))
284+
{
285+
return true;
286+
}
287+
288+
// Test for first-term
289+
termWord = sa[0];
290+
if (termWord.EndsWith('s'))
291+
{
292+
return true;
293+
}
294+
295+
if (sa.Any(x => x.Equals("By", StringComparison.Ordinal)))
296+
{
297+
var index = Array.IndexOf(sa, "By");
298+
if (index > 1)
299+
{
300+
termWord = sa[index - 1];
301+
if (termWord.Equals("Details", StringComparison.Ordinal) ||
302+
termWord.Equals("Status", StringComparison.Ordinal))
303+
{
304+
return false;
305+
}
306+
307+
if (termWord.EndsWith('s'))
308+
{
309+
return true;
310+
}
311+
}
312+
}
313+
}
314+
315+
return false;
316+
}
317+
318+
//// ! This from Atc.OpenApi.OpenApiOperationExtensions - remove this when OpenApi in this project is updated!
319+
public static bool IsOperationIdPluralized2(
320+
this OpenApiOperation openApiOperation,
321+
OperationType operationType)
322+
{
323+
if (openApiOperation is null)
324+
{
325+
throw new ArgumentNullException(nameof(openApiOperation));
326+
}
327+
328+
return IsOperationNamePluralized2(openApiOperation, operationType);
329+
}
251330
}

src/Atc.Rest.ApiGenerator.OpenApi/Validators/OpenApiDocumentValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ private void ValidateOperations(
500500
var responseModelSchema = operationValue.GetModelSchemaFromResponse();
501501
if (responseModelSchema is not null)
502502
{
503-
if (operationValue.IsOperationIdPluralized(operationKey))
503+
if (operationValue.IsOperationIdPluralized2(operationKey))
504504
{
505505
if (!responseModelSchema.IsModelOfTypeArray(modelSchemas) &&
506506
!responseModelSchema.IsTypeCustomPagination())

0 commit comments

Comments
 (0)