Skip to content

Commit 2c3f6af

Browse files
authored
Merge pull request #652 from microsoft/vnext
Merge Cmd tool updates to master for release
2 parents 1374d77 + 0834f1f commit 2c3f6af

26 files changed

+1443
-229
lines changed

.azure-pipelines/ci-build.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ steps:
3030
inputs:
3131
testAssemblyVer2: |
3232
**\*.Tests.dll
33-
33+
3434
vsTestVersion: 16.0
3535
codeCoverageEnabled: true
3636

@@ -94,6 +94,13 @@ steps:
9494
configuration: Release
9595
msbuildArguments: '/t:pack /p:PackageOutputPath=$(Build.ArtifactStagingDirectory) /p:IncludeSymbols=true /p:SymbolPackageFormat=snupkg'
9696

97+
- task: MSBuild@1
98+
displayName: 'Pack OpenApi Hidi'
99+
inputs:
100+
solution: src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
101+
configuration: Release
102+
msbuildArguments: '/t:pack /p:PackageOutputPath=$(Build.ArtifactStagingDirectory) /p:IncludeSymbols=true /p:SymbolPackageFormat=snupkg'
103+
97104
- task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@1
98105
displayName: 'ESRP CodeSigning Nuget Packages'
99106
inputs:

.github/workflows/ci-cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
$projectsArray = @(
6363
'.\src\Microsoft.OpenApi\Microsoft.OpenApi.csproj',
6464
'.\src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj',
65-
'.\src\Microsoft.OpenApi.Tool\Microsoft.OpenApi.Tool.csproj'
65+
'.\src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj'
6666
)
6767
$gitNewVersion = if ("${{ steps.tag_generator.outputs.new_version }}") {"${{ steps.tag_generator.outputs.new_version }}"} else {$null}
6868
$projectCurrentVersion = ([xml](Get-Content .\src\Microsoft.OpenApi\Microsoft.OpenApi.csproj)).Project.PropertyGroup.Version

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
$projectsArray = @(
3434
'.\src\Microsoft.OpenApi\Microsoft.OpenApi.csproj',
3535
'.\src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj',
36-
'.\src\Microsoft.OpenApi.Tool\Microsoft.OpenApi.Tool.csproj'
36+
'.\src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj'
3737
)
3838
3939
$projectsArray | ForEach-Object {

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"request": "launch",
1111
"preLaunchTask": "build",
1212
// If you have changed target frameworks, make sure to update the program path.
13-
"program": "${workspaceFolder}/src/Microsoft.OpenApi.Tool/bin/Debug/netcoreapp3.1/Microsoft.OpenApi.Tool.dll",
13+
"program": "${workspaceFolder}/src/Microsoft.OpenApi.Hidi/bin/Debug/netcoreapp3.1/Microsoft.OpenApi.Hidi.dll",
1414
"args": [],
15-
"cwd": "${workspaceFolder}/src/Microsoft.OpenApi.Tool",
15+
"cwd": "${workspaceFolder}/src/Microsoft.OpenApi.Hidi",
1616
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
1717
"console": "internalConsole",
1818
"stopAtEntry": false

Microsoft.OpenApi.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{6357D7FD-2
2626
EndProject
2727
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.SmokeTests", "test\Microsoft.OpenApi.SmokeTests\Microsoft.OpenApi.SmokeTests.csproj", "{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}"
2828
EndProject
29-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.Tool", "src\Microsoft.OpenApi.Tool\Microsoft.OpenApi.Tool.csproj", "{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}"
29+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.Hidi", "src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj", "{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}"
3030
EndProject
3131
Global
3232
GlobalSection(SolutionConfigurationPlatforms) = preSolution

build.cmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
@echo off
2-
Echo Building Microsoft.OpenApi
2+
Echo Building Microsoft.OpenApi
33

4-
SET PROJ=%~dp0src\Microsoft.OpenApi\Microsoft.OpenApi.csproj
4+
SET PROJ=%~dp0src\Microsoft.OpenApi\Microsoft.OpenApi.csproj
55
dotnet msbuild %PROJ% /t:restore /p:Configuration=Release
66
dotnet msbuild %PROJ% /t:build /p:Configuration=Release
77
dotnet msbuild %PROJ% /t:pack /p:Configuration=Release;PackageOutputPath=%~dp0artifacts
88

99
Echo Building Microsoft.OpenApi.Readers
1010

11-
SET PROJ=%~dp0src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj
11+
SET PROJ=%~dp0src\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj
1212
dotnet msbuild %PROJ% /t:restore /p:Configuration=Release
1313
dotnet msbuild %PROJ% /t:build /p:Configuration=Release
1414
dotnet msbuild %PROJ% /t:pack /p:Configuration=Release;PackageOutputPath=%~dp0artifacts
1515

16-
Echo Building Microsoft.OpenApi.Tool
16+
Echo Building Microsoft.OpenApi.Hidi
1717

18-
SET PROJ=%~dp0src\Microsoft.OpenApi.Tool\Microsoft.OpenApi.Tool.csproj
18+
SET PROJ=%~dp0src\Microsoft.OpenApi.Hidi\Microsoft.OpenApi.Hidi.csproj
1919
dotnet msbuild %PROJ% /t:restore /p:Configuration=Release
2020
dotnet msbuild %PROJ% /t:build /p:Configuration=Release
2121
dotnet msbuild %PROJ% /t:pack /p:Configuration=Release;PackageOutputPath=%~dp0artifacts

install-tool.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
$latest = Get-ChildItem .\artifacts\ Microsoft.OpenApi.Tool* | select-object -Last 1
1+
$latest = Get-ChildItem .\artifacts\Microsoft.OpenApi.Hidi* | select-object -Last 1
22
$version = $latest.Name.Split(".")[3..5] | join-string -Separator "."
33

4-
if (Test-Path -Path ./artifacts/openapi-parser.exe) {
5-
dotnet tool uninstall --tool-path artifacts Microsoft.OpenApi.Tool
4+
if (Test-Path -Path ./artifacts/hidi.exe) {
5+
dotnet tool uninstall --tool-path artifacts Microsoft.OpenApi.Hidi
66
}
7-
dotnet tool install --tool-path artifacts --add-source .\artifacts\ --version $version Microsoft.OpenApi.Tool
7+
dotnet tool install --tool-path artifacts --add-source .\artifacts\ --version $version Microsoft.OpenApi.Hidi

src/Microsoft.OpenApi.Tool/Microsoft.OpenApi.Tool.csproj renamed to src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<PackAsTool>true</PackAsTool>
7-
<ToolCommandName>openapi-parser</ToolCommandName>
7+
<ToolCommandName>hidi</ToolCommandName>
88
<PackageOutputPath>./../../artifacts</PackageOutputPath>
9-
<Version>1.3.0-preview</Version>
9+
<Version>0.5.0-preview</Version>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

src/Microsoft.OpenApi.Tool/OpenApiService.cs renamed to src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
35
using System.IO;
46
using System.Linq;
57
using System.Net;
@@ -12,7 +14,7 @@
1214
using Microsoft.OpenApi.Validations;
1315
using Microsoft.OpenApi.Writers;
1416

15-
namespace Microsoft.OpenApi.Tool
17+
namespace Microsoft.OpenApi.Hidi
1618
{
1719
static class OpenApiService
1820
{
@@ -21,29 +23,55 @@ public static void ProcessOpenApiDocument(
2123
FileInfo output,
2224
OpenApiSpecVersion version,
2325
OpenApiFormat format,
26+
string filterByOperationIds,
27+
string filterByTags,
2428
bool inline,
2529
bool resolveExternal)
2630
{
27-
if (input == null)
31+
if (string.IsNullOrEmpty(input))
2832
{
29-
throw new ArgumentNullException("input");
33+
throw new ArgumentNullException(nameof(input));
34+
}
35+
if(output == null)
36+
{
37+
throw new ArgumentException(nameof(output));
38+
}
39+
if (output.Exists)
40+
{
41+
throw new IOException("The file you're writing to already exists. Please input a new output path.");
3042
}
3143

3244
var stream = GetStream(input);
33-
34-
OpenApiDocument document;
35-
3645
var result = new OpenApiStreamReader(new OpenApiReaderSettings
3746
{
38-
ReferenceResolution = resolveExternal == true ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
47+
ReferenceResolution = resolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
3948
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
4049
}
4150
).ReadAsync(stream).GetAwaiter().GetResult();
4251

52+
OpenApiDocument document;
4353
document = result.OpenApiDocument;
54+
55+
// Check if filter options are provided, then execute
56+
if (!string.IsNullOrEmpty(filterByOperationIds) && !string.IsNullOrEmpty(filterByTags))
57+
{
58+
throw new InvalidOperationException("Cannot filter by operationIds and tags at the same time.");
59+
}
60+
61+
if (!string.IsNullOrEmpty(filterByOperationIds))
62+
{
63+
var predicate = OpenApiFilterService.CreatePredicate(operationIds: filterByOperationIds);
64+
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
65+
}
66+
if (!string.IsNullOrEmpty(filterByTags))
67+
{
68+
var predicate = OpenApiFilterService.CreatePredicate(tags: filterByTags);
69+
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
70+
}
71+
4472
var context = result.OpenApiDiagnostic;
4573

46-
if (context.Errors.Count != 0)
74+
if (context.Errors.Count > 0)
4775
{
4876
var errorReport = new StringBuilder();
4977

@@ -52,43 +80,26 @@ public static void ProcessOpenApiDocument(
5280
errorReport.AppendLine(error.ToString());
5381
}
5482

55-
throw new ArgumentException(String.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray()));
83+
throw new ArgumentException(string.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray()));
5684
}
5785

58-
using (var outputStream = output?.Create())
59-
{
60-
TextWriter textWriter;
86+
using var outputStream = output?.Create();
6187

62-
if (outputStream != null)
63-
{
64-
textWriter = new StreamWriter(outputStream);
65-
}
66-
else
67-
{
68-
textWriter = Console.Out;
69-
}
88+
var textWriter = outputStream != null ? new StreamWriter(outputStream) : Console.Out;
7089

71-
var settings = new OpenApiWriterSettings()
72-
{
73-
ReferenceInline = inline == true ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
74-
};
75-
IOpenApiWriter writer;
76-
switch (format)
77-
{
78-
case OpenApiFormat.Json:
79-
writer = new OpenApiJsonWriter(textWriter, settings);
80-
break;
81-
case OpenApiFormat.Yaml:
82-
writer = new OpenApiYamlWriter(textWriter, settings);
83-
break;
84-
default:
85-
throw new ArgumentException("Unknown format");
86-
}
87-
88-
document.Serialize(writer, version);
90+
var settings = new OpenApiWriterSettings()
91+
{
92+
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
93+
};
94+
IOpenApiWriter writer = format switch
95+
{
96+
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
97+
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
98+
_ => throw new ArgumentException("Unknown format"),
99+
};
100+
document.Serialize(writer, version);
89101

90-
textWriter.Flush();
91-
}
102+
textWriter.Flush();
92103
}
93104

94105
private static Stream GetStream(string input)
@@ -127,7 +138,6 @@ internal static void ValidateOpenApiDocument(string input)
127138

128139
document = new OpenApiStreamReader(new OpenApiReaderSettings
129140
{
130-
//ReferenceResolution = resolveExternal == true ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
131141
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
132142
}
133143
).Read(stream, out var context);

src/Microsoft.OpenApi.Tool/Program.cs renamed to src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,15 @@
1-
using System;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
24
using System.CommandLine;
35
using System.CommandLine.Invocation;
46
using System.IO;
57
using System.Threading.Tasks;
6-
using Microsoft.OpenApi;
78

8-
namespace Microsoft.OpenApi.Tool
9+
namespace Microsoft.OpenApi.Hidi
910
{
10-
class Program
11+
static class Program
1112
{
12-
static async Task<int> OldMain(string[] args)
13-
{
14-
15-
var command = new RootCommand
16-
{
17-
new Option("--input", "Input OpenAPI description file path or URL", typeof(string) ),
18-
new Option("--output","Output OpenAPI description file", typeof(FileInfo), arity: ArgumentArity.ZeroOrOne),
19-
new Option("--version", "OpenAPI specification version", typeof(OpenApiSpecVersion)),
20-
new Option("--format", "File format",typeof(OpenApiFormat) ),
21-
new Option("--inline", "Inline $ref instances", typeof(bool) ),
22-
new Option("--resolveExternal","Resolve external $refs", typeof(bool))
23-
};
24-
25-
command.Handler = CommandHandler.Create<string,FileInfo,OpenApiSpecVersion,OpenApiFormat,bool, bool>(
26-
OpenApiService.ProcessOpenApiDocument);
27-
28-
// Parse the incoming args and invoke the handler
29-
return await command.InvokeAsync(args);
30-
}
31-
3213
static async Task<int> Main(string[] args)
3314
{
3415
var rootCommand = new RootCommand() {
@@ -47,9 +28,11 @@ static async Task<int> Main(string[] args)
4728
new Option("--version", "OpenAPI specification version", typeof(OpenApiSpecVersion)),
4829
new Option("--format", "File format",typeof(OpenApiFormat) ),
4930
new Option("--inline", "Inline $ref instances", typeof(bool) ),
50-
new Option("--resolveExternal","Resolve external $refs", typeof(bool))
31+
new Option("--resolveExternal","Resolve external $refs", typeof(bool)),
32+
new Option("--filterByOperationIds", "Filters OpenApiDocument by OperationId(s) provided", typeof(string)),
33+
new Option("--filterByTags", "Filters OpenApiDocument by Tag(s) provided", typeof(string))
5134
};
52-
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion, OpenApiFormat, bool, bool>(
35+
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion, OpenApiFormat, string, string, bool, bool>(
5336
OpenApiService.ProcessOpenApiDocument);
5437

5538
rootCommand.Add(transformCommand);

0 commit comments

Comments
 (0)