Skip to content

Commit 86c41f8

Browse files
authored
Merge pull request #453 from microsoft/dm/commandline
Create dotnet commandline tool for performing conversions and inlining
2 parents 6f5e63c + 8215249 commit 86c41f8

File tree

12 files changed

+159
-7
lines changed

12 files changed

+159
-7
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}

build.cmd

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

44
SET PROJ=%~dp0src\Microsoft.OpenApi\Microsoft.OpenApi.csproj
55
dotnet build %PROJ% /t:restore /p:Configuration=Release

src/Microsoft.OpenApi.Readers/ParsingContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ public class ParsingContext
3030
internal List<OpenApiTag> Tags { get; private set; } = new List<OpenApiTag>();
3131
internal Uri BaseUrl { get; set; }
3232

33+
/// <summary>
34+
/// Diagnostic object that returns metadata about the parsing process.
35+
/// </summary>
3336
public OpenApiDiagnostic Diagnostic { get; }
3437

38+
/// <summary>
39+
/// Create Parsing Context
40+
/// </summary>
41+
/// <param name="diagnostic">Provide instance for diagnotic object for collecting and accessing information about the parsing.</param>
3542
public ParsingContext(OpenApiDiagnostic diagnostic)
3643
{
3744
Diagnostic = diagnostic;
@@ -41,7 +48,6 @@ public ParsingContext(OpenApiDiagnostic diagnostic)
4148
/// Initiates the parsing process. Not thread safe and should only be called once on a parsing context
4249
/// </summary>
4350
/// <param name="yamlDocument">Yaml document to parse.</param>
44-
/// <param name="diagnostic">Diagnostic object which will return diagnostic results of the operation.</param>
4551
/// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
4652
internal OpenApiDocument Parse(YamlDocument yamlDocument)
4753
{
@@ -77,7 +83,6 @@ internal OpenApiDocument Parse(YamlDocument yamlDocument)
7783
/// </summary>
7884
/// <param name="yamlDocument"></param>
7985
/// <param name="version">OpenAPI version of the fragment</param>
80-
/// <param name="diagnostic">Diagnostic object which will return diagnostic results of the operation.</param>
8186
/// <returns>An OpenApiDocument populated based on the passed yamlDocument </returns>
8287
internal T ParseFragment<T>(YamlDocument yamlDocument, OpenApiSpecVersion version) where T : IOpenApiElement
8388
{
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
<Version>0.5.0</Version>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20104.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\Microsoft.OpenApi.Readers\Microsoft.OpenApi.Readers.csproj" />
18+
<ProjectReference Include="..\Microsoft.OpenApi\Microsoft.OpenApi.csproj" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="1.0.0" />
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.Models;
8+
using Microsoft.OpenApi.Readers;
9+
using Microsoft.OpenApi.Validations;
10+
using Microsoft.OpenApi.Writers;
11+
12+
namespace Microsoft.OpenApi.Tool
13+
{
14+
static class OpenApiService
15+
{
16+
public static void ProcessOpenApiDocument(
17+
FileInfo input,
18+
FileInfo output,
19+
OpenApiSpecVersion version,
20+
OpenApiFormat format,
21+
bool inline)
22+
{
23+
OpenApiDocument document;
24+
using (Stream stream = input.OpenRead())
25+
{
26+
27+
document = new OpenApiStreamReader(new OpenApiReaderSettings
28+
{
29+
ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences,
30+
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
31+
}
32+
).Read(stream, out var context);
33+
if (context.Errors.Count != 0)
34+
{
35+
var errorReport = new StringBuilder();
36+
37+
foreach (var error in context.Errors)
38+
{
39+
errorReport.AppendLine(error.ToString());
40+
}
41+
42+
throw new ArgumentException(String.Join(Environment.NewLine, context.Errors.Select(e => e.Message).ToArray()));
43+
}
44+
}
45+
46+
using (var outputStream = output?.Create())
47+
{
48+
TextWriter textWriter;
49+
50+
if (outputStream!=null)
51+
{
52+
textWriter = new StreamWriter(outputStream);
53+
} else
54+
{
55+
textWriter = Console.Out;
56+
}
57+
58+
var settings = new OpenApiWriterSettings()
59+
{
60+
ReferenceInline = inline == true ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
61+
};
62+
IOpenApiWriter writer;
63+
switch (format)
64+
{
65+
case OpenApiFormat.Json:
66+
writer = new OpenApiJsonWriter(textWriter, settings);
67+
break;
68+
case OpenApiFormat.Yaml:
69+
writer = new OpenApiYamlWriter(textWriter, settings);
70+
break;
71+
default:
72+
throw new ArgumentException("Unknown format");
73+
}
74+
75+
document.Serialize(writer,version );
76+
77+
textWriter.Flush();
78+
}
79+
}
80+
}
81+
}

src/Microsoft.OpenApi.Tool/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.CommandLine;
3+
using System.CommandLine.Invocation;
4+
using System.IO;
5+
using System.Threading.Tasks;
6+
using Microsoft.OpenApi;
7+
8+
namespace Microsoft.OpenApi.Tool
9+
{
10+
class Program
11+
{
12+
static async Task<int> Main(string[] args)
13+
{
14+
var command = new RootCommand
15+
{
16+
new Option("--input") { Argument = new Argument<FileInfo>() },
17+
new Option("--output") { Argument = new Argument<FileInfo>() },
18+
new Option("--version") { Argument = new Argument<OpenApiSpecVersion>() },
19+
new Option("--format") { Argument = new Argument<OpenApiFormat>() },
20+
new Option("--inline") { Argument = new Argument<bool>() }
21+
};
22+
23+
command.Handler = CommandHandler.Create<FileInfo,FileInfo,OpenApiSpecVersion,OpenApiFormat,bool>(
24+
OpenApiService.ProcessOpenApiDocument);
25+
26+
// Parse the incoming args and invoke the handler
27+
return await command.InvokeAsync(args);
28+
}
29+
}
30+
}

src/Microsoft.OpenApi/Any/OpenApiString.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class OpenApiString : OpenApiPrimitive<string>
1414
/// Initializes the <see cref="OpenApiString"/> class.
1515
/// </summary>
1616
/// <param name="value"></param>
17+
/// <param name="isExplicit">Used to indicate if a string is quoted.</param>
1718
public OpenApiString(string value, bool isExplicit = false)
1819
: base(value)
1920
{

src/Microsoft.OpenApi/Extensions/OpenApiSerializableExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static void SerializeAsYaml<T>(this T element, Stream stream, OpenApiSpec
5050
/// <param name="stream">The given stream.</param>
5151
/// <param name="specVersion">The Open API specification version.</param>
5252
/// <param name="format">The output format (JSON or YAML).</param>
53+
/// <param name="settings">Provide configuration settings for controlling writing output</param>
5354
public static void Serialize<T>(
5455
this T element,
5556
Stream stream,

src/Microsoft.OpenApi/Services/LoopDetector.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ internal class LoopDetector
1313
/// <summary>
1414
/// Maintain history of traversals to avoid stack overflows from cycles
1515
/// </summary>
16-
/// <param name="loopId">Any unique identifier for a stack.</param>
1716
/// <param name="key">Identifier used for current context.</param>
1817
/// <returns>If method returns false a loop was detected and the key is not added.</returns>
1918
public bool PushLoop<T>(T key)
@@ -39,7 +38,6 @@ public bool PushLoop<T>(T key)
3938
/// <summary>
4039
/// Exit from the context in cycle detection
4140
/// </summary>
42-
/// <param name="loopid">Identifier of loop</param>
4341
public void PopLoop<T>()
4442
{
4543
if (_loopStacks[typeof(T)].Count > 0)
@@ -65,7 +63,6 @@ public void SaveLoop<T>(T loop)
6563
/// <summary>
6664
/// Reset loop tracking stack
6765
/// </summary>
68-
/// <param name="loopid">Identifier of loop to clear</param>
6966
internal void ClearLoop<T>()
7067
{
7168
_loopStacks[typeof(T)].Clear();

src/Microsoft.OpenApi/Services/OpenApiDifferenceOperation.cs

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

44
namespace Microsoft.OpenApi.Services
55
{
6+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
7+
68
/// <summary>
79
/// The open api difference operation.
810
/// </summary>
@@ -12,4 +14,6 @@ public enum OpenApiDifferenceOperation
1214
Remove,
1315
Update
1416
}
17+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
18+
1519
}

0 commit comments

Comments
 (0)