Skip to content

Commit f1c3c31

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

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ static class OpenApiService
2121
public static void ProcessOpenApiDocument(
2222
string input,
2323
FileInfo output,
24-
OpenApiSpecVersion version,
25-
OpenApiFormat format,
24+
OpenApiSpecVersion? version,
25+
OpenApiFormat? format,
2626
string filterByOperationIds,
2727
string filterByTags,
2828
bool inline,
@@ -91,13 +91,16 @@ 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+
var openApiVersion = version ?? result.OpenApiDiagnostic.SpecificationVersion;
97+
IOpenApiWriter writer = openApiFormat switch
9598
{
9699
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
97100
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
98101
_ => throw new ArgumentException("Unknown format"),
99102
};
100-
document.Serialize(writer, version);
103+
document.Serialize(writer, openApiVersion);
101104

102105
textWriter.Flush();
103106
}
@@ -156,5 +159,17 @@ internal static void ValidateOpenApiDocument(string input)
156159

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

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)