Skip to content

Commit 3e4bf01

Browse files
committed
Add PowerShellFormatter tests.
1 parent a2ee29d commit 3e4bf01

File tree

6 files changed

+166
-11
lines changed

6 files changed

+166
-11
lines changed

src/Microsoft.OpenApi/Services/OpenApiFilterService.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private static void ValidateFilters(IDictionary<string, List<string>> requestUrl
347347
Debug.WriteLine($"The url {url.Key} could not be found in the OpenApi description");
348348
continue;
349349
}
350-
operationTypes = GetOperationTypes(openApiOperations, url.Value, path);
350+
operationTypes.AddRange(GetOperationTypes(openApiOperations, url.Value, path));
351351
}
352352
}
353353

@@ -362,16 +362,10 @@ private static void ValidateFilters(IDictionary<string, List<string>> requestUrl
362362

363363
private static List<string> GetOperationTypes(IDictionary<OperationType, OpenApiOperation> openApiOperations, List<string> url, string path)
364364
{
365-
var operationTypes = new List<string>();
366365
// Add the available ops if they are in the postman collection. See path.Value
367-
foreach (var ops in openApiOperations)
368-
{
369-
if (url.Contains(ops.Key.ToString().ToUpper()))
370-
{
371-
operationTypes.Add(ops.Key + path);
372-
}
373-
}
374-
return operationTypes;
366+
return openApiOperations.Where(ops => url.Contains(ops.Key.ToString().ToUpper()))
367+
.Select(ops => ops.Key + path)
368+
.ToList();
375369
}
376370
}
377371
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using Microsoft.OpenApi.Hidi.Formatters;
2+
using Microsoft.OpenApi.Models;
3+
using Microsoft.OpenApi.Services;
4+
using Xunit;
5+
6+
namespace Microsoft.OpenApi.Hidi.Tests.Formatters
7+
{
8+
public class PowerShellFormatterTests
9+
{
10+
[Theory]
11+
[InlineData("drives.drive.ListDrive", "drive_ListDrive", OperationType.Get)]
12+
[InlineData("print.taskDefinitions.tasks.GetTrigger", "print.taskDefinition.task_GetTrigger", OperationType.Get)]
13+
[InlineData("groups.sites.termStore.groups.GetSets", "group.site.termStore.group_GetSet", OperationType.Get)]
14+
[InlineData("external.industryData.ListDataConnectors", "external.industryData_ListDataConnector", OperationType.Get)]
15+
[InlineData("applications.application.UpdateLogo", "application_SetLogo", OperationType.Put)]
16+
[InlineData("identityGovernance.lifecycleWorkflows.workflows.workflow.activate", "identityGovernance.lifecycleWorkflow.workflow_activate", OperationType.Post)]
17+
[InlineData("directory.GetDeletedItems.AsApplication", "directory_GetDeletedItemAsApplication", OperationType.Get)]
18+
[InlineData("education.users.GetCount-6be9", "education.user_GetCount", OperationType.Get)]
19+
public void FormatOperationIdsInOpenAPIDocument(string operationId, string expectedOperationId, OperationType operationType, string path = "/foo")
20+
{
21+
// Arrange
22+
var openApiDocument = new OpenApiDocument()
23+
{
24+
Info = new() { Title = "Test", Version = "1.0" },
25+
Servers = new List<OpenApiServer>() { new() { Url = "https://localhost/" } },
26+
Paths = new()
27+
{
28+
{ path, new() {
29+
Operations = new Dictionary<OperationType, OpenApiOperation>() {
30+
{ operationType, new() { OperationId = operationId } }
31+
}
32+
}
33+
}
34+
}
35+
};
36+
37+
// Act
38+
var powerShellFormatter = new PowerShellFormatter();
39+
var walker = new OpenApiWalker(powerShellFormatter);
40+
walker.Walk(openApiDocument);
41+
42+
// Assert
43+
Assert.Equal(expectedOperationId, openApiDocument.Paths[path].Operations[operationType].OperationId);
44+
}
45+
46+
public void RemoveAnyOfAndOneOfFromSchema()
47+
{
48+
// Arrange
49+
var openApiDocument = new OpenApiDocument()
50+
{
51+
Info = new() { Title = "Test", Version = "1.0" },
52+
Servers = new List<OpenApiServer>() { new() { Url = "https://localhost/" } },
53+
Paths = new() { },
54+
Components = new()
55+
{
56+
Schemas = new Dictionary<string, OpenApiSchema>
57+
{
58+
{ "TestSchema", new OpenApiSchema
59+
{
60+
Type = "object",
61+
Properties = new Dictionary<string, OpenApiSchema>
62+
{
63+
{
64+
"averageAudioDegradation", new OpenApiSchema
65+
{
66+
AnyOf = new List<OpenApiSchema>
67+
{
68+
new OpenApiSchema { Type = "number" },
69+
new OpenApiSchema { Type = "string" }
70+
},
71+
Format = "float",
72+
Nullable = true
73+
}
74+
},
75+
{
76+
"defaultPrice", new OpenApiSchema
77+
{
78+
OneOf = new List<OpenApiSchema>
79+
{
80+
new OpenApiSchema { Type = "number", Format = "double" },
81+
new OpenApiSchema { Type = "string" }
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}
90+
};
91+
92+
// Act
93+
var powerShellFormatter = new PowerShellFormatter();
94+
var walker = new OpenApiWalker(powerShellFormatter);
95+
walker.Walk(openApiDocument);
96+
97+
var averageAudioDegradationProperty = openApiDocument.Components.Schemas["TestSchema"].Properties["averageAudioDegradation"];
98+
var defaultPriceProperty = openApiDocument.Components.Schemas["TestSchema"].Properties["defaultPrice"];
99+
100+
// Assert
101+
Assert.Null(averageAudioDegradationProperty.AnyOf);
102+
Assert.Equal("number", averageAudioDegradationProperty.Type);
103+
Assert.Equal("float", averageAudioDegradationProperty.Format);
104+
Assert.True(averageAudioDegradationProperty.Nullable);
105+
Assert.Null(defaultPriceProperty.OneOf);
106+
Assert.Equal("number", defaultPriceProperty.Type);
107+
Assert.Equal("double", defaultPriceProperty.Format);
108+
}
109+
}
110+
}

test/Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net7.0</TargetFramework>
@@ -59,6 +59,9 @@
5959
<None Update="UtilityFiles\Todo.xml">
6060
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
6161
</None>
62+
<None Update="UtilityFiles\examplepowershellsettings.json">
63+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
64+
</None>
6265
</ItemGroup>
6366

6467
</Project>

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.CommandLine;
55
using System.CommandLine.Invocation;
6+
using System.CommandLine.Parsing;
67
using Microsoft.Extensions.Configuration;
78
using Microsoft.Extensions.Logging;
89
using Microsoft.OpenApi.Hidi.Options;
@@ -295,6 +296,28 @@ await Assert.ThrowsAsync<ArgumentException>(async () =>
295296

296297
}
297298

299+
[Fact]
300+
public async Task TransformToPowerShellCompliantOpenApi()
301+
{
302+
var settingsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UtilityFiles\\examplepowershellsettings.json");
303+
HidiOptions options = new HidiOptions
304+
{
305+
OpenApi = "UtilityFiles\\SampleOpenApi.yml",
306+
CleanOutput = true,
307+
Version = "3.0",
308+
OpenApiFormat = OpenApiFormat.Yaml,
309+
TerseOutput = false,
310+
InlineLocal = false,
311+
InlineExternal = false,
312+
SettingsConfig = SettingsUtilities.GetConfiguration(settingsPath)
313+
};
314+
// create a dummy ILogger instance for testing
315+
await OpenApiService.TransformOpenApiDocument(options, _logger, new CancellationToken());
316+
317+
var output = File.ReadAllText("output.yml");
318+
Assert.NotEmpty(output);
319+
}
320+
298321
[Fact]
299322
public void InvokeTransformCommand()
300323
{

test/Microsoft.OpenApi.Hidi.Tests/UtilityFiles/SampleOpenApi.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ info:
55
paths:
66
/api/editresource:
77
get:
8+
operationId: api.ListEditresource
89
responses:
910
'200':
1011
description: OK
1112
patch:
13+
operationId: api.UpdateEditresource
1214
responses:
1315
'200':
1416
description: OK
1517
/api/viewresource:
1618
get:
19+
operationId: api.ListViewresource
1720
responses:
1821
'200':
1922
description: OK
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"OpenApiConvertSettings": {
3+
"AddSingleQuotesForStringParameters": true,
4+
"AddEnumDescriptionExtension": true,
5+
"EnableKeyAsSegment": true,
6+
"EnableOperationId": true,
7+
"PrefixEntityTypeNameBeforeKey": true,
8+
"TagDepth": 2,
9+
"EnablePagination": true,
10+
"EnableDiscriminatorValue": false,
11+
"EnableDerivedTypesReferencesForRequestBody": false,
12+
"EnableDerivedTypesReferencesForResponses": false,
13+
"ShowRootPath": false,
14+
"ExpandDerivedTypesNavigationProperties": false,
15+
"ShowLinks": false,
16+
"DeclarePathParametersOnPathItem": false,
17+
"EnableODataAnnotationReferencesForResponses": false,
18+
"EnableODataTypeCast": true,
19+
"UseSuccessStatusCodeRange": true
20+
},
21+
"LanguageFormat": "PowerShell"
22+
}

0 commit comments

Comments
 (0)