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
2 changes: 1 addition & 1 deletion .azure-pipelines/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ stages:
displayName: publish Hidi as executable
inputs:
command: 'publish'
arguments: -c Release --runtime win-x64 /p:PublishSingleFile=true --self-contained --output $(Build.ArtifactStagingDirectory)/Microsoft.OpenApi.Hidi-v$(hidiversion) -p:PublishTrimmed=true
arguments: -c Release --runtime win-x64 /p:PublishSingleFile=true --self-contained --output $(Build.ArtifactStagingDirectory)/Microsoft.OpenApi.Hidi-v$(hidiversion)
projects: 'src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj'
publishWebProjects: False
zipAfterPublish: false
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"activityBar.background": "#03323C",
"titleBar.activeBackground": "#054754",
"titleBar.activeForeground": "#F0FCFE"
}
},
"cSpell.words": [
"csdl",
"Hidi"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ internal static class OpenApiExtensibleExtensions
/// <param name="extensions">A dictionary of <see cref="IOpenApiExtension"/>.</param>
/// <param name="extensionKey">The key corresponding to the <see cref="IOpenApiExtension"/>.</param>
/// <returns>A <see cref="string"/> value matching the provided extensionKey. Return null when extensionKey is not found. </returns>
public static string GetExtension(this IDictionary<string, IOpenApiExtension> extensions, string extensionKey)
internal static string GetExtension(this IDictionary<string, IOpenApiExtension> extensions, string extensionKey)
{
if (extensions.TryGetValue(extensionKey, out var value) && value is OpenApiString castValue)
{
return castValue.Value;
}
return default;
return string.Empty;
}
}
}
9 changes: 6 additions & 3 deletions src/Microsoft.OpenApi.Hidi/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ internal static class StringExtensions
/// <summary>
/// Checks if the specified searchValue is equal to the target string based on the specified <see cref="StringComparison"/>.
/// </summary>
/// <param name="target">The target string to commpare to.</param>
/// <param name="target">The target string to compare to.</param>
/// <param name="searchValue">The search string to seek.</param>
/// <param name="comparison">The <see cref="StringComparison"/> to use. This defaults to <see cref="StringComparison.OrdinalIgnoreCase"/>.</param>
/// <returns>true if the searchValue parameter occurs within this string; otherwise, false.</returns>
public static bool IsEquals(this string target, string searchValue, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
public static bool IsEquals(this string? target, string? searchValue, StringComparison comparison = StringComparison.OrdinalIgnoreCase)
{
if (string.IsNullOrWhiteSpace(target) || string.IsNullOrWhiteSpace(searchValue))
if (string.IsNullOrWhiteSpace(target) && string.IsNullOrWhiteSpace(searchValue))
{
return true;
} else if (string.IsNullOrWhiteSpace(target))
{
return false;
}
Expand Down
10 changes: 4 additions & 6 deletions src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override void Visit(OpenApiOperation operation)
private static string ResolveVerbSegmentInOpertationId(string operationId)
{
var charPos = operationId.LastIndexOf('.', operationId.Length - 1);
if (operationId.Contains('_') || charPos < 0)
if (operationId.Contains('_', StringComparison.OrdinalIgnoreCase) || charPos < 0)
return operationId;
var newOperationId = new StringBuilder(operationId);
newOperationId[charPos] = '_';
Expand All @@ -99,7 +99,7 @@ private static string ResolveVerbSegmentInOpertationId(string operationId)
private static string ResolvePutOperationId(string operationId)
{
return operationId.Contains(DefaultPutPrefix, StringComparison.OrdinalIgnoreCase) ?
operationId.Replace(DefaultPutPrefix, PowerShellPutPrefix) : operationId;
operationId.Replace(DefaultPutPrefix, PowerShellPutPrefix, StringComparison.Ordinal) : operationId;
}

private static string ResolveByRefOperationId(string operationId)
Expand Down Expand Up @@ -191,19 +191,17 @@ private void AddAdditionalPropertiesToSchema(OpenApiSchema schema)

private static void ResolveOneOfSchema(OpenApiSchema schema)
{
if (schema.OneOf?.Any() ?? false)
if (schema.OneOf?.FirstOrDefault() is {} newSchema)
{
var newSchema = schema.OneOf.FirstOrDefault();
schema.OneOf = null;
FlattenSchema(schema, newSchema);
}
}

private static void ResolveAnyOfSchema(OpenApiSchema schema)
{
if (schema.AnyOf?.Any() ?? false)
if (schema.AnyOf?.FirstOrDefault() is {} newSchema)
{
var newSchema = schema.AnyOf.FirstOrDefault();
schema.AnyOf = null;
FlattenSchema(schema, newSchema);
}
Expand Down
13 changes: 11 additions & 2 deletions src/Microsoft.OpenApi.Hidi/Handlers/PluginCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Options;

Expand All @@ -24,26 +25,34 @@ public int Invoke(InvocationContext context)
public async Task<int> InvokeAsync(InvocationContext context)
{
HidiOptions hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));

using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
var logger = loggerFactory.CreateLogger<PluginCommandHandler>();
try
{
await OpenApiService.PluginManifest(hidiOptions, logger, cancellationToken);
await OpenApiService.PluginManifest(hidiOptions, logger, cancellationToken).ConfigureAwait(false);

return 0;
}
#if RELEASE
#pragma warning disable CA1031 // Do not catch general exception types
#endif
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, "Command failed");
throw; // so debug tools go straight to the source of the exception when attached
#else
#pragma warning disable CA2254
logger.LogCritical(ex.Message);
#pragma warning restore CA2254
return 1;
#endif
}
#if RELEASE
#pragma warning restore CA1031 // Do not catch general exception types
#endif
}
}
}
13 changes: 11 additions & 2 deletions src/Microsoft.OpenApi.Hidi/Handlers/ShowCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Options;

