Skip to content

Commit

Permalink
Merge pull request #1465 from microsoftgraph/fix/is/singularize-drives
Browse files Browse the repository at this point in the history
Add entry for `drives` to Humanizer Vocabulary to properly singularize to `drive`
  • Loading branch information
irvinesunday authored Mar 31, 2023
2 parents 7ad72d2 + 3a936b6 commit 2f0c4f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
31 changes: 31 additions & 0 deletions OpenAPIService.Test/OpenAPIDocumentCreatorMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,37 @@ private OpenApiDocument CreateOpenApiDocument()
}
}
},
["/drives"] = new OpenApiPathItem()
{
Operations = new Dictionary<OperationType, OpenApiOperation>
{
{
OperationType.Get, new OpenApiOperation
{
Tags = new List<OpenApiTag>
{
{
new OpenApiTag()
{
Name = "drives.drive"
}
}
},
OperationId = "drives.drive.ListDrive",
Summary = "Get Drive",
Responses = new OpenApiResponses()
{
{
"200", new OpenApiResponse()
{
Description = "Success"
}
}
}
}
}
}
},
["/security/hostSecurityProfiles"] = new OpenApiPathItem()
{
Operations = new Dictionary<OperationType, OpenApiOperation>
Expand Down
25 changes: 24 additions & 1 deletion OpenAPIService.Test/OpenAPIServiceShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public void RetrieveAllOperationsAndPaths()
subsetOpenApiDocument = _openApiService.ApplyStyle(OpenApiStyle.Plain, subsetOpenApiDocument);

// Assert
Assert.Equal(16, subsetOpenApiDocument.Paths.Count);
Assert.Equal(17, subsetOpenApiDocument.Paths.Count);
Assert.NotEmpty(subsetOpenApiDocument.Components.Schemas);
Assert.NotEmpty(subsetOpenApiDocument.Components.Parameters);
Assert.NotEmpty(subsetOpenApiDocument.Components.Responses);
Expand Down Expand Up @@ -412,6 +412,29 @@ public void ResolveActionFunctionOperationIdsForPowerShellStyle(string url, Oper
Assert.Equal(expectedOperationId, operationId);
}

[Theory]
[InlineData("drives.drive.ListDrive", "drive_ListDrive", OperationType.Get)]
[InlineData("applications.application.UpdateLogo", "application_SetLogo", OperationType.Put)]
public void SingularizeAndDeduplicateOperationIdsForPowerShellStyle(string operationId, string expectedOperationId, OperationType operationType)
{
// Act
var predicate = _openApiService.CreatePredicate(operationIds: operationId,
tags: null,
url: null,
source: _graphMockSource,
graphVersion: GraphVersion);

var subsetOpenApiDocument = _openApiService.CreateFilteredDocument(_graphMockSource, Title, GraphVersion, predicate);
subsetOpenApiDocument = _openApiService.ApplyStyle(OpenApiStyle.PowerShell, subsetOpenApiDocument, singularizeOperationIds: true);
var singularizedOpId = subsetOpenApiDocument.Paths
.FirstOrDefault().Value
.Operations[operationType]
.OperationId;

// Assert
Assert.Equal(expectedOperationId, singularizedOpId);
}

[Fact]
public void ResolveStructuredAndCollectionValuedFunctionParameters()
{
Expand Down
6 changes: 5 additions & 1 deletion OpenAPIService/PowershellFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------------------------------------------------------------------------------

using Humanizer;
using Humanizer.Inflections;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;
Expand Down Expand Up @@ -216,6 +217,9 @@ private static string SingularizeAndDeduplicateOperationId(string operationId)
if (string.IsNullOrEmpty(operationId))
return operationId;

// drives does not properly singularize to drive.
Vocabularies.Default.AddSingular("(drive)s$", "$1");

var segments = operationId.Split('.').ToList();

// The last segment is ignored as a rule.
Expand Down

0 comments on commit 2f0c4f3

Please sign in to comment.