Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a commandline option that takes in a metadata version for OpenAPI conversion #1197

Merged
merged 5 commits into from
Mar 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal class TransformCommandHandler : ICommandHandler
public Option<FileInfo> OutputOption { get; set; }
public Option<bool> CleanOutputOption { get; set; }
public Option<string?> VersionOption { get; set; }
public Option<string?> MetadataVersionOption { get; set; }
public Option<OpenApiFormat?> FormatOption { get; set; }
public Option<bool> TerseOutputOption { get; set; }
public Option<string> SettingsFileOption { get; set; }
Expand All @@ -41,6 +42,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
FileInfo output = context.ParseResult.GetValueForOption(OutputOption);
bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption);
string? version = context.ParseResult.GetValueForOption(VersionOption);
string metadataVersion = context.ParseResult.GetValueForOption(MetadataVersionOption);
OpenApiFormat? format = context.ParseResult.GetValueForOption(FormatOption);
bool terseOutput = context.ParseResult.GetValueForOption(TerseOutputOption);
string settingsFile = context.ParseResult.GetValueForOption(SettingsFileOption);
Expand All @@ -57,7 +59,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
var logger = loggerFactory.CreateLogger<OpenApiService>();
try
{
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, format, terseOutput, settingsFile, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, logger, cancellationToken);
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, metadataVersion, format, terseOutput, settingsFile, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, logger, cancellationToken);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.15.0" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.3.0-preview4" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.3.0" />
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
</ItemGroup>

Expand Down
19 changes: 12 additions & 7 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security;
Expand All @@ -20,7 +19,6 @@
using Microsoft.OpenApi.OData;
using Microsoft.OpenApi.Readers;
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Validations;
using Microsoft.OpenApi.Writers;
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionHelper;
using System.Threading;
Expand All @@ -43,6 +41,7 @@ public static async Task TransformOpenApiDocument(
FileInfo output,
bool cleanoutput,
string? version,
string metadataVersion,
OpenApiFormat? format,
bool terseOutput,
string settingsFile,
Expand Down Expand Up @@ -81,7 +80,7 @@ CancellationToken cancellationToken
OpenApiFormat openApiFormat = format ?? (!string.IsNullOrEmpty(openapi) ? GetOpenApiFormat(openapi, logger) : OpenApiFormat.Yaml);
OpenApiSpecVersion openApiVersion = version != null ? TryParseOpenApiSpecVersion(version) : OpenApiSpecVersion.OpenApi3_0;

OpenApiDocument document = await GetOpenApi(openapi, csdl, csdlFilter, settingsFile, inlineExternal, logger, cancellationToken);
OpenApiDocument document = await GetOpenApi(openapi, csdl, csdlFilter, settingsFile, inlineExternal, logger, cancellationToken, metadataVersion);
document = await FilterOpenApiDocument(filterbyoperationids, filterbytags, filterbycollection, document, logger, cancellationToken);
WriteOpenApi(output, terseOutput, inlineLocal, inlineExternal, openApiFormat, openApiVersion, document, logger);
}
Expand Down Expand Up @@ -132,11 +131,11 @@ private static void WriteOpenApi(FileInfo output, bool terseOutput, bool inlineL
}

// Get OpenAPI document either from OpenAPI or CSDL
private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csdl, string csdlFilter, string settingsFile, bool inlineExternal, ILogger logger, CancellationToken cancellationToken)
private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csdl, string csdlFilter, string settingsFile, bool inlineExternal, ILogger logger, CancellationToken cancellationToken, string metadataVersion = null)
{
OpenApiDocument document;
Stream stream;

if (!string.IsNullOrEmpty(csdl))
{
var stopwatch = new Stopwatch();
Expand All @@ -154,7 +153,7 @@ private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csd
stream = null;
}

document = await ConvertCsdlToOpenApi(filteredStream ?? stream, settingsFile, cancellationToken);
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, metadataVersion, settingsFile, cancellationToken);
stopwatch.Stop();
logger.LogTrace("{timestamp}ms: Generated OpenAPI with {paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
}
Expand Down Expand Up @@ -317,14 +316,20 @@ internal static IConfiguration GetConfiguration(string settingsFile)
/// </summary>
/// <param name="csdl">The CSDL stream.</param>
/// <returns>An OpenAPI document.</returns>
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string settingsFile = null, CancellationToken token = default)
public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, string metadataVersion = null, string settingsFile = null, CancellationToken token = default)
{
using var reader = new StreamReader(csdl);
var csdlText = await reader.ReadToEndAsync(token);
var edmModel = CsdlReader.Parse(XElement.Parse(csdlText).CreateReader());

var config = GetConfiguration(settingsFile);
var settings = new OpenApiConvertSettings();

if (!string.IsNullOrEmpty(metadataVersion))
{
settings.SemVerVersion = metadataVersion;
}

config.GetSection("OpenApiConvertSettings").Bind(settings);

OpenApiDocument document = edmModel.ConvertToOpenApi(settings);
Expand Down
7 changes: 6 additions & 1 deletion src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ internal static RootCommand CreateRootCommand()
var versionOption = new Option<string?>("--version", "OpenAPI specification version");
versionOption.AddAlias("-v");

var metadataVersionOption = new Option<string?>("--metadata-version", "Graph metadata version to use.");
metadataVersionOption.AddAlias("--mv");

var formatOption = new Option<OpenApiFormat?>("--format", "File format");
formatOption.AddAlias("-f");

var terseOutputOption = new Option<bool>("--terse-output", "Produce terse json output");
terseOutputOption.AddAlias("--to");

Expand Down Expand Up @@ -93,6 +96,7 @@ internal static RootCommand CreateRootCommand()
outputOption,
cleanOutputOption,
versionOption,
metadataVersionOption,
formatOption,
terseOutputOption,
settingsFileOption,
Expand All @@ -112,6 +116,7 @@ internal static RootCommand CreateRootCommand()
OutputOption = outputOption,
CleanOutputOption = cleanOutputOption,
VersionOption = versionOption,
MetadataVersionOption = metadataVersionOption,
FormatOption = formatOption,
TerseOutputOption = terseOutputOption,
SettingsFileOption = settingsFileOption,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public async Task ReturnConvertedCSDLFile()
}

[Theory]
[InlineData("Todos.Todo.UpdateTodo",null, 1)]
[InlineData("Todos.Todo.UpdateTodo", null, 1)]
[InlineData("Todos.Todo.ListTodo", null, 1)]
[InlineData(null, "Todos.Todo", 4)]
[InlineData(null, "Todos.Todo", 5)]
public async Task ReturnFilteredOpenApiDocBasedOnOperationIdsAndInputCsdlDocument(string operationIds, string tags, int expectedPathCount)
{
// Arrange
Expand Down Expand Up @@ -190,7 +190,7 @@ public async Task TransformCommandConvertsOpenApi()
{
var fileinfo = new FileInfo("sample.json");
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml",null, null, fileinfo, true, null, null,false,null,false,false,null,null,null,new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml",null, null, fileinfo, true, null, null, null,false,null,false,false,null,null,null,new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());

var output = File.ReadAllText("sample.json");
Assert.NotEmpty(output);
Expand All @@ -201,7 +201,7 @@ public async Task TransformCommandConvertsOpenApi()
public async Task TransformCommandConvertsOpenApiWithDefaultOutputname()
{
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, null, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());

var output = File.ReadAllText("output.yml");
Assert.NotEmpty(output);
Expand All @@ -211,7 +211,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputname()
public async Task TransformCommandConvertsCsdlWithDefaultOutputname()
{
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument(null, "UtilityFiles\\Todo.xml", null, null, true, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
await OpenApiService.TransformOpenApiDocument(null, "UtilityFiles\\Todo.xml", null, null, true, null, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());

var output = File.ReadAllText("output.yml");
Assert.NotEmpty(output);
Expand All @@ -221,7 +221,7 @@ public async Task TransformCommandConvertsCsdlWithDefaultOutputname()
public async Task TransformCommandConvertsOpenApiWithDefaultOutputnameAndSwitchFormat()
{
// create a dummy ILogger instance for testing
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, "3.0", OpenApiFormat.Yaml, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
await OpenApiService.TransformOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, true, "3.0", null, OpenApiFormat.Yaml, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());

var output = File.ReadAllText("output.yml");
Assert.NotEmpty(output);
Expand All @@ -231,7 +231,7 @@ public async Task TransformCommandConvertsOpenApiWithDefaultOutputnameAndSwitchF
public async Task ThrowTransformCommandIfOpenApiAndCsdlAreEmpty()
{
await Assert.ThrowsAsync<ArgumentException>(async () =>
await OpenApiService.TransformOpenApiDocument(null, null, null, null, true, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));
await OpenApiService.TransformOpenApiDocument(null, null, null, null, true, null, null, null, false, null, false, false, null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken()));

}

Expand Down