Skip to content

Commit a2ee29d

Browse files
committed
Separate concerns in OpenApiFilterService class.
1 parent 6cdb8f9 commit a2ee29d

File tree

11 files changed

+222
-180
lines changed

11 files changed

+222
-180
lines changed

src/Microsoft.OpenApi.Hidi/Formatters/PowerShellFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static PowerShellFormatter()
4343

4444
public override void Visit(OpenApiSchema schema)
4545
{
46-
AddAddtionalPropertiesToSchema(schema);
46+
AddAdditionalPropertiesToSchema(schema);
4747
ResolveAnyOfSchema(schema);
4848
ResolveOneOfSchema(schema);
4949

@@ -175,7 +175,7 @@ private static IList<OpenApiParameter> ResolveFunctionParameters(IList<OpenApiPa
175175
return parameters;
176176
}
177177

178-
private void AddAddtionalPropertiesToSchema(OpenApiSchema schema)
178+
private void AddAdditionalPropertiesToSchema(OpenApiSchema schema)
179179
{
180180
if (schema != null && !_schemaLoop.Contains(schema) && "object".Equals(schema.Type, StringComparison.OrdinalIgnoreCase))
181181
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -27,7 +27,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
2727
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
2828

2929
using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
30-
var logger = loggerFactory.CreateLogger("Hidi.PluginCommandHandler");
30+
var logger = loggerFactory.CreateLogger<PluginCommandHandler>();
3131
try
3232
{
3333
await OpenApiService.PluginManifest(hidiOptions, logger, cancellationToken);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
2727
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
2828

2929
using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
30-
var logger = loggerFactory.CreateLogger<OpenApiService>();
30+
var logger = loggerFactory.CreateLogger<ShowCommandHandler>();
3131
try
3232
{
3333
await OpenApiService.ShowOpenApiDocument(hidiOptions, logger, cancellationToken);
@@ -37,7 +37,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
3737
catch (Exception ex)
3838
{
3939
#if DEBUG
40-
logger.LogCritical(ex, "Command failed");
40+
logger.LogCritical(ex, "Command failed");
4141
throw; // so debug tools go straight to the source of the exception when attached
4242
#else
4343
logger.LogCritical( ex.Message);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
2727
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
2828

2929
using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
30-
var logger = loggerFactory.CreateLogger<OpenApiService>();
30+
var logger = loggerFactory.CreateLogger<TransformCommandHandler>();
3131
try
3232
{
3333
await OpenApiService.TransformOpenApiDocument(hidiOptions, logger, cancellationToken);
@@ -37,7 +37,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
3737
catch (Exception ex)
3838
{
3939
#if DEBUG
40-
logger.LogCritical(ex, "Command failed");
40+
logger.LogCritical(ex, "Command failed");
4141
throw; // so debug tools go straight to the source of the exception when attached
4242
#else
4343
logger.LogCritical( ex.Message);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task<int> InvokeAsync(InvocationContext context)
2828
HidiOptions hidiOptions = new HidiOptions(context.ParseResult, CommandOptions);
2929
CancellationToken cancellationToken = (CancellationToken)context.BindingContext.GetService(typeof(CancellationToken));
3030
using var loggerFactory = Logger.ConfigureLogger(hidiOptions.LogLevel);
31-
var logger = loggerFactory.CreateLogger<OpenApiService>();
31+
var logger = loggerFactory.CreateLogger<ValidateCommandHandler>();
3232
try
3333
{
3434
await OpenApiService.ValidateOpenApiDocument(hidiOptions.OpenApi, logger, cancellationToken);

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.IO;
8+
using System.Linq;
89
using System.Net;
910
using System.Net.Http;
11+
using System.Reflection;
1012
using System.Security;
1113
using System.Text;
12-
using System.Threading.Tasks;
1314
using System.Text.Json;
14-
using Microsoft.Extensions.Logging;
15+
using System.Threading;
16+
using System.Threading.Tasks;
17+
using System.Xml;
1518
using System.Xml.Linq;
19+
using System.Xml.Xsl;
20+
using Microsoft.Extensions.Configuration;
21+
using Microsoft.Extensions.Logging;
1622
using Microsoft.OData.Edm.Csdl;
23+
using Microsoft.OpenApi.ApiManifest;
24+
using Microsoft.OpenApi.ApiManifest.OpenAI;
1725
using Microsoft.OpenApi.Extensions;
26+
using Microsoft.OpenApi.Hidi.Formatters;
27+
using Microsoft.OpenApi.Hidi.Options;
28+
using Microsoft.OpenApi.Hidi.Utilities;
1829
using Microsoft.OpenApi.Models;
1930
using Microsoft.OpenApi.OData;
2031
using Microsoft.OpenApi.Readers;
2132
using Microsoft.OpenApi.Services;
2233
using Microsoft.OpenApi.Writers;
2334
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionHelper;
24-
using System.Threading;
25-
using System.Xml.Xsl;
26-
using System.Xml;
27-
using System.Reflection;
28-
using Microsoft.OpenApi.Hidi.Options;
29-
using Microsoft.Extensions.Configuration;
30-
using Microsoft.OpenApi.Hidi.Utilities;
31-
using Microsoft.OpenApi.Hidi.Formatters;
32-
using System.Linq;
33-
using Microsoft.OpenApi.ApiManifest.OpenAI;
34-
using Microsoft.OpenApi.ApiManifest;
3535

3636
namespace Microsoft.OpenApi.Hidi
3737
{
38-
internal class OpenApiService
38+
internal static class OpenApiService
3939
{
4040
/// <summary>
4141
/// Implementation of the transform command
@@ -70,7 +70,8 @@ public static async Task TransformOpenApiDocument(HidiOptions options, ILogger l
7070

7171
// If ApiManifest is provided, set the referenced OpenAPI document
7272
var apiDependency = await FindApiDependency(options.FilterOptions?.FilterByApiManifest, logger, cancellationToken);
73-
if (apiDependency != null) {
73+
if (apiDependency != null)
74+
{
7475
options.OpenApi = apiDependency.ApiDescripionUrl;
7576
}
7677

@@ -207,7 +208,7 @@ private static void WriteOpenApi(HidiOptions options, OpenApiFormat openApiForma
207208
// Get OpenAPI document either from OpenAPI or CSDL
208209
private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogger logger, CancellationToken cancellationToken, string metadataVersion = null)
209210
{
210-
211+
211212
OpenApiDocument document;
212213
Stream stream;
213214

@@ -257,7 +258,7 @@ private static async Task<OpenApiDocument> GetOpenApi(HidiOptions options, ILogg
257258
if (!string.IsNullOrEmpty(filterbyoperationids))
258259
{
259260
logger.LogTrace("Creating predicate based on the operationIds supplied.");
260-
predicate = OpenApiFilterService.CreatePredicate(operationIds: filterbyoperationids);
261+
predicate = OpenApiFilterService.CreatePredicate(tags: filterbyoperationids);
261262

262263
}
263264
if (!string.IsNullOrEmpty(filterbytags))
@@ -280,10 +281,10 @@ private static Dictionary<string, List<string>> GetRequestUrlsFromManifest(ApiDe
280281
{
281282
// Get the request URLs from the API Dependencies in the API manifest
282283
var requests = apiDependency
283-
.Requests.Where(r => !r.Exclude)
284-
.Select(r=> new { UriTemplate= r.UriTemplate, Method=r.Method } )
285-
.GroupBy(r => r.UriTemplate)
286-
.ToDictionary(g => g.Key, g => g.Select(r => r.Method).ToList());
284+
.Requests.Where(static r => !r.Exclude)
285+
.Select(static r => new { UriTemplate = r.UriTemplate, Method = r.Method })
286+
.GroupBy(static r => r.UriTemplate)
287+
.ToDictionary(static g => g.Key, static g => g.Select(static r => r.Method).ToList());
287288
// This makes the assumption that the UriTemplate in the ApiManifest matches exactly the UriTemplate in the OpenAPI document
288289
// This does not need to be the case. The URI template in the API manifest could map to a set of OpenAPI paths.
289290
// Additional logic will be required to handle this scenario. I sugggest we build this into the OpenAPI.Net library at some point.
@@ -699,51 +700,54 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
699700

700701
internal async static Task PluginManifest(HidiOptions options, ILogger logger, CancellationToken cancellationToken)
701702
{
702-
// If ApiManifest is provided, set the referenced OpenAPI document
703-
var apiDependency = await FindApiDependency(options.FilterOptions?.FilterByApiManifest, logger, cancellationToken);
704-
if (apiDependency != null) {
705-
options.OpenApi = apiDependency.ApiDescripionUrl;
706-
}
703+
// If ApiManifest is provided, set the referenced OpenAPI document
704+
var apiDependency = await FindApiDependency(options.FilterOptions?.FilterByApiManifest, logger, cancellationToken);
705+
if (apiDependency != null)
706+
{
707+
options.OpenApi = apiDependency.ApiDescripionUrl;
708+
}
707709

708-
// Load OpenAPI document
709-
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion);
710+
// Load OpenAPI document
711+
OpenApiDocument document = await GetOpenApi(options, logger, cancellationToken, options.MetadataVersion);
710712

711-
cancellationToken.ThrowIfCancellationRequested();
713+
cancellationToken.ThrowIfCancellationRequested();
712714

713-
if (options.FilterOptions != null)
714-
{
715-
document = ApplyFilters(options, logger, apiDependency, null, document);
716-
}
715+
if (options.FilterOptions != null)
716+
{
717+
document = ApplyFilters(options, logger, apiDependency, null, document);
718+
}
717719

718-
// Ensure path in options.OutputFolder exists
719-
var outputFolder = new DirectoryInfo(options.OutputFolder);
720-
if (!outputFolder.Exists)
720+
// Ensure path in options.OutputFolder exists
721+
var outputFolder = new DirectoryInfo(options.OutputFolder);
722+
if (!outputFolder.Exists)
723+
{
724+
outputFolder.Create();
725+
}
726+
// Write OpenAPI to Output folder
727+
options.Output = new FileInfo(Path.Combine(options.OutputFolder, "openapi.json"));
728+
options.TerseOutput = true;
729+
WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);
730+
731+
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
732+
var manifest = new OpenAIPluginManifest()
733+
{
734+
NameForHuman = document.Info.Title,
735+
DescriptionForHuman = document.Info.Description,
736+
Api = new()
721737
{
722-
outputFolder.Create();
738+
Type = "openapi",
739+
Url = "./openapi.json"
723740
}
724-
// Write OpenAPI to Output folder
725-
options.Output = new FileInfo(Path.Combine(options.OutputFolder,"openapi.json"));
726-
options.TerseOutput =true;
727-
WriteOpenApi(options, OpenApiFormat.Json, OpenApiSpecVersion.OpenApi3_0, document, logger);
728-
729-
// Create OpenAIPluginManifest from ApiDependency and OpenAPI document
730-
var manifest = new OpenAIPluginManifest() {
731-
NameForHuman = document.Info.Title,
732-
DescriptionForHuman = document.Info.Description,
733-
Api = new() {
734-
Type = "openapi",
735-
Url = "./openapi.json"
736-
}
737-
};
738-
manifest.NameForModel = manifest.NameForHuman;
739-
manifest.DescriptionForModel = manifest.DescriptionForHuman;
740-
741-
// Write OpenAIPluginManifest to Output folder
742-
var manifestFile = new FileInfo(Path.Combine(options.OutputFolder,"ai-plugin.json"));
743-
using var file = new FileStream(manifestFile.FullName, FileMode.Create);
744-
using var jsonWriter = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true });
745-
manifest.Write(jsonWriter);
746-
jsonWriter.Flush();
741+
};
742+
manifest.NameForModel = manifest.NameForHuman;
743+
manifest.DescriptionForModel = manifest.DescriptionForHuman;
744+
745+
// Write OpenAIPluginManifest to Output folder
746+
var manifestFile = new FileInfo(Path.Combine(options.OutputFolder, "ai-plugin.json"));
747+
using var file = new FileStream(manifestFile.FullName, FileMode.Create);
748+
using var jsonWriter = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true });
749+
manifest.Write(jsonWriter);
750+
jsonWriter.Flush();
747751
}
748752
}
749753
}

src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.OpenApi/Properties/SRResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
<data name="ArgumentNullOrWhiteSpace" xml:space="preserve">
124124
<value>The argument '{0}' is null, empty or consists only of white-space.</value>
125125
</data>
126+
<data name="DefaultBaseUri" xml:space="preserve">
127+
<value>http://localhost/</value>
128+
</data>
126129
<data name="ExtensionFieldNameMustBeginWithXDash" xml:space="preserve">
127130
<value>The filed name '{0}' of extension doesn't begin with x-.</value>
128131
</data>

0 commit comments

Comments
 (0)