Skip to content

Commit 177b5ea

Browse files
authored
Merge pull request #658 from microsoft/feature/dm/use-input-file-format-as-default-ouput-format-in-hidi
Use input file OpenApi format / version as the default output format / version in Hidi
2 parents 77ce0bc + ee1c4c6 commit 177b5ea

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 12 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,10 @@ internal static void ValidateOpenApiDocument(string input)
198201

199202
Console.WriteLine(statsVisitor.GetStatisticsReport());
200203
}
204+
205+
private static OpenApiFormat GetOpenApiFormat(string input)
206+
{
207+
return !input.StartsWith("http") && Path.GetExtension(input) == ".json" ? OpenApiFormat.Json : OpenApiFormat.Yaml;
208+
}
201209
}
202210
}

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);

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,6 @@ namespace Microsoft.OpenApi.Services
959959
public static Microsoft.OpenApi.Models.OpenApiDocument CreateFilteredDocument(Microsoft.OpenApi.Models.OpenApiDocument source, System.Func<string, Microsoft.OpenApi.Models.OperationType?, Microsoft.OpenApi.Models.OpenApiOperation, bool> predicate) { }
960960
public static Microsoft.OpenApi.Services.OpenApiUrlTreeNode CreateOpenApiUrlTreeNode(System.Collections.Generic.Dictionary<string, Microsoft.OpenApi.Models.OpenApiDocument> sources) { }
961961
public static System.Func<string, Microsoft.OpenApi.Models.OperationType?, Microsoft.OpenApi.Models.OpenApiOperation, bool> CreatePredicate(string operationIds = null, string tags = null, System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>> requestUrls = null, Microsoft.OpenApi.Models.OpenApiDocument source = null) { }
962-
public static System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>> ParseJsonCollectionFile(System.IO.Stream stream) { }
963962
}
964963
public class OpenApiReferenceError : Microsoft.OpenApi.Models.OpenApiError
965964
{

0 commit comments

Comments
 (0)