Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static class OpenApiService
public static void ProcessOpenApiDocument(
string input,
FileInfo output,
OpenApiSpecVersion version,
OpenApiFormat format,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use OpenApiFormat? (nullable OpenApiFormat) so that you can distinguish between being sent a parameter or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@darrelmiller that's a good suggestion. I adopted it. Thank you.

OpenApiSpecVersion? version,
OpenApiFormat? format,
string filterByOperationIds,
string filterByTags,
string filterByCollection,
Expand Down Expand Up @@ -101,13 +101,16 @@ public static void ProcessOpenApiDocument(
{
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
};
IOpenApiWriter writer = format switch

var openApiFormat = format ?? GetOpenApiFormat(input);
var openApiVersion = version ?? result.OpenApiDiagnostic.SpecificationVersion;
IOpenApiWriter writer = openApiFormat switch
{
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
_ => throw new ArgumentException("Unknown format"),
};
document.Serialize(writer, version);
document.Serialize(writer, openApiVersion);

textWriter.Flush();
}
Expand Down Expand Up @@ -198,5 +201,10 @@ internal static void ValidateOpenApiDocument(string input)

Console.WriteLine(statsVisitor.GetStatisticsReport());
}

private static OpenApiFormat GetOpenApiFormat(string input)
{
return !input.StartsWith("http") && Path.GetExtension(input) == ".json" ? OpenApiFormat.Json : OpenApiFormat.Yaml;
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static async Task<int> Main(string[] args)
new Option("--filterByTags", "Filters OpenApiDocument by Tag(s) provided", typeof(string)),
new Option("--filterByCollection", "Filters OpenApiDocument by Postman collection provided", typeof(string))
};
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion, OpenApiFormat, string, string, string, bool, bool>(
transformCommand.Handler = CommandHandler.Create<string, FileInfo, OpenApiSpecVersion?, OpenApiFormat?, string, string, string, bool, bool>(
OpenApiService.ProcessOpenApiDocument);

rootCommand.Add(transformCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,6 @@ namespace Microsoft.OpenApi.Services
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) { }
public static Microsoft.OpenApi.Services.OpenApiUrlTreeNode CreateOpenApiUrlTreeNode(System.Collections.Generic.Dictionary<string, Microsoft.OpenApi.Models.OpenApiDocument> sources) { }
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) { }
public static System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<string>> ParseJsonCollectionFile(System.IO.Stream stream) { }
}
public class OpenApiReferenceError : Microsoft.OpenApi.Models.OpenApiError
{
Expand Down