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
19 changes: 10 additions & 9 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Validations;
using Microsoft.OpenApi.Writers;
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionHelper;
using System.Threading;

namespace Microsoft.OpenApi.Hidi
Expand All @@ -32,7 +33,7 @@ public static async Task<int> ProcessOpenApiDocument(
string openapi,
string csdl,
FileInfo output,
OpenApiSpecVersion? version,
string? version,
OpenApiFormat? format,
LogLevel loglevel,
bool inline,
Expand Down Expand Up @@ -63,14 +64,15 @@ CancellationToken cancellationToken
Stream stream;
OpenApiDocument document;
OpenApiFormat openApiFormat;
OpenApiSpecVersion openApiVersion;
var stopwatch = new Stopwatch();

if (!string.IsNullOrEmpty(csdl))
{
// Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion
openApiFormat = format ?? GetOpenApiFormat(csdl, logger);
version ??= OpenApiSpecVersion.OpenApi3_0;

openApiVersion = version == null ? OpenApiSpecVersion.OpenApi3_0 : TryParseOpenApiSpecVersion(version);
stream = await GetStream(csdl, logger, cancellationToken);
document = await ConvertCsdlToOpenApi(stream);
}
Expand All @@ -81,12 +83,12 @@ CancellationToken cancellationToken
// Parsing OpenAPI file
stopwatch.Start();
logger.LogTrace("Parsing OpenApi file");
var result = new OpenApiStreamReader(new OpenApiReaderSettings
var result = await new OpenApiStreamReader(new OpenApiReaderSettings
{
ReferenceResolution = resolveexternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
}
).ReadAsync(stream).GetAwaiter().GetResult();
).ReadAsync(stream);

document = result.OpenApiDocument;
stopwatch.Stop();
Expand All @@ -111,7 +113,7 @@ CancellationToken cancellationToken
}

openApiFormat = format ?? GetOpenApiFormat(openapi, logger);
version ??= result.OpenApiDiagnostic.SpecificationVersion;
openApiVersion = version == null ? TryParseOpenApiSpecVersion(version) : result.OpenApiDiagnostic.SpecificationVersion;
}

Func<string, OperationType?, OpenApiOperation, bool> predicate;
Expand Down Expand Up @@ -168,11 +170,10 @@ CancellationToken cancellationToken
logger.LogTrace("Serializing to OpenApi document using the provided spec version and writer");

stopwatch.Start();
document.Serialize(writer, (OpenApiSpecVersion)version);
document.Serialize(writer, openApiVersion);
stopwatch.Stop();

logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms");

textWriter.Flush();

return 0;
Expand All @@ -187,7 +188,7 @@ CancellationToken cancellationToken
return 1;
}
}

/// <summary>
/// Converts CSDL to OpenAPI
/// </summary>
Expand Down
31 changes: 31 additions & 0 deletions src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.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 System;
using System.Linq;

namespace Microsoft.OpenApi.Hidi
{
public static class OpenApiSpecVersionHelper
{
public static OpenApiSpecVersion TryParseOpenApiSpecVersion(string value)
{
if (string.IsNullOrEmpty(value))
{
throw new InvalidOperationException("Please provide a version");
}
var res = value.Split('.', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();

if (int.TryParse(res, out int result))
{

if (result >= 2 && result < 3)
{
return OpenApiSpecVersion.OpenApi2_0;
}
}

return OpenApiSpecVersion.OpenApi3_0; // default
}
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static async Task<int> Main(string[] args)
var outputOption = new Option<FileInfo>("--output", () => new FileInfo("./output"), "The output directory path for the generated file.") { Arity = ArgumentArity.ZeroOrOne };
outputOption.AddAlias("-o");

var versionOption = new Option<OpenApiSpecVersion?>("--version", "OpenAPI specification version");
var versionOption = new Option<string?>("--version", "OpenAPI specification version");
versionOption.AddAlias("-v");

var formatOption = new Option<OpenApiFormat?>("--format", "File format");
Expand Down Expand Up @@ -73,7 +73,7 @@ static async Task<int> Main(string[] args)
resolveExternalOption,
};

transformCommand.SetHandler<string, string, FileInfo, OpenApiSpecVersion?, OpenApiFormat?, LogLevel, bool, bool, string, string, string, CancellationToken> (
transformCommand.SetHandler<string, string, FileInfo, string?, OpenApiFormat?, LogLevel, bool, bool, string, string, string, CancellationToken> (
OpenApiService.ProcessOpenApiDocument, descriptionOption, csdlOption, outputOption, versionOption, formatOption, logLevelOption, inlineOption, resolveExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);

rootCommand.Add(transformCommand);
Expand Down