Skip to content

Commit 6926b6d

Browse files
committed
First running version of OpenApi tool
1 parent ab91774 commit 6926b6d

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

src/Microsoft.OpenApi.Tool/Microsoft.OpenApi.Tool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<PackAsTool>true</PackAsTool>
77
<ToolCommandName>openapi</ToolCommandName>
88
<PackageOutputPath>./../../artifacts</PackageOutputPath>
9+
<Version>0.5.0</Version>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Microsoft.OpenApi.Tool/OpenApiService.cs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text;
66
using Microsoft.OpenApi.Extensions;
7+
using Microsoft.OpenApi.Models;
78
using Microsoft.OpenApi.Readers;
89
using Microsoft.OpenApi.Validations;
910
using Microsoft.OpenApi.Writers;
@@ -13,21 +14,22 @@ namespace Microsoft.OpenApi.Tool
1314
static class OpenApiService
1415
{
1516
public static void ProcessOpenApiDocument(
16-
FileInfo fileOption,
17-
string outputPath,
17+
FileInfo input,
18+
FileInfo output,
1819
OpenApiSpecVersion version,
1920
OpenApiFormat format,
20-
bool inline = false)
21+
bool inline)
2122
{
22-
Stream stream = fileOption.OpenRead();
23+
OpenApiDocument document;
24+
using (Stream stream = input.OpenRead())
25+
{
2326

24-
var document = new OpenApiStreamReader(new OpenApiReaderSettings
27+
document = new OpenApiStreamReader(new OpenApiReaderSettings
2528
{
2629
ReferenceResolution = ReferenceResolutionSetting.ResolveLocalReferences,
2730
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
2831
}
2932
).Read(stream, out var context);
30-
3133
if (context.Errors.Count != 0)
3234
{
3335
var errorReport = new StringBuilder();
@@ -38,21 +40,41 @@ public static void ProcessOpenApiDocument(
3840
}
3941

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

43-
using (var outputStream = new FileStream(outputPath, FileMode.Create))
46+
using (var outputStream = output?.Create())
4447
{
45-
document.Serialize(
46-
outputStream,
47-
version,
48-
format,
49-
new OpenApiWriterSettings()
50-
{
51-
ReferenceInline = inline == true ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
52-
});
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 );
5376

54-
outputStream.Position = 0;
55-
outputStream.Flush();
77+
textWriter.Flush();
5678
}
5779
}
5880
}

src/Microsoft.OpenApi.Tool/Program.cs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,29 @@
22
using System.CommandLine;
33
using System.CommandLine.Invocation;
44
using System.IO;
5+
using System.Threading.Tasks;
56
using Microsoft.OpenApi;
67

78
namespace Microsoft.OpenApi.Tool
89
{
910
class Program
1011
{
11-
static int Main(string[] args)
12+
static async Task<int> Main(string[] args)
1213
{
13-
var rootCommand = new RootCommand
14+
var command = new RootCommand
1415
{
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-
}
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>() }
3921
};
4022

41-
rootCommand.Description = "OpenAPI";
42-
43-
rootCommand.Handler = CommandHandler.Create<FileInfo,string,OpenApiSpecVersion,OpenApiFormat, bool>(
44-
OpenApiService.ProcessOpenApiDocument);
23+
command.Handler = CommandHandler.Create<FileInfo,FileInfo,OpenApiSpecVersion,OpenApiFormat,bool>(
24+
OpenApiService.ProcessOpenApiDocument);
4525

4626
// Parse the incoming args and invoke the handler
47-
return rootCommand.InvokeAsync(args).Result;
27+
return await command.InvokeAsync(args);
4828
}
4929
}
5030
}

0 commit comments

Comments
 (0)