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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.OpenApi.Models;
Expand All @@ -25,5 +26,7 @@ public class OpenApiConfigurationOptions : IOpenApiConfigurationOptions
Url = new Uri("http://opensource.org/licenses/MIT"),
}
};

public List<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.OpenApi.Models;
Expand All @@ -25,5 +26,7 @@ public class OpenApiConfigurationOptions : IOpenApiConfigurationOptions
Url = new Uri("http://opensource.org/licenses/MIT"),
}
};

public List<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.OpenApi.Models;
Expand All @@ -25,5 +26,7 @@ public class OpenApiConfigurationOptions : IOpenApiConfigurationOptions
Url = new Uri("http://opensource.org/licenses/MIT"),
}
};

public List<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.OpenApi.Models;
Expand All @@ -25,5 +26,7 @@ public class OpenApiConfigurationOptions : IOpenApiConfigurationOptions
Url = new Uri("http://opensource.org/licenses/MIT"),
}
};

public List<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
using Microsoft.OpenApi.Models;
Expand All @@ -25,5 +26,11 @@ public class OpenApiConfigurationOptions : IOpenApiConfigurationOptions
Url = new Uri("http://opensource.org/licenses/MIT"),
}
};

public List<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>()
{
new OpenApiServer() { Url = "https://contoso.com/api/" },
new OpenApiServer() { Url = "https://fabrikam.com/api/" },
};
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

using Microsoft.OpenApi.Models;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations
Expand All @@ -11,5 +13,10 @@ public interface IOpenApiConfigurationOptions
/// Gets or sets the <see cref="OpenApiInfo"/> instance.
/// </summary>
OpenApiInfo Info { get; set; }

/// <summary>
/// Gets or sets the list of <see cref="OpenApiServer"/> instances.
/// </summary>
List<OpenApiServer> Servers { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ protected OpenApiAppSettingsBase()
{
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<string>("OpenApi:ApiKey");

this.HttpSettings = host.GetHttpSettings();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

using Microsoft.OpenApi.Models;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations
Expand All @@ -13,5 +15,8 @@ public sealed class OpenApiSettings : IOpenApiConfigurationOptions
Version = "1.0.0",
Title = "Azure Functions Open API Extension",
};

/// <inheritdoc />
public List<OpenApiServer> Servers { get; set; } = new List<OpenApiServer>();
}
}
29 changes: 26 additions & 3 deletions src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Document.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Collections.Generic;
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.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;
Expand Down Expand Up @@ -33,6 +34,13 @@ public Document(IDocumentHelper helper)
this._helper = helper.ThrowIfNullOrDefault();
}

/// <inheritdoc />
public Document(OpenApiDocument openApiDocument)
{
this.OpenApiDocument = openApiDocument;

}

/// <inheritdoc />
public OpenApiDocument OpenApiDocument { get; private set; }

Expand All @@ -56,12 +64,27 @@ public IDocument AddMetadata(OpenApiInfo info)
}

