Skip to content
Next Next commit
Initial implementation of OpenAPI cmdline tool
  • Loading branch information
darrelmiller committed Jan 20, 2020
commit 9f715d4c6ccf6fb7f34296fbb57b722f86ab5937
7 changes: 7 additions & 0 deletions Microsoft.OpenApi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{6357D7FD-2
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.SmokeTests", "test\Microsoft.OpenApi.SmokeTests\Microsoft.OpenApi.SmokeTests.csproj", "{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.OpenApi.Tool", "src\Microsoft.OpenApi.Tool\Microsoft.OpenApi.Tool.csproj", "{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +58,10 @@ Global
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC}.Release|Any CPU.Build.0 = Release|Any CPU
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -67,6 +73,7 @@ Global
{AD83F991-DBF3-4251-8613-9CC54C826964} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
{1ED3C2C1-E1E7-4925-B4E6-2D969C3F5237} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
{AD79B61D-88CF-497C-9ED5-41AE3867C5AC} = {6357D7FD-2DE4-4900-ADB9-ABC37052040A}
{254841B5-7DAC-4D1D-A9C5-44FE5CE467BE} = {E546B92F-20A8-49C3-8323-4B25BB78F3E1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9F171EFC-0DB5-4B10-ABFA-AF48D52CC565}
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.OpenApi.Tool/Microsoft.OpenApi.Tool.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PackAsTool>true</PackAsTool>
<ToolCommandName>openapi</ToolCommandName>
<PackageOutputPath>./../../artifacts</PackageOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19577.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj" />
<ProjectReference Include="..\Microsoft.OpenApi\Microsoft.OpenApi.csproj" />
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions src/Microsoft.OpenApi.Tool/OpenApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Validations;
using Microsoft.OpenApi.Writers;

namespace Microsoft.OpenApi.Tool
{
static class OpenApiService
{
public static void ProcessOpenApiDocument(
FileInfo fileOption,
string outputPath,
OpenApiSpecVersion version,
OpenApiFormat format,
bool inline = false)
{
Stream stream = fileOption.OpenRead();

var document = new OpenApiStreamReader(new OpenApiReaderSettings
{
ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences,
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
}
).Read(stream, out var context);

if (context.Errors.Count != 0)
{
var errorReport = new StringBuilder();

foreach (var error in context.Errors)
{
errorReport.AppendLine(error.ToString());
}

throw new ArgumentException(String.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray()));
}

using (var outputStream = new FileStream(outputPath, FileMode.Create))
{
document.Serialize(
outputStream,
version,
format,
new OpenApiWriterSettings()
{
ReferenceInline = inline == true ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
});

outputStream.Position = 0;
outputStream.Flush();
}
}
}
}
50 changes: 50 additions & 0 deletions src/Microsoft.OpenApi.Tool/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using Microsoft.OpenApi;

namespace Microsoft.OpenApi.Tool
{
class Program
{
static int Main(string[] args)
{
var rootCommand = new RootCommand
{
new Option(
"--input",
"Input OpenAPI description")
{
Argument = new Argument<FileInfo>()
},
new Option(
"--output",
"Output path for OpenAPI Description")
{
Argument = new Argument<string>()
},
new Option(
"--output-version",
"OpenAPI Version")
{
Argument = new Argument<OpenApiSpecVersion>(() => OpenApiSpecVersion.OpenApi3_0)
},
new Option(
"--output-format",
"OpenAPI format [Json | Yaml")
{
Argument = new Argument<OpenApiFormat>(() => OpenApiFormat.Yaml )
}
};

rootCommand.Description = "OpenAPI";

rootCommand.Handler = CommandHandler.Create<FileInfo,string,OpenApiSpecVersion,OpenApiFormat, bool>(
OpenApiService.ProcessOpenApiDocument);

// Parse the incoming args and invoke the handler
return rootCommand.InvokeAsync(args).Result;
}
}
}