Expand All @@ -24,26 +25,34 @@ public int Invoke(InvocationContext context)
public async Task<int> InvokeAsync(InvocationContext context)
{
HidiOptions hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));

using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
var logger = loggerFactory.CreateLogger<ShowCommandHandler>();
try
{
await OpenApiService.ShowOpenApiDocument(hidiOptions, logger, cancellationToken);
await OpenApiService.ShowOpenApiDocument(hidiOptions, logger, cancellationToken).ConfigureAwait(false);

return 0;
}
#if RELEASE
#pragma warning disable CA1031 // Do not catch general exception types
#endif
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, "Command failed");
throw; // so debug tools go straight to the source of the exception when attached
#else
#pragma warning disable CA2254
logger.LogCritical( ex.Message);
#pragma warning restore CA2254
return 1;
#endif
}
#if RELEASE
#pragma warning restore CA1031 // Do not catch general exception types
#endif
}
}
}
13 changes: 11 additions & 2 deletions src/Microsoft.OpenApi.Hidi/Handlers/TransformCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Options;

Expand All @@ -24,26 +25,34 @@ public int Invoke(InvocationContext context)
public async Task<int> InvokeAsync(InvocationContext context)
{
HidiOptions hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));

using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
var logger = loggerFactory.CreateLogger<TransformCommandHandler>();
try
{
await OpenApiService.TransformOpenApiDocument(hidiOptions, logger, cancellationToken);
await OpenApiService.TransformOpenApiDocument(hidiOptions, logger, cancellationToken).ConfigureAwait(false);

return 0;
}
#if RELEASE
#pragma warning disable CA1031 // Do not catch general exception types
#endif
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, "Command failed");
throw; // so debug tools go straight to the source of the exception when attached
#else
#pragma warning disable CA2254
logger.LogCritical( ex.Message);
#pragma warning restore CA2254
return 1;
#endif
}
#if RELEASE
#pragma warning restore CA1031 // Do not catch general exception types
#endif
}
}
}
16 changes: 13 additions & 3 deletions src/Microsoft.OpenApi.Hidi/Handlers/ValidateCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.CommandLine.Invocation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Options;

Expand All @@ -26,24 +27,33 @@ public int Invoke(InvocationContext context)
public async Task<int> InvokeAsync(InvocationContext context)
{
HidiOptions hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetRequiredService(typeof(CancellationToken));
using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
var logger = loggerFactory.CreateLogger<ValidateCommandHandler>();
try
{
await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken);
if (hidiOptions.OpenApi is null) throw new InvalidOperationException("OpenApi file is required");
await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken).ConfigureAwait(false);
return 0;
}
#if RELEASE
#pragma warning disable CA1031 // Do not catch general exception types
#endif
catch (Exception ex)
{
#if DEBUG
logger.LogCritical(ex, "Command failed");
throw; // so debug tools go straight to the source of the exception when attached
#else
logger.LogCritical( ex.Message);
#pragma warning disable CA2254
logger.LogCritical(ex.Message);
#pragma warning restore CA2254
return 1;
#endif
}
#if RELEASE
#pragma warning restore CA1031 // Do not catch general exception types
#endif
}
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.OpenApi.Hidi
{
public class Logger
public static class Logger
{
public static ILoggerFactory ConfigureLogger(LogLevel logLevel)
{
Expand Down
12 changes: 11 additions & 1 deletion src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackAsTool>true</PackAsTool>
<Nullable>enable</Nullable>
<PackageIconUrl>http://go.microsoft.com/fwlink/?LinkID=288890</PackageIconUrl>
<PackageProjectUrl>https://github.com/Microsoft/OpenAPI.NET</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -26,6 +28,10 @@
<SignAssembly>true</SignAssembly>
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<NoWarn>NU5048;CA1848;</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<AnalysisMode>All</AnalysisMode>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -62,4 +68,8 @@
</ItemGroup>
<!-- End Unit test Internals -->

<ItemGroup>
<None Include="../../readme.md" Pack="true" PackagePath="" />
</ItemGroup>

</Project>
Loading