From 94cca78bfb549ff88cdb1ac7d9e941f82616e174 Mon Sep 17 00:00:00 2001 From: Justin Yoo Date: Wed, 10 Feb 2021 02:42:38 +0900 Subject: [PATCH] Add OpenApiServer details to configuration options (#43) --- .../OpenApiConfigurationOptions.cs | 3 ++ .../OpenApiConfigurationOptions.cs | 3 ++ .../OpenApiConfigurationOptions.cs | 3 ++ .../OpenApiConfigurationOptions.cs | 3 ++ .../OpenApiConfigurationOptions.cs | 7 +++ .../IOpenApiConfigurationOptions.cs | 7 +++ .../Configurations/OpenApiAppSettingsBase.cs | 3 +- .../Configurations/OpenApiSettings.cs | 5 ++ .../Document.cs | 29 +++++++++-- .../IDocument.cs | 4 +- .../ISwaggerUI.cs | 4 +- ...ver.cs => OpenApiConfigurationResolver.cs} | 14 +++--- .../SwaggerUI.cs | 23 +++++++-- .../IOpenApiHttpTriggerContext.cs | 5 +- .../OpenApiEndpoints/OpenApiHttpTrigger.cs | 12 ++--- .../OpenApiHttpTriggerContext.cs | 5 +- .../DocumentTests.cs | 48 ++++++++++++++++--- ...s => OpenApiConfigurationResolverTests.cs} | 8 ++-- 18 files changed, 149 insertions(+), 37 deletions(-) rename src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/{OpenApiInfoResolver.cs => OpenApiConfigurationResolver.cs} (64%) rename test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Resolvers/{OpenApiInfoResolverTests.cs => OpenApiConfigurationResolverTests.cs} (65%) diff --git a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V1Proxy/Configurations/OpenApiConfigurationOptions.cs b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V1Proxy/Configurations/OpenApiConfigurationOptions.cs index da5a8a8..dc4a787 100644 --- a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V1Proxy/Configurations/OpenApiConfigurationOptions.cs +++ b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V1Proxy/Configurations/OpenApiConfigurationOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.OpenApi.Models; @@ -25,5 +26,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V1Proxy.Configu Url = new Uri("http://opensource.org/licenses/MIT"), } }; + + public List Servers { get; set; } = new List(); } } diff --git a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2IoC/Configurations/OpenApiConfigurationOptions.cs b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2IoC/Configurations/OpenApiConfigurationOptions.cs index 3e92b33..02727ee 100644 --- a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2IoC/Configurations/OpenApiConfigurationOptions.cs +++ b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2IoC/Configurations/OpenApiConfigurationOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.OpenApi.Models; @@ -25,5 +26,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2IoC.Configura Url = new Uri("http://opensource.org/licenses/MIT"), } }; + + public List Servers { get; set; } = new List(); } } diff --git a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2Static/Configurations/OpenApiConfigurationOptions.cs b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2Static/Configurations/OpenApiConfigurationOptions.cs index fbe2033..66ecaf4 100644 --- a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2Static/Configurations/OpenApiConfigurationOptions.cs +++ b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2Static/Configurations/OpenApiConfigurationOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.OpenApi.Models; @@ -25,5 +26,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2Static.Config Url = new Uri("http://opensource.org/licenses/MIT"), } }; + + public List Servers { get; set; } = new List(); } } diff --git a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC/Configurations/OpenApiConfigurationOptions.cs b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC/Configurations/OpenApiConfigurationOptions.cs index 8378f8d..70f0143 100644 --- a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC/Configurations/OpenApiConfigurationOptions.cs +++ b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC/Configurations/OpenApiConfigurationOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.OpenApi.Models; @@ -25,5 +26,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC.Configura Url = new Uri("http://opensource.org/licenses/MIT"), } }; + + public List Servers { get; set; } = new List(); } } diff --git a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3Static/Configurations/OpenApiConfigurationOptions.cs b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3Static/Configurations/OpenApiConfigurationOptions.cs index 5ab4cb3..de9bea4 100644 --- a/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3Static/Configurations/OpenApiConfigurationOptions.cs +++ b/samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3Static/Configurations/OpenApiConfigurationOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.OpenApi.Models; @@ -25,5 +26,11 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3Static.Config Url = new Uri("http://opensource.org/licenses/MIT"), } }; + + public List Servers { get; set; } = new List() + { + new OpenApiServer() { Url = "https://contoso.com/api/" }, + new OpenApiServer() { Url = "https://fabrikam.com/api/" }, + }; } } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/IOpenApiConfigurationOptions.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/IOpenApiConfigurationOptions.cs index 468af5f..94b7e89 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/IOpenApiConfigurationOptions.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/IOpenApiConfigurationOptions.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + using Microsoft.OpenApi.Models; namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations @@ -11,5 +13,10 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations /// Gets or sets the instance. /// OpenApiInfo Info { get; set; } + + /// + /// Gets or sets the list of instances. + /// + List Servers { get; set; } } } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiAppSettingsBase.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiAppSettingsBase.cs index 2b7f23a..d10b891 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiAppSettingsBase.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiAppSettingsBase.cs @@ -20,8 +20,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations { var basePath = this.GetBasePath(); var host = HostJsonResolver.Resolve(this.Config, basePath); + var options = OpenApiConfigurationResolver.Resolve(Assembly.GetExecutingAssembly()); - this.OpenApiInfo = OpenApiInfoResolver.Resolve(Assembly.GetExecutingAssembly()); + this.OpenApiInfo = options.Info; this.SwaggerAuthKey = this.Config.GetValue("OpenApi:ApiKey"); this.HttpSettings = host.GetHttpSettings(); diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiSettings.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiSettings.cs index ac9f328..9480493 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiSettings.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Configurations/OpenApiSettings.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + using Microsoft.OpenApi.Models; namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations @@ -13,5 +15,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations Version = "1.0.0", Title = "Azure Functions Open API Extension", }; + + /// + public List Servers { get; set; } = new List(); } } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Document.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Document.cs index 188ad89..85bbedc 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Document.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Document.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; @@ -5,7 +6,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; -using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Comparers; +using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors; using Microsoft.OpenApi; @@ -33,6 +34,13 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core this._helper = helper.ThrowIfNullOrDefault(); } + /// + public Document(OpenApiDocument openApiDocument) + { + this.OpenApiDocument = openApiDocument; + + } + /// public OpenApiDocument OpenApiDocument { get; private set; } @@ -56,12 +64,27 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core } /// - public IDocument AddServer(HttpRequest req, string routePrefix) + public IDocument AddServer(HttpRequest req, string routePrefix, IOpenApiConfigurationOptions options = null) { var prefix = string.IsNullOrWhiteSpace(routePrefix) ? string.Empty : $"/{routePrefix}"; var baseUrl = $"{req.Scheme}://{req.Host}{prefix}"; - this.OpenApiDocument.Servers.Add(new OpenApiServer { Url = baseUrl }); + var server = new OpenApiServer { Url = baseUrl }; + + if (options.IsNullOrDefault()) + { + this.OpenApiDocument.Servers = new List() { server }; + + return this; + } + + // Filters out the existing base URLs that are the same as the current host URL. + var servers = options.Servers + .Where(p => p.Url.TrimEnd('/') != baseUrl.TrimEnd('/')) + .ToList(); + servers.Insert(0, server); + + this.OpenApiDocument.Servers = servers; return this; } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/IDocument.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/IDocument.cs index 3011a42..671e3cb 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/IDocument.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/IDocument.cs @@ -2,6 +2,7 @@ using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors; using Microsoft.OpenApi; using Microsoft.OpenApi.Models; @@ -38,8 +39,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions /// /// instance. /// Route prefix value. + /// instance. /// instance. - IDocument AddServer(HttpRequest req, string routePrefix); + IDocument AddServer(HttpRequest req, string routePrefix, IOpenApiConfigurationOptions options = null); /// /// Adds the naming strategy. diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/ISwaggerUI.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/ISwaggerUI.cs index 95af43c..24c1a7d 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/ISwaggerUI.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/ISwaggerUI.cs @@ -1,6 +1,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.OpenApi.Models; namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions @@ -22,8 +23,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions /// /// instance. /// Route prefix value. + /// instance. /// instance. - ISwaggerUI AddServer(HttpRequest req, string routePrefix); + ISwaggerUI AddServer(HttpRequest req, string routePrefix, IOpenApiConfigurationOptions options = null); /// /// Builds Open API document. diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/OpenApiInfoResolver.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/OpenApiConfigurationResolver.cs similarity index 64% rename from src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/OpenApiInfoResolver.cs rename to src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/OpenApiConfigurationResolver.cs index 7a58342..cbd2fb5 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/OpenApiInfoResolver.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/OpenApiConfigurationResolver.cs @@ -9,16 +9,16 @@ using Microsoft.OpenApi.Models; namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers { /// - /// This represents the resolver entity for from one of host.json, openapisettings.json and environment variables. + /// This represents the resolver entity for . /// - public static class OpenApiInfoResolver + public static class OpenApiConfigurationResolver { /// - /// Gets the instance from one of host.json, openapisettings.json and environment variables. + /// Gets the instance from the given assembly. /// /// The executing assembly instance. - /// Returns instance resolved. - public static OpenApiInfo Resolve(Assembly assembly) + /// Returns the instance resolved. + public static IOpenApiConfigurationOptions Resolve(Assembly assembly) { var type = assembly.GetTypes() .SingleOrDefault(p => p.GetInterface("IOpenApiConfigurationOptions", ignoreCase: true).IsNullOrDefault() == false); @@ -26,12 +26,12 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers { var settings = new OpenApiSettings(); - return settings.Info; + return settings; } var options = Activator.CreateInstance(type); - return (options as IOpenApiConfigurationOptions).Info; + return options as IOpenApiConfigurationOptions; } } } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs index 211fd84..afd0e26 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs @@ -1,9 +1,11 @@ using System.IO; +using System.Linq; using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; +using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; using Microsoft.OpenApi.Models; @@ -41,11 +43,26 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core } /// - public ISwaggerUI AddServer(HttpRequest req, string routePrefix) + public ISwaggerUI AddServer(HttpRequest req, string routePrefix, IOpenApiConfigurationOptions options = null) { var prefix = string.IsNullOrWhiteSpace(routePrefix) ? string.Empty : $"/{routePrefix}"; var baseUrl = $"{req.Scheme}://{req.Host}{prefix}"; - this._baseUrl = baseUrl; + + if (options.IsNullOrDefault()) + { + this._baseUrl = baseUrl; + + return this; + } + + var server = new OpenApiServer { Url = baseUrl }; + // Filters out the existing base URLs that are the same as the current host URL. + var servers = options.Servers + .Where(p => p.Url.TrimEnd('/') != baseUrl.TrimEnd('/')) + .ToList(); + servers.Insert(0, server); + + this._baseUrl = servers.First().Url; return this; } @@ -97,7 +114,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core private string Render(string endpoint, string authKey = null) { var swaggerUiTitle = $"{this._info.Title} - Swagger UI"; - var swaggerUrl = $"{this._baseUrl}/{endpoint}"; + var swaggerUrl = $"{this._baseUrl.TrimEnd('/')}/{endpoint}"; if (!string.IsNullOrWhiteSpace(authKey)) { swaggerUrl += $"?code={authKey}"; diff --git a/templates/OpenApiEndpoints/IOpenApiHttpTriggerContext.cs b/templates/OpenApiEndpoints/IOpenApiHttpTriggerContext.cs index 7b3ad3f..4849fec 100644 --- a/templates/OpenApiEndpoints/IOpenApiHttpTriggerContext.cs +++ b/templates/OpenApiEndpoints/IOpenApiHttpTriggerContext.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Reflection; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; @@ -18,9 +19,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi public interface IOpenApiHttpTriggerContext { /// - /// Gets the instance. + /// Gets the instance. /// - OpenApiInfo OpenApiInfo { get; } + IOpenApiConfigurationOptions OpenApiConfiguration { get; } /// /// Gets the instance. diff --git a/templates/OpenApiEndpoints/OpenApiHttpTrigger.cs b/templates/OpenApiEndpoints/OpenApiHttpTrigger.cs index eb14307..942c624 100644 --- a/templates/OpenApiEndpoints/OpenApiHttpTrigger.cs +++ b/templates/OpenApiEndpoints/OpenApiHttpTrigger.cs @@ -48,8 +48,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi var result = await context.Document .InitialiseDocument() - .AddMetadata(context.OpenApiInfo) - .AddServer(req, context.HttpSettings.RoutePrefix) + .AddMetadata(context.OpenApiConfiguration.Info) + .AddServer(req, context.HttpSettings.RoutePrefix, context.OpenApiConfiguration) .AddNamingStrategy(context.NamingStrategy) .AddVisitors(context.GetVisitorCollection()) .Build(context.GetExecutingAssembly()) @@ -86,8 +86,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi var result = await context.Document .InitialiseDocument() - .AddMetadata(context.OpenApiInfo) - .AddServer(req, context.HttpSettings.RoutePrefix) + .AddMetadata(context.OpenApiConfiguration.Info) + .AddServer(req, context.HttpSettings.RoutePrefix, context.OpenApiConfiguration) .AddNamingStrategy(context.NamingStrategy) .AddVisitors(context.GetVisitorCollection()) .Build(context.GetExecutingAssembly()) @@ -119,8 +119,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi log.LogInformation($"SwaggerUI page was requested."); var result = await context.SwaggerUI - .AddMetadata(context.OpenApiInfo) - .AddServer(req, context.HttpSettings.RoutePrefix) + .AddMetadata(context.OpenApiConfiguration.Info) + .AddServer(req, context.HttpSettings.RoutePrefix, context.OpenApiConfiguration) .BuildAsync() .RenderAsync("swagger.json", context.GetSwaggerAuthKey()) .ConfigureAwait(false); diff --git a/templates/OpenApiEndpoints/OpenApiHttpTriggerContext.cs b/templates/OpenApiEndpoints/OpenApiHttpTriggerContext.cs index 436b421..43544a4 100644 --- a/templates/OpenApiEndpoints/OpenApiHttpTriggerContext.cs +++ b/templates/OpenApiEndpoints/OpenApiHttpTriggerContext.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Reflection; @@ -35,7 +36,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi { var host = HostJsonResolver.Resolve(); - this.OpenApiInfo = OpenApiInfoResolver.Resolve(this.GetExecutingAssembly()); + this.OpenApiConfiguration = OpenApiConfigurationResolver.Resolve(this.GetExecutingAssembly()); this.HttpSettings = host.GetHttpSettings(); var filter = new RouteConstraintFilter(); @@ -47,7 +48,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi } /// - public virtual OpenApiInfo OpenApiInfo { get; } + public virtual IOpenApiConfigurationOptions OpenApiConfiguration { get; } /// public virtual HttpSettings HttpSettings { get; } diff --git a/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/DocumentTests.cs b/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/DocumentTests.cs index 959a43b..fb5705a 100644 --- a/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/DocumentTests.cs +++ b/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/DocumentTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; @@ -6,6 +7,7 @@ using FluentAssertions; using Microsoft.AspNetCore.Http; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; +using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors; using Microsoft.OpenApi; using Microsoft.OpenApi.Models; @@ -22,9 +24,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests public class DocumentTests { [TestMethod] - public void Given_Null_Constructor_Should_Throw_Exception() + public void Given_Null_When_Instantiated_Then_It_Should_Throw_Exception() { - Action action = () => new Document(null); + Action action = () => new Document((IDocumentHelper)null); action.Should().Throw(); } @@ -70,7 +72,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests } [TestMethod] - public async Task Given_VersionAndFormat_RenderAsync_Should_Return_Result() + public async Task Given_VersionAndFormat_When_RenderAsync_Invoked_Then_It_Should_Return_Result() { var helper = new Mock(); var doc = new Document(helper.Object); @@ -84,7 +86,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests } [TestMethod] - public async Task Given_Metadata_RenderAsync_Should_Return_Result() + public async Task Given_Metadata_When_RenderAsync_Invoked_Then_It_Should_Return_Result() { var helper = new Mock(); @@ -103,13 +105,14 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests } [TestMethod] - public async Task Given_ServerDetails_RenderAsync_Should_Return_Result() + public async Task Given_ServerDetails_When_RenderAsync_Invoked_Then_It_Should_Return_Result() { var helper = new Mock(); var scheme = "https"; var host = "localhost"; var routePrefix = "api"; + var url = $"{scheme}://{host}"; var req = new Mock(); req.SetupGet(p => p.Scheme).Returns(scheme); @@ -129,13 +132,43 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests } [TestMethod] - public async Task Given_ServerDetails_WithNullRoutePrefix_RenderAsync_Should_Return_Result() + public async Task Given_ServerDetails_With_ConfigurationOptions_When_RenderAsync_Invoked_Then_It_Should_Return_Result() + { + var helper = new Mock(); + + var scheme = "https"; + var host = "localhost"; + var routePrefix = "api"; + + var req = new Mock(); + req.SetupGet(p => p.Scheme).Returns(scheme); + req.SetupGet(p => p.Host).Returns(new HostString(host)); + + var options = new Mock(); + options.SetupGet(p => p.Servers).Returns(new List() { new OpenApiServer() { Url = $"https://contoso.com/{routePrefix}" } }); + + var doc = new Document(helper.Object); + + var result = await doc.InitialiseDocument() + .AddServer(req.Object, routePrefix, options.Object) + .RenderAsync(OpenApiSpecVersion.OpenApi2_0, OpenApiFormat.Json); + + dynamic json = JObject.Parse(result); + + ((string)json?.host).Should().BeEquivalentTo(host); + ((string)json?.basePath).Should().BeEquivalentTo($"/{routePrefix}"); + ((string)json?.schemes[0]).Should().BeEquivalentTo(scheme); + } + + [TestMethod] + public async Task Given_ServerDetails_WithNullRoutePrefix_When_RenderAsync_Invoked_Then_It_Should_Return_Result() { var helper = new Mock(); var scheme = "https"; var host = "localhost"; string routePrefix = null; + var url = $"{scheme}://{host}"; var req = new Mock(); req.SetupGet(p => p.Scheme).Returns(scheme); @@ -155,13 +188,14 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests } [TestMethod] - public async Task Given_ServerDetails_WithEmptyRoutePrefix_RenderAsync_Should_Return_Result() + public async Task Given_ServerDetails_WithEmptyRoutePrefix_When_RenderAsync_Invoked_Then_It_Should_Return_Result() { var helper = new Mock(); var scheme = "https"; var host = "localhost"; var routePrefix = string.Empty; + var url = $"{scheme}://{host}"; var req = new Mock(); req.SetupGet(p => p.Scheme).Returns(scheme); diff --git a/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Resolvers/OpenApiInfoResolverTests.cs b/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Resolvers/OpenApiConfigurationResolverTests.cs similarity index 65% rename from test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Resolvers/OpenApiInfoResolverTests.cs rename to test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Resolvers/OpenApiConfigurationResolverTests.cs index bf43fec..7365d07 100644 --- a/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Resolvers/OpenApiInfoResolverTests.cs +++ b/test/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests/Resolvers/OpenApiConfigurationResolverTests.cs @@ -2,21 +2,21 @@ using System.Reflection; using FluentAssertions; +using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers; -using Microsoft.OpenApi.Models; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Resolvers { [TestClass] - public class OpenApiInfoResolverTests + public class OpenApiConfigurationResolverTests { [TestMethod] public void Given_Type_Then_It_Should_Have_Methods() { - typeof(OpenApiInfoResolver) + typeof(OpenApiConfigurationResolver) .Should().HaveMethod("Resolve", new[] { typeof(Assembly) }) - .Which.Should().Return(); + .Which.Should().Return(); } } }