Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'vnext' into fix/simplify-openapi-spec-version
  • Loading branch information
darrelmiller authored Mar 9, 2022
commit 27179a94c27f1c61a678791b19182f265446c8ee
145 changes: 70 additions & 75 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Microsoft.OpenApi.Validations;
using Microsoft.OpenApi.Writers;
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionHelper;
using System.Threading;

namespace Microsoft.OpenApi.Hidi
{
Expand Down Expand Up @@ -60,29 +61,12 @@ CancellationToken cancellationToken
throw new IOException($"The file {output} already exists. Please input a new file path.");
}

Stream stream;
OpenApiDocument document;
OpenApiFormat openApiFormat;
OpenApiSpecVersion? openApiVersion = null;
var stopwatch = new Stopwatch();

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

stream = await GetStream(csdl, logger);
document = await ConvertCsdlToOpenApi(stream);
}
else
{
stream = await GetStream(openapi, logger);
Stream stream;
OpenApiDocument document;
OpenApiFormat openApiFormat;
var stopwatch = new Stopwatch();

// Parsing OpenAPI file
stopwatch.Start();
logger.LogTrace("Parsing OpenApi file");
var result = new OpenApiStreamReader(new OpenApiReaderSettings
if (!string.IsNullOrEmpty(csdl))
{
// Default to yaml and OpenApiVersion 3 during csdl to OpenApi conversion
openApiFormat = format ?? GetOpenApiFormat(csdl, logger);
Expand All @@ -98,12 +82,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 Down Expand Up @@ -131,66 +115,77 @@ CancellationToken cancellationToken
version ??= result.OpenApiDiagnostic.SpecificationVersion;
}

openApiFormat = format ?? GetOpenApiFormat(openapi, logger);
openApiVersion ??= result.OpenApiDiagnostic.SpecificationVersion;
}
catch (Exception ex)
{
throw new InvalidOperationException("Cannot filter by operationIds and tags at the same time.");
}
if (!string.IsNullOrEmpty(filterbyoperationids))
{
logger.LogTrace("Creating predicate based on the operationIds supplied.");
predicate = OpenApiFilterService.CreatePredicate(operationIds: filterbyoperationids);
Func<string, OperationType?, OpenApiOperation, bool> predicate;

logger.LogTrace("Creating subset OpenApi document.");
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
}
if (!string.IsNullOrEmpty(filterbytags))
{
logger.LogTrace("Creating predicate based on the tags supplied.");
predicate = OpenApiFilterService.CreatePredicate(tags: filterbytags);
// Check if filter options are provided, then slice the OpenAPI document
if (!string.IsNullOrEmpty(filterbyoperationids) && !string.IsNullOrEmpty(filterbytags))
{
throw new InvalidOperationException("Cannot filter by operationIds and tags at the same time.");
}
if (!string.IsNullOrEmpty(filterbyoperationids))
{
logger.LogTrace("Creating predicate based on the operationIds supplied.");
predicate = OpenApiFilterService.CreatePredicate(operationIds: filterbyoperationids);

logger.LogTrace("Creating subset OpenApi document.");
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
}
if (!string.IsNullOrEmpty(filterbycollection))
{
var fileStream = await GetStream(filterbycollection, logger);
var requestUrls = ParseJsonCollectionFile(fileStream, logger);
\ logger.LogTrace("Creating subset OpenApi document.");
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
}
if (!string.IsNullOrEmpty(filterbytags))
{
logger.LogTrace("Creating predicate based on the tags supplied.");
predicate = OpenApiFilterService.CreatePredicate(tags: filterbytags);
\
logger.LogTrace("Creating subset OpenApi document.");
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
}
if (!string.IsNullOrEmpty(filterbycollection))
{
var fileStream = await GetStream(filterbycollection, logger, cancellationToken);
var requestUrls = ParseJsonCollectionFile(fileStream, logger);

logger.LogTrace("Creating predicate based on the paths and Http methods defined in the Postman collection.");
predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source:document);
logger.LogTrace("Creating predicate based on the paths and Http methods defined in the Postman collection.");
predicate = OpenApiFilterService.CreatePredicate(requestUrls: requestUrls, source: document);

logger.LogTrace("Creating subset OpenApi document.");
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
}

logger.LogTrace("Creating a new file");
using var outputStream = output?.Create();
var textWriter = outputStream != null ? new StreamWriter(outputStream) : Console.Out;
logger.LogTrace("Creating subset OpenApi document.");
document = OpenApiFilterService.CreateFilteredDocument(document, predicate);
}

var settings = new OpenApiWriterSettings()
{
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
};
logger.LogTrace("Creating a new file");
using var outputStream = output?.Create();
var textWriter = outputStream != null ? new StreamWriter(outputStream) : Console.Out;

IOpenApiWriter writer = openApiFormat switch
{
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
_ => throw new ArgumentException("Unknown format"),
};
var settings = new OpenApiWriterSettings()
{
ReferenceInline = inline ? ReferenceInlineSetting.InlineLocalReferences : ReferenceInlineSetting.DoNotInlineReferences
};

logger.LogTrace("Serializing to OpenApi document using the provided spec version and writer");

stopwatch.Start();
document.Serialize(writer, (OpenApiSpecVersion)openApiVersion);
stopwatch.Stop();
IOpenApiWriter writer = openApiFormat switch
{
OpenApiFormat.Json => new OpenApiJsonWriter(textWriter, settings),
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
_ => throw new ArgumentException("Unknown format"),
};

logger.LogTrace($"Finished serializing in {stopwatch.ElapsedMilliseconds}ms");
logger.LogTrace("Serializing to OpenApi document using the provided spec version and writer");

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

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;
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static async Task<int> Main(string[] args)
resolveExternalOption,
};

transformCommand.SetHandler<string, string, FileInfo, string?, OpenApiFormat?, LogLevel, bool, bool, string, string, string> (
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
You are viewing a condensed version of this merge commit. You can view the full changes here.