Skip to content

Commit 03ab201

Browse files
committed
Use input file OpenApi format as the default output format
1 parent ff584c7 commit 03ab201

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void ProcessOpenApiDocument(
2222
string input,
2323
FileInfo output,
2424
OpenApiSpecVersion version,
25-
OpenApiFormat format,
25+
OpenApiFormat? format,
2626
string filterByOperationIds,
2727
string filterByTags,
2828
bool inline,
@@ -91,7 +91,9 @@ public static void ProcessOpenApiDocument(
9191
{
9292
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
9393
};
94-
IOpenApiWriter writer = format switch
94+
95+
var openApiFormat = format ?? GetOpenApiFormat(input);
96+
IOpenApiWriter writer = openApiFormat switch
9597
{
9698
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
9799
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
@@ -156,5 +158,17 @@ internal static void ValidateOpenApiDocument(string input)
156158

157159
Console.WriteLine(statsVisitor.GetStatisticsReport());
158160
}
161+
162+
private static OpenApiFormat GetOpenApiFormat(string input)
163+
{
164+
if (!input.StartsWith("http") && Path.GetExtension(input) == ".json")
165+
{
166+
return OpenApiFormat.Json;
167+
}
168+
else
169+
{
170+
return OpenApiFormat.Yaml;
171+
}
172+
}
159173
}
160174
}

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static async Task<int> Main(string[] args)
3232
new Option("--filterByOperationIds", "Filters OpenApiDocument by OperationId(s) provided", typeof(string)),
3333
new Option("--filterByTags", "Filters OpenApiDocument by Tag(s) provided", typeof(string))
3434
};
35-
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion, OpenApiFormat, string, string, bool, bool>(
35+
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion, OpenApiFormat?, string, string, bool, bool>(
3636
OpenApiService.ProcessOpenApiDocument);
3737

3838
rootCommand.Add(transformCommand);

0 commit comments

Comments
 (0)