Skip to content

Commit 4e0639f

Browse files
committed
Use input file OpenApi format as the default output format
1 parent 77ce0bc commit 4e0639f

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
@@ -23,8 +23,8 @@ public static class OpenApiService
2323
public static void ProcessOpenApiDocument(
2424
string input,
2525
FileInfo output,
26-
OpenApiSpecVersion version,
27-
OpenApiFormat format,
26+
OpenApiSpecVersion? version,
27+
OpenApiFormat? format,
2828
string filterByOperationIds,
2929
string filterByTags,
3030
string filterByCollection,
@@ -101,13 +101,16 @@ public static void ProcessOpenApiDocument(
101101
{
102102
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
103103
};
104-
IOpenApiWriter writer = format switch
104+
105+
var openApiFormat = format ?? GetOpenApiFormat(input);
106+
var openApiVersion = version ?? result.OpenApiDiagnostic.SpecificationVersion;
107+
IOpenApiWriter writer = openApiFormat switch
105108
{
106109
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
107110
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
108111
_ => throw new ArgumentException("Unknown format"),
109112
};
110-
document.Serialize(writer, version);
113+
document.Serialize(writer, openApiVersion);
111114

112115
textWriter.Flush();
113116
}
@@ -198,5 +201,17 @@ internal static void ValidateOpenApiDocument(string input)
198201

199202
Console.WriteLine(statsVisitor.GetStatisticsReport());
200203
}
204+
205+
private static OpenApiFormat GetOpenApiFormat(string input)
206+
{
207+
if (!input.StartsWith("http") && Path.GetExtension(input) == ".json")
208+
{
209+
return OpenApiFormat.Json;
210+
}
211+
else
212+
{
213+
return OpenApiFormat.Yaml;
214+
}
215+
}
201216
}
202217
}

src/Microsoft.OpenApi.Hidi/Program.cs

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

3939
rootCommand.Add(transformCommand);

0 commit comments

Comments
 (0)