Skip to content

Commit

Permalink
Merge pull request #979 from microsoft/vnext
Browse files Browse the repository at this point in the history
Release v1.4.0-preview2
  • Loading branch information
MaggieKimani1 authored Aug 15, 2022
2 parents 35b5282 + 232b45f commit 03ff377
Show file tree
Hide file tree
Showing 46 changed files with 496 additions and 274 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ jobs:
id: getversion
- name: Push to GitHub Packages - Nightly
if: ${{ github.ref == 'refs/heads/vnext' }}
uses: docker/[email protected].0
uses: docker/[email protected].1
with:
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly
- name: Push to GitHub Packages - Release
if: ${{ github.ref == 'refs/heads/master' }}
uses: docker/[email protected].0
uses: docker/[email protected].1
with:
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.getversion.outputs.version }}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0-preview-20220612-01" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="xunit" Version="2.4.2-pre.12" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Project Objectives

# Installation

- Install core Nuget package `Microsoft.OpenApi`
- Install readers Nuget package `Microsoft.OpenApi.Readers`
- Install core Nuget package [**Microsoft.OpenApi**](https://www.nuget.org/packages/Microsoft.OpenApi)
- Install readers Nuget package [**Microsoft.OpenApi.Readers**](https://www.nuget.org/packages/Microsoft.OpenApi.Readers)

# Processors
The OpenAPI.NET project holds the base object model for representing OpenAPI documents as .NET objects. Some developers have found the need to write processors that convert other data formats into this OpenAPI.NET object model. We'd like to curate that list of processors in this section of the readme.
Expand Down
73 changes: 73 additions & 0 deletions src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Microsoft.OpenApi.Hidi.Handlers
{
internal class TransformCommandHandler : ICommandHandler
{
public Option<string> DescriptionOption { get; set; }
public Option<string> CsdlOption { get; set; }
public Option<string> CsdlFilterOption { get; set; }
public Option<FileInfo> OutputOption { get; set; }
public Option<bool> CleanOutputOption { get; set; }
public Option<string?> VersionOption { get; set; }
public Option<OpenApiFormat?> FormatOption { get; set; }
public Option<bool> TerseOutputOption { get; set; }
public Option<LogLevel> LogLevelOption { get; set; }
public Option<string> FilterByOperationIdsOption { get; set; }
public Option<string> FilterByTagsOption { get; set; }
public Option<string> FilterByCollectionOption { get; set; }
public Option<bool> InlineLocalOption { get; set; }
public Option<bool> InlineExternalOption { get; set; }

public int Invoke(InvocationContext context)
{
return InvokeAsync(context).GetAwaiter().GetResult();
}
public async Task<int> InvokeAsync(InvocationContext context)
{
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
string csdlFilter = context.ParseResult.GetValueForOption(CsdlFilterOption);
string csdl = context.ParseResult.GetValueForOption(CsdlOption);
FileInfo output = context.ParseResult.GetValueForOption(OutputOption);
bool cleanOutput = context.ParseResult.GetValueForOption(CleanOutputOption);
string? version = context.ParseResult.GetValueForOption(VersionOption);
OpenApiFormat? format = context.ParseResult.GetValueForOption(FormatOption);
bool terseOutput = context.ParseResult.GetValueForOption(TerseOutputOption);
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
bool inlineLocal = context.ParseResult.GetValueForOption(InlineLocalOption);
bool inlineExternal = context.ParseResult.GetValueForOption(InlineExternalOption);
string filterbyoperationids = context.ParseResult.GetValueForOption(FilterByOperationIdsOption);
string filterbytags = context.ParseResult.GetValueForOption(FilterByTagsOption);
string filterbycollection = context.ParseResult.GetValueForOption(FilterByCollectionOption);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));

using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();
try
{
await OpenApiService.TransformOpenApiDocument(openapi, csdl, csdlFilter, output, cleanOutput, version, format, terseOutput, logLevel, inlineLocal, inlineExternal, filterbyoperationids, filterbytags, filterbycollection, cancellationToken);

return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
throw; // so debug tools go straight to the source of the exception when attached
#else
logger.LogCritical( ex.Message);
return 1;
#endif
}
}
}
}
48 changes: 48 additions & 0 deletions src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Microsoft.OpenApi.Hidi.Handlers
{
internal class ValidateCommandHandler : ICommandHandler
{
public Option<string> DescriptionOption { get; set; }
public Option<LogLevel> LogLevelOption { get; set; }

public int Invoke(InvocationContext context)
{
return InvokeAsync(context).GetAwaiter().GetResult();
}
public async Task<int> InvokeAsync(InvocationContext context)
{
string openapi = context.ParseResult.GetValueForOption(DescriptionOption);
LogLevel logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));


using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();
try
{
await OpenApiService.ValidateOpenApiDocument(openapi, logLevel, cancellationToken);
return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
throw; // so debug tools go straight to the source of the exception when attached
#else
logger.LogCritical( ex.Message);
return 1;
#endif
}
}
}
}
31 changes: 31 additions & 0 deletions src/Microsoft.OpenApi.Hidi/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using Microsoft.Extensions.Logging;

namespace Microsoft.OpenApi.Hidi
{
public class Logger
{
public static ILoggerFactory ConfigureLogger(LogLevel logLevel)
{
// Configure logger options
#if DEBUG
logLevel = logLevel > LogLevel.Debug ? LogLevel.Debug : logLevel;
#endif

return LoggerFactory.Create((builder) =>
{
builder
.AddSimpleConsole(c =>
{
c.IncludeScopes = true;
})
#if DEBUG
.AddDebug()
#endif
.SetMinimumLevel(logLevel);
});
}
}
}
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 @@ -41,8 +41,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="Microsoft.OData.Edm" Version="7.12.2" />
<PackageReference Include="Microsoft.OpenApi.OData" Version="1.0.11" />
</ItemGroup>

Expand Down
40 changes: 11 additions & 29 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand Down Expand Up @@ -26,7 +26,6 @@
using System.Threading;
using System.Xml.Xsl;
using System.Xml;
using System.Runtime.CompilerServices;
using System.Reflection;

namespace Microsoft.OpenApi.Hidi
Expand All @@ -36,7 +35,7 @@ public class OpenApiService
/// <summary>
/// Implementation of the transform command
/// </summary>
public static async Task<int> TransformOpenApiDocument(
public static async Task TransformOpenApiDocument(
string openapi,
string csdl,
string csdlFilter,
Expand All @@ -45,7 +44,7 @@ public static async Task<int> TransformOpenApiDocument(
string? version,
OpenApiFormat? format,
bool terseOutput,
LogLevel loglevel,
LogLevel logLevel,
bool inlineLocal,
bool inlineExternal,
string filterbyoperationids,
Expand All @@ -54,9 +53,8 @@ public static async Task<int> TransformOpenApiDocument(
CancellationToken cancellationToken
)
{
using var loggerFactory = ConfigureLoggerInstance(loglevel);
using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();

try
{
if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl))
Expand Down Expand Up @@ -213,18 +211,11 @@ CancellationToken cancellationToken
logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms");
textWriter.Flush();
}
return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
#else
logger.LogCritical(ex.Message);

#endif
return 1;
}
throw new InvalidOperationException($"Could not transform the document, reason: {ex.Message}", ex);
}
}

private static XslCompiledTransform GetFilterTransform()
Expand Down Expand Up @@ -253,14 +244,13 @@ private static Stream ApplyFilter(string csdl, string entitySetOrSingleton, XslC
/// <summary>
/// Implementation of the validate command
/// </summary>
public static async Task<int> ValidateOpenApiDocument(
public static async Task ValidateOpenApiDocument(
string openapi,
LogLevel loglevel,
LogLevel logLevel,
CancellationToken cancellationToken)
{
using var loggerFactory = ConfigureLoggerInstance(loglevel);
using var loggerFactory = Logger.ConfigureLogger(logLevel);
var logger = loggerFactory.CreateLogger<OpenApiService>();

try
{
if (string.IsNullOrEmpty(openapi))
Expand Down Expand Up @@ -307,19 +297,11 @@ public static async Task<int> ValidateOpenApiDocument(
logger.LogTrace("Finished walking through the OpenApi document. Generating a statistics report..");
logger.LogInformation(statsVisitor.GetStatisticsReport());
}

return 0;
}
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, ex.Message);
#else
logger.LogCritical(ex.Message);
#endif
return 1;
throw new InvalidOperationException($"Could not validate the document, reason: {ex.Message}", ex);
}

}

/// <summary>
Expand Down Expand Up @@ -596,7 +578,7 @@ private static ILoggerFactory ConfigureLoggerInstance(LogLevel loglevel)
loglevel = loglevel > LogLevel.Debug ? LogLevel.Debug : loglevel;
#endif

return LoggerFactory.Create((builder) => {
return Microsoft.Extensions.Logging.LoggerFactory.Create((builder) => {
builder
.AddSimpleConsole(c => {
c.IncludeScopes = true;
Expand Down
28 changes: 23 additions & 5 deletions src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Handlers;

namespace Microsoft.OpenApi.Hidi
{
Expand Down Expand Up @@ -66,7 +65,11 @@ static async Task Main(string[] args)
logLevelOption
};

validateCommand.SetHandler<string, LogLevel, CancellationToken>(OpenApiService.ValidateOpenApiDocument, descriptionOption, logLevelOption);
validateCommand.Handler = new ValidateCommandHandler
{
DescriptionOption = descriptionOption,
LogLevelOption = logLevelOption
};

var transformCommand = new Command("transform")
{
Expand All @@ -86,8 +89,23 @@ static async Task Main(string[] args)
inlineExternalOption
};

transformCommand.SetHandler<string, string, string, FileInfo, bool, string?, OpenApiFormat?, bool, LogLevel, bool, bool, string, string, string, CancellationToken> (
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, terseOutputOption, logLevelOption, inlineLocalOption, inlineExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
transformCommand.Handler = new TransformCommandHandler
{
DescriptionOption = descriptionOption,
CsdlOption = csdlOption,
CsdlFilterOption = csdlFilterOption,
OutputOption = outputOption,
CleanOutputOption = cleanOutputOption,
VersionOption = versionOption,
FormatOption = formatOption,
TerseOutputOption = terseOutputOption,
LogLevelOption = logLevelOption,
FilterByOperationIdsOption = filterByOperationIdsOption,
FilterByTagsOption = filterByTagsOption,
FilterByCollectionOption = filterByCollectionOption,
InlineLocalOption = inlineLocalOption,
InlineExternalOption = inlineExternalOption
};

rootCommand.Add(transformCommand);
rootCommand.Add(validateCommand);
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.4.0-preview1</Version>
<Version>1.4.0-preview2</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/Attributes/DisplayAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.OpenApi.Attributes
/// Represents the Open Api Data type metadata attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
internal class DisplayAttribute : Attribute
public class DisplayAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="DisplayAttribute"/> class.
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.4.0-preview1</Version>
<Version>1.4.0-preview2</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
Loading

0 comments on commit 03ff377

Please sign in to comment.