Skip to content

Commit

Permalink
Merge pull request #1199 from microsoft/vnext
Browse files Browse the repository at this point in the history
Merge latest changes into master
  • Loading branch information
MaggieKimani1 authored Mar 30, 2023
2 parents 9155d11 + d432567 commit 4da5e7a
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 31 deletions.
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
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageId>Microsoft.OpenApi.Hidi</PackageId>
<ToolCommandName>hidi</ToolCommandName>
<PackageOutputPath>./../../artifacts</PackageOutputPath>
<Version>1.2.4</Version>
<Version>1.2.5-preview1</Version>
<Description>OpenAPI.NET CLI tool for slicing OpenAPI documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down 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-preview2" />
<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 @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi.Readers</Title>
<PackageId>Microsoft.OpenApi.Readers</PackageId>
<Version>1.6.3</Version>
<Version>1.6.4-preview1</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi</Title>
<PackageId>Microsoft.OpenApi</PackageId>
<Version>1.6.3</Version>
<Version>1.6.4-preview1</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ public OpenApiOperation() {}
/// </summary>
public OpenApiOperation(OpenApiOperation operation)
{
Tags = new List<OpenApiTag>(operation?.Tags);
Tags = operation?.Tags != null ? new List<OpenApiTag>(operation?.Tags) : null;
Summary = operation?.Summary ?? Summary;
Description = operation?.Description ?? Description;
ExternalDocs = operation?.ExternalDocs != null ? new(operation?.ExternalDocs) : null;
OperationId = operation?.OperationId ?? OperationId;
Parameters = operation?.Parameters != null ? new List<OpenApiParameter>(operation.Parameters) : null;
RequestBody = new(operation?.RequestBody);
RequestBody = operation?.RequestBody != null ? new(operation?.RequestBody) : null;
Responses = operation?.Responses != null ? new(operation?.Responses) : null;
Callbacks = operation?.Callbacks != null ? new Dictionary<string, OpenApiCallback>(operation.Callbacks) : null;
Deprecated = operation?.Deprecated ?? Deprecated;
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
// style
if (_style.HasValue)
{
writer.WriteProperty(OpenApiConstants.Style, Style.Value.GetDisplayName());
writer.WriteProperty(OpenApiConstants.Style, _style.Value.GetDisplayName());
}

// explode
writer.WriteProperty(OpenApiConstants.Explode, Explode, Style.HasValue && Style.Value == ParameterStyle.Form);
writer.WriteProperty(OpenApiConstants.Explode, _explode, _style.HasValue && _style.Value == ParameterStyle.Form);

// allowReserved
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);
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
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="FluentAssertions" Version="6.10.0">
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2">
<PackageReference Include="Newtonsoft.Json" Version="13.0.3">
</PackageReference>
<PackageReference Include="SharpYaml" Version="2.1.0">
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpYaml" Version="2.1.0" />
<PackageReference Include="Verify.Xunit" Version="19.10.0" />
<PackageReference Include="Verify.Xunit" Version="19.11.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading

0 comments on commit 4da5e7a

Please sign in to comment.