Skip to content

Commit eb539de

Browse files
committed
Addressed code smell issues
1 parent 0e13150 commit eb539de

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

src/Microsoft.OpenApi.Hidi/Handlers/ShowCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
3030
var logger = loggerFactory.CreateLogger<OpenApiService>();
3131
try
3232
{
33-
await OpenApiService.ShowOpenApiDocument(hidiOptions.OpenApi, hidiOptions.Csdl, hidiOptions.CsdlFilter, hidiOptions.Output, logger, cancellationToken);
33+
await OpenApiService.ShowOpenApiDocument(hidiOptions, logger, cancellationToken);
3434

3535
return 0;
3636
}

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
8585
}
8686

8787
// Load OpenAPI document
88-
OpenApiDocument document = await GetOpenApi(options.OpenApi, options.Csdl, options.CsdlFilter, options.SettingsConfig, options.InlineExternal, logger, cancellationToken, options.MetadataVersion);
88+
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion);
8989

9090
if (options.FilterOptions != null)
9191
{
92-
document = ApplyFilters(options, logger, apiDependency, postmanCollection, document, cancellationToken);
92+
document = ApplyFilters(options, logger, apiDependency, postmanCollection, document);
9393
}
9494

9595
var languageFormat = options.SettingsConfig?.GetSection("LanguageFormat")?.Value;
@@ -100,7 +100,7 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
100100
var walker = new OpenApiWalker(powerShellFormatter);
101101
walker.Walk(document);
102102
}
103-
WriteOpenApi(options.Output, options.TerseOutput, options.InlineLocal, options.InlineExternal, openApiFormat, openApiVersion, document, logger);
103+
WriteOpenApi(options, openApiFormat, openApiVersion, document, logger);
104104
}
105105
catch (TaskCanceledException)
106106
{
@@ -141,7 +141,7 @@ private static async Task<ApiDependency> FindApiDependency(string apiManifestPat
141141
return apiDependency;
142142
}
143143

144-
private static OpenApiDocument ApplyFilters(HidiOptions options, ILogger logger, ApiDependency apiDependency, JsonDocument postmanCollection, OpenApiDocument document, CancellationToken cancellationToken)
144+
private static OpenApiDocument ApplyFilters(HidiOptions options, ILogger logger, ApiDependency apiDependency, JsonDocument postmanCollection, OpenApiDocument document)
145145
{
146146
Dictionary<string, List<string>> requestUrls = null;
147147
if (apiDependency != null)
@@ -172,22 +172,22 @@ private static OpenApiDocument ApplyFilters(HidiOptions options, ILogger logger,
172172
return document;
173173
}
174174

175-
private static void WriteOpenApi(FileInfo output, bool terseOutput, bool inlineLocal, bool inlineExternal, OpenApiFormat openApiFormat, OpenApiSpecVersion openApiVersion, OpenApiDocument document, ILogger logger)
175+
private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiFormat, OpenApiSpecVersion openApiVersion, OpenApiDocument document, ILogger logger)
176176
{
177177
using (logger.BeginScope("Output"))
178178
{
179-
using var outputStream = output.Create();
179+
using var outputStream = options.Output.Create();
180180
var textWriter = new StreamWriter(outputStream);
181181

182182
var settings = new OpenApiWriterSettings()
183183
{
184-
InlineLocalReferences = inlineLocal,
185-
InlineExternalReferences = inlineExternal
184+
InlineLocalReferences = options.InlineLocal,
185+
InlineExternalReferences = options.InlineExternal
186186
};
187187

188188
IOpenApiWriter writer = openApiFormat switch
189189
{
190-
OpenApiFormat.Json => terseOutput ? new OpenApiJsonWriter(textWriter, settings, terseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
190+
OpenApiFormat.Json => options.TerseOutput ? new OpenApiJsonWriter(textWriter, settings, options.TerseOutput) : new OpenApiJsonWriter(textWriter, settings, false),
191191
OpenApiFormat.Yaml => new OpenApiYamlWriter(textWriter, settings),
192192
_ => throw new ArgumentException("Unknown format"),
193193
};
@@ -205,37 +205,38 @@ private static void WriteOpenApi(FileInfo output, bool terseOutput, bool inlineL
205205
}
206206

207207
// Get OpenAPI document either from OpenAPI or CSDL
208-
private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csdl, string csdlFilter, IConfiguration settings, bool inlineExternal, ILogger logger, CancellationToken cancellationToken, string metadataVersion = null)
208+
private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogger logger, CancellationToken cancellationToken, string metadataVersion = null)
209209
{
210+
210211
OpenApiDocument document;
211212
Stream stream;
212213

213-
if (!string.IsNullOrEmpty(csdl))
214+
if (!string.IsNullOrEmpty(options.Csdl))
214215
{
215216
var stopwatch = new Stopwatch();
216-
using (logger.BeginScope($"Convert CSDL: {csdl}", csdl))
217+
using (logger.BeginScope($"Convert CSDL: {options.Csdl}", options.Csdl))
217218
{
218219
stopwatch.Start();
219-
stream = await GetStream(csdl, logger, cancellationToken);
220+
stream = await GetStream(options.Csdl, logger, cancellationToken);
220221
Stream filteredStream = null;
221-
if (!string.IsNullOrEmpty(csdlFilter))
222+
if (!string.IsNullOrEmpty(options.CsdlFilter))
222223
{
223224
XslCompiledTransform transform = GetFilterTransform();
224-
filteredStream = ApplyFilterToCsdl(stream, csdlFilter, transform);
225+
filteredStream = ApplyFilterToCsdl(stream, options.CsdlFilter, transform);
225226
filteredStream.Position = 0;
226227
stream.Dispose();
227228
stream = null;
228229
}
229230

230-
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, metadataVersion, settings, cancellationToken);
231+
document = await ConvertCsdlToOpenApi(filteredStream ?? stream, metadataVersion, options.SettingsConfig, cancellationToken);
231232
stopwatch.Stop();
232233
logger.LogTrace("{timestamp}ms: Generated OpenAPI with {paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
233234
}
234235
}
235236
else
236237
{
237-
stream = await GetStream(openapi, logger, cancellationToken);
238-
var result = await ParseOpenApi(openapi, inlineExternal, logger, stream, cancellationToken);
238+
stream = await GetStream(options.OpenApi, logger, cancellationToken);
239+
var result = await ParseOpenApi(options.OpenApi, options.InlineExternal, logger, stream, cancellationToken);
239240
document = result.OpenApiDocument;
240241
}
241242

@@ -548,21 +549,21 @@ private static string GetInputPathExtension(string openapi = null, string csdl =
548549
return extension;
549550
}
550551

551-
internal static async Task<string> ShowOpenApiDocument(string openapi, string csdl, string csdlFilter, FileInfo output, ILogger logger, CancellationToken cancellationToken)
552+
internal static async Task<string> ShowOpenApiDocument(HidiOptions options, ILogger logger, CancellationToken cancellationToken)
552553
{
553554
try
554555
{
555-
if (string.IsNullOrEmpty(openapi) && string.IsNullOrEmpty(csdl))
556+
if (string.IsNullOrEmpty(options.OpenApi) && string.IsNullOrEmpty(options.Csdl))
556557
{
557558
throw new ArgumentException("Please input a file path or URL");
558559
}
559560

560-
var document = await GetOpenApi(openapi, csdl, csdlFilter, null, false, logger, cancellationToken);
561+
var document = await GetOpenApi(options, logger, cancellationToken);
561562

562563
using (logger.BeginScope("Creating diagram"))
563564
{
564565
// If output is null, create a HTML file in the user's temporary directory
565-
if (output == null)
566+
if (options.Output == null)
566567
{
567568
var tempPath = Path.GetTempPath() + "/hidi/";
568569
if (!File.Exists(tempPath))
@@ -572,11 +573,11 @@ internal static async Task<string> ShowOpenApiDocument(string openapi, string cs
572573

573574
var fileName = Path.GetRandomFileName();
574575

575-
output = new FileInfo(Path.Combine(tempPath, fileName + ".html"));
576+
var output = new FileInfo(Path.Combine(tempPath, fileName + ".html"));
576577
using (var file = new FileStream(output.FullName, FileMode.Create))
577578
{
578579
using var writer = new StreamWriter(file);
579-
WriteTreeDocumentAsHtml(openapi ?? csdl, document, writer);
580+
WriteTreeDocumentAsHtml(options.OpenApi ?? options.Csdl, document, writer);
580581
}
581582
logger.LogTrace("Created Html document with diagram ");
582583

@@ -590,13 +591,13 @@ internal static async Task<string> ShowOpenApiDocument(string openapi, string cs
590591
}
591592
else // Write diagram as Markdown document to output file
592593
{
593-
using (var file = new FileStream(output.FullName, FileMode.Create))
594+
using (var file = new FileStream(options.Output.FullName, FileMode.Create))
594595
{
595596
using var writer = new StreamWriter(file);
596-
WriteTreeDocumentAsMarkdown(openapi ?? csdl, document, writer);
597+
WriteTreeDocumentAsMarkdown(options.OpenApi ?? options.Csdl, document, writer);
597598
}
598599
logger.LogTrace("Created markdown document with diagram ");
599-
return output.FullName;
600+
return options.Output.FullName;
600601
}
601602
}
602603
}
@@ -705,13 +706,13 @@ internal async static Task PluginManifest(HidiOptions options, ILogger logger, C
705706
}
706707

707708
// Load OpenAPI document
708-
OpenApiDocument document = await GetOpenApi(options.OpenApi, options.Csdl, options.CsdlFilter, options.SettingsConfig, options.InlineExternal, logger, cancellationToken, options.MetadataVersion);
709+
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion);
709710

710711
cancellationToken.ThrowIfCancellationRequested();
711712

712713
if (options.FilterOptions != null)
713714
{
714-
document = ApplyFilters(options, logger, apiDependency, null, document, cancellationToken);
715+
document = ApplyFilters(options, logger, apiDependency, null, document);
715716
}
716717

717718
// Ensure path in options.OutputFolder exists
@@ -722,7 +723,8 @@ internal async static Task PluginManifest(HidiOptions options, ILogger logger, C
722723
}
723724
var slicedOpenApi = new FileInfo(Path.Combine(options.OutputFolder,"openapi.json"));
724725
// Write OpenAPI to Output folder
725-
WriteOpenApi(slicedOpenApi, true, options.InlineLocal, options.InlineExternal, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);
726+
options.TerseOutput =true;
727+
WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);
726728

727729
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
728730
var manifest = new OpenAIPluginManifest() {

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,42 @@ public void ShowCommandGeneratesMermaidDiagramAsHtml()
124124
[Fact]
125125
public async Task ShowCommandGeneratesMermaidMarkdownFileWithMermaidDiagram()
126126
{
127-
var fileinfo = new FileInfo("sample.md");
127+
128128
// create a dummy ILogger instance for testing
129-
await OpenApiService.ShowOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, fileinfo, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
129+
var options = new HidiOptions() {
130+
OpenApi = "UtilityFiles\\SampleOpenApi.yml",
131+
Output = new FileInfo("sample.md")
132+
};
133+
134+
await OpenApiService.ShowOpenApiDocument(options, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
130135

131-
var output = File.ReadAllText(fileinfo.FullName);
136+
var output = File.ReadAllText(options.Output.FullName);
132137
Assert.Contains("graph LR", output);
133138
}
134139

135140
[Fact]
136141
public async Task ShowCommandGeneratesMermaidHtmlFileWithMermaidDiagram()
137142
{
138-
var filePath = await OpenApiService.ShowOpenApiDocument("UtilityFiles\\SampleOpenApi.yml", null, null, null, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
143+
var options = new HidiOptions() {
144+
OpenApi = "UtilityFiles\\SampleOpenApi.yml"
145+
};
146+
var filePath = await OpenApiService.ShowOpenApiDocument(options, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
139147
Assert.True(File.Exists(filePath));
140148
}
141149

142150
[Fact]
143151
public async Task ShowCommandGeneratesMermaidMarkdownFileFromCsdlWithMermaidDiagram()
144152
{
145-
var fileinfo = new FileInfo("sample.md");
153+
var options = new HidiOptions() {
154+
Csdl = "UtilityFiles\\Todo.xml",
155+
CsdlFilter = "todos",
156+
Output = new FileInfo("sample.md")
157+
};
158+
146159
// create a dummy ILogger instance for testing
147-
await OpenApiService.ShowOpenApiDocument(null, "UtilityFiles\\Todo.xml", "todos", fileinfo, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
160+
await OpenApiService.ShowOpenApiDocument(options, new Logger<OpenApiService>(new LoggerFactory()), new CancellationToken());
148161

149-
var output = File.ReadAllText(fileinfo.FullName);
162+
var output = File.ReadAllText(options.Output.FullName);
150163
Assert.Contains("graph LR", output);
151164
}
152165

0 commit comments

Comments
 (0)