Skip to content

Commit 9f715d4

Browse files
committed
Initial implementation of OpenAPI cmdline tool
1 parent 3516127 commit 9f715d4

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

Microsoft.OpenApi.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ 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}"
30+
EndProject
2931
Global
3032
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3133
Debug|Any CPU = Debug|Any CPU
@@ -56,6 +58,10 @@ Global
5658
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
5759
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
5860
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Release|Any CPU.Build.0 = Release|Any CPU
61+
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
62+
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
63+
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
64+
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Release|Any CPU.Build.0 = Release|Any CPU
5965
EndGlobalSection
6066
GlobalSection(SolutionProperties) = preSolution
6167
HideSolutionNode = FALSE
@@ -67,6 +73,7 @@ Global
6773
{AD83F991-DBF3-4251-8613-9CC54C826964} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
6874
{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
6975
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
76+
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE} = {E546B92F-20A8-49C3-8323-4B25BB78F3E1}
7077
EndGlobalSection
7178
GlobalSection(ExtensibilityGlobals) = postSolution
7279
SolutionGuid = {9F171EFC-0DB5-4B10-ABFA-AF48D52CC565}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<PackAsTool>true</PackAsTool>
7+
<ToolCommandName>openapi</ToolCommandName>
8+
<PackageOutputPath>./../../artifacts</PackageOutputPath>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19577.1" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj" />
17+
<ProjectReference Include="..\Microsoft.OpenApi\Microsoft.OpenApi.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using Microsoft.OpenApi.Extensions;
7+
using Microsoft.OpenApi.Readers;
8+
using Microsoft.OpenApi.Validations;
9+
using Microsoft.OpenApi.Writers;
10+
11+
namespace Microsoft.OpenApi.Tool
12+
{
13+
static class OpenApiService
14+
{
15+
public static void ProcessOpenApiDocument(
16+
FileInfo fileOption,
17+
string outputPath,
18+
OpenApiSpecVersion version,
19+
OpenApiFormat format,
20+
bool inline = false)
21+
{
22+
Stream stream = fileOption.OpenRead();
23+
24+
var document = new OpenApiStreamReader(new OpenApiReaderSettings
25+
{
26+
ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences,
27+
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
28+
}
29+
).Read(stream, out var context);
30+
31+
if (context.Errors.Count != 0)
32+
{
33+
var errorReport = new StringBuilder();
34+
35+
foreach (var error in context.Errors)
36+
{
37+
errorReport.AppendLine(error.ToString());
38+
}
39+
40+
throw new ArgumentException(String.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray()));
41+
}
42+
43+
using (var outputStream = new FileStream(outputPath, FileMode.Create))
44+
{
45+
document.Serialize(
46+
outputStream,
47+
version,
48+
format,
49+
new OpenApiWriterSettings()
50+
{
51+
ReferenceInline = inline == true ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
52+
});
53+
54+
outputStream.Position = 0;
55+
outputStream.Flush();
56+
}
57+
}
58+
}
59+
}

src/Microsoft.OpenApi.Tool/Program.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.CommandLine;
3+
using System.CommandLine.Invocation;
4+
using System.IO;
5+
using Microsoft.OpenApi;
6+
7+
namespace Microsoft.OpenApi.Tool
8+
{
9+
class Program
10+
{
11+
static int Main(string[] args)
12+
{
13+
var rootCommand = new RootCommand
14+
{
15+
new Option(
16+
"--input",
17+
"Input OpenAPI description")
18+
{
19+
Argument = new Argument<FileInfo>()
20+
},
21+
new Option(
22+
"--output",
23+
"Output path for OpenAPI Description")
24+
{
25+
Argument = new Argument<string>()
26+
},
27+
new Option(
28+
"--output-version",
29+
"OpenAPI Version")
30+
{
31+
Argument = new Argument<OpenApiSpecVersion>(() => OpenApiSpecVersion.OpenApi3_0)
32+
},
33+
new Option(
34+
"--output-format",
35+
"OpenAPI format [Json | Yaml")
36+
{
37+
Argument = new Argument<OpenApiFormat>(() => OpenApiFormat.Yaml )
38+
}
39+
};
40+
41+
rootCommand.Description = "OpenAPI";
42+
43+
rootCommand.Handler = CommandHandler.Create<FileInfo,string,OpenApiSpecVersion,OpenApiFormat, bool>(
44+
OpenApiService.ProcessOpenApiDocument);
45+
46+
// Parse the incoming args and invoke the handler
47+
return rootCommand.InvokeAsync(args).Result;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)