/// <inheritdoc />
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<OpenApiServer>() { 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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;
Expand Down Expand Up @@ -38,8 +39,9 @@ public interface IDocument
/// </summary>
/// <param name="req"><see cref="HttpRequest"/> instance.</param>
/// <param name="routePrefix">Route prefix value.</param>
/// <param name="options"><see cref="IOpenApiConfigurationOptions"/> instance.</param>
/// <returns><see cref="IDocument"/> instance.</returns>
IDocument AddServer(HttpRequest req, string routePrefix);
IDocument AddServer(HttpRequest req, string routePrefix, IOpenApiConfigurationOptions options = null);

/// <summary>
/// Adds the naming strategy.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,8 +23,9 @@ public interface ISwaggerUI
/// </summary>
/// <param name="req"><see cref="HttpRequest"/> instance.</param>
/// <param name="routePrefix">Route prefix value.</param>
/// <param name="options"><see cref="IOpenApiConfigurationOptions"/> instance.</param>
/// <returns><see cref="IDocument"/> instance.</returns>
ISwaggerUI AddServer(HttpRequest req, string routePrefix);
ISwaggerUI AddServer(HttpRequest req, string routePrefix, IOpenApiConfigurationOptions options = null);

/// <summary>
/// Builds Open API document.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers
{
/// <summary>
/// This represents the resolver entity for <see cref="OpenApiInfo"/> from one of host.json, openapisettings.json and environment variables.
/// This represents the resolver entity for <see cref="OpenApiServer"/>.
/// </summary>
public static class OpenApiInfoResolver
public static class OpenApiConfigurationResolver
{
/// <summary>
/// Gets the <see cref="OpenApiInfo"/> instance from one of host.json, openapisettings.json and environment variables.
/// Gets the <see cref="IOpenApiConfigurationOptions"/> instance from the given assembly.
/// </summary>
/// <param name="assembly">The executing assembly instance.</param>
/// <returns>Returns <see cref="OpenApiInfo"/> instance resolved.</returns>
public static OpenApiInfo Resolve(Assembly assembly)
/// <returns>Returns the <see cref="IOpenApiConfigurationOptions"/> instance resolved.</returns>
public static IOpenApiConfigurationOptions Resolve(Assembly assembly)
{
var type = assembly.GetTypes()
.SingleOrDefault(p => p.GetInterface("IOpenApiConfigurationOptions", ignoreCase: true).IsNullOrDefault() == false);
if (type.IsNullOrDefault())
{
var settings = new OpenApiSettings();

return settings.Info;
return settings;
}

var options = Activator.CreateInstance(type);

return (options as IOpenApiConfigurationOptions).Info;
return options as IOpenApiConfigurationOptions;
}
}
}
23 changes: 20 additions & 3 deletions src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/SwaggerUI.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -41,11 +43,26 @@ public ISwaggerUI AddMetadata(OpenApiInfo info)
}

/// <inheritdoc />
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;
}
Expand Down Expand Up @@ -97,7 +114,7 @@ public async Task<string> RenderAsync(string endpoint, string authKey = null)
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}";
Expand Down
5 changes: 3 additions & 2 deletions templates/OpenApiEndpoints/IOpenApiHttpTriggerContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Reflection;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
Expand All @@ -18,9 +19,9 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi
public interface IOpenApiHttpTriggerContext
{
/// <summary>
/// Gets the <see cref="OpenApiInfo"/> instance.
/// Gets the <see cref="IOpenApiConfigurationOptions"/> instance.
/// </summary>
OpenApiInfo OpenApiInfo { get; }
IOpenApiConfigurationOptions OpenApiConfiguration { get; }

/// <summary>
/// Gets the <see cref="HttpSettings"/> instance.
Expand Down
12 changes: 6 additions & 6 deletions templates/OpenApiEndpoints/OpenApiHttpTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static async Task<IActionResult> RenderSwaggerDocument(

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())
Expand Down Expand Up @@ -86,8 +86,8 @@ public static async Task<IActionResult> RenderOpenApiDocument(

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())
Expand Down Expand Up @@ -119,8 +119,8 @@ public static async Task<IActionResult> RenderSwaggerUI(
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);
Expand Down
5 changes: 3 additions & 2 deletions templates/OpenApiEndpoints/OpenApiHttpTriggerContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

Expand Down Expand Up @@ -35,7 +36,7 @@ public OpenApiHttpTriggerContext()
{
var host = HostJsonResolver.Resolve();

this.OpenApiInfo = OpenApiInfoResolver.Resolve(this.GetExecutingAssembly());
this.OpenApiConfiguration = OpenApiConfigurationResolver.Resolve(this.GetExecutingAssembly());
this.HttpSettings = host.GetHttpSettings();

var filter = new RouteConstraintFilter();
Expand All @@ -47,7 +48,7 @@ public OpenApiHttpTriggerContext()
}

/// <inheritdoc />
public virtual OpenApiInfo OpenApiInfo { get; }
public virtual IOpenApiConfigurationOptions OpenApiConfiguration { get; }

/// <inheritdoc />
public virtual HttpSettings HttpSettings { get; }
Expand Down
Loading