Skip to content

Commit b035471

Browse files
committed
First pass at support dynamic OpenApiConfigurationOptions.
TODO: - Move examples into readme files - Update tests
1 parent f7070c6 commit b035471

File tree

8 files changed

+71
-24
lines changed

8 files changed

+71
-24
lines changed

samples/Microsoft.Azure.Functions.Worker.Extensions.OpenApi.FunctionApp.OutOfProc/Microsoft.Azure.Functions.Worker.Extensions.OpenApi.FunctionApp.OutOfProc.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<!-- Comment this block if you want to use NuGet package from https://nuget.org -->
2424
<ItemGroup>
2525
<ProjectReference Include="..\..\src\Microsoft.Azure.Functions.Worker.Extensions.OpenApi\Microsoft.Azure.Functions.Worker.Extensions.OpenApi.csproj" />
26-
<ProjectReference Include="..\Microsoft.Azure.Functions.Worker.Extensions.OpenApi.FunctionApp.OutOfProc.Ping\Microsoft.Azure.Functions.Worker.Extensions.OpenApi.FunctionApp.OutOfProc.Ping.csproj" />
2726
<ProjectReference Include="..\Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.Models\Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.Models.csproj" />
2827
</ItemGroup>
2928
<!-- Comment this block if you want to use NuGet package from https://nuget.org -->

samples/Microsoft.Azure.Functions.Worker.Extensions.OpenApi.FunctionApp.OutOfProc/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using AutoFixture;
22

33
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Extensions;
4+
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.FunctionApp.OutOfProc.Configurations;
5+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
46
using Microsoft.Extensions.DependencyInjection;
57
using Microsoft.Extensions.Hosting;
68

@@ -13,7 +15,23 @@ public static void Main()
1315
var host = new HostBuilder()
1416
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
1517
.ConfigureOpenApi()
16-
.ConfigureServices(services => services.AddSingleton<Fixture>())
18+
.ConfigureServices(services => {
19+
services.AddSingleton<Fixture>();
20+
21+
// Example: If you want to change the configuration during startup of your function you can use the following code:
22+
23+
//services.AddSingleton<IOpenApiConfigurationOptions>(x =>
24+
//{
25+
// return new OpenApiConfigurationOptions()
26+
// {
27+
// Info = new Microsoft.OpenApi.Models.OpenApiInfo
28+
// {
29+
// Title = "A dynamic title generated at runtime",
30+
// Description = "Dynamic Open API information at runtime"
31+
// }
32+
// };
33+
//});
34+
})
1735
.Build();
1836

1937
host.Run();

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc/Startup.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using AutoFixture;
22

33
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
4+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
5+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
6+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers;
7+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc.Configurations;
48
using Microsoft.Extensions.DependencyInjection;
59

610
[assembly: FunctionsStartup(typeof(Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc.Startup))]
@@ -12,7 +16,20 @@ public class Startup : FunctionsStartup
1216
public override void Configure(IFunctionsHostBuilder builder)
1317
{
1418
var fixture = new Fixture();
15-
builder.Services.AddSingleton(fixture);
19+
builder.Services.AddSingleton(fixture);
20+
21+
// Example: If you want to change the configuration during startup of your function you can use the following code:
22+
23+
//var test = new OpenApiConfigurationOptions()
24+
//{
25+
// Info = new Microsoft.OpenApi.Models.OpenApiInfo
26+
// {
27+
// Title = "A dynamic title generated at runtime",
28+
// Description = "Dynamic Open API information at runtime"
29+
// }
30+
//};
31+
32+
//OpenApiConfigurationResolver.ConfigurationOptions = test;
1633
}
1734
}
1835
}

src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/Extensions/OpenApiHostBuilderExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Functions;
22
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
3+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Hosting;
56

@@ -20,7 +21,8 @@ public static IHostBuilder ConfigureOpenApi(this IHostBuilder hostBuilder)
2021
hostBuilder.ConfigureServices(services =>
2122
{
2223
services.AddSingleton<IOpenApiHttpTriggerContext, OpenApiHttpTriggerContext>();
23-
services.AddSingleton<IOpenApiTriggerFunction, OpenApiTriggerFunction>();
24+
services.AddSingleton<IOpenApiTriggerFunction, OpenApiTriggerFunction>();
25+
services.AddSingleton<IOpenApiConfigurationOptions, DefaultOpenApiConfigurationOptions>();
2426
// services.AddSingleton<DefaultOpenApiHttpTrigger, DefaultOpenApiHttpTrigger>();
2527
});
2628

src/Microsoft.Azure.Functions.Worker.Extensions.OpenApi/OpenApiHttpTriggerContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public class OpenApiHttpTriggerContext : IOpenApiHttpTriggerContext
4040
/// <summary>
4141
/// Initializes a new instance of the <see cref="OpenApiHttpTriggerContext"/> class.
4242
/// </summary>
43-
public OpenApiHttpTriggerContext()
43+
public OpenApiHttpTriggerContext(IOpenApiConfigurationOptions configOptions)
4444
{
45+
this._configOptions = configOptions;
4546
this.PackageAssembly = this.GetAssembly<ISwaggerUI>();
4647

4748
var host = HostJsonResolver.Resolve();

src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Resolvers/OpenApiConfigurationResolver.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
1-
using System;
2-
using System.Linq;
3-
using System.Reflection;
4-
51
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
62
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Configurations;
73
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
84
using Microsoft.OpenApi.Models;
5+
using System;
6+
using System.Linq;
7+
using System.Reflection;
98

109
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Resolvers
1110
{
1211
/// <summary>
1312
/// This represents the resolver entity for <see cref="OpenApiServer"/>.
1413
/// </summary>
1514
public static class OpenApiConfigurationResolver
16-
{
15+
{
16+
/// <summary>
17+
/// Property used to allow for dynamic generation of OpenApiConfigurationOptions
18+
/// </summary>
19+
public static IOpenApiConfigurationOptions ConfigurationOptions { get; set; }
20+
1721
/// <summary>
1822
/// Gets the <see cref="IOpenApiConfigurationOptions"/> instance from the given assembly.
1923
/// </summary>
2024
/// <param name="assembly">The executing assembly instance.</param>
2125
/// <returns>Returns the <see cref="IOpenApiConfigurationOptions"/> instance resolved.</returns>
2226
public static IOpenApiConfigurationOptions Resolve(Assembly assembly)
23-
{
27+
{
28+
// If we have already dynamically configured the <see cref="IOpenApiConfigurationOptions"/>, then return it
29+
if (ConfigurationOptions != null)
30+
{
31+
return ConfigurationOptions;
32+
}
33+
2434
var type = assembly.GetLoadableTypes()
2535
.SingleOrDefault(p => p.GetInterface("IOpenApiConfigurationOptions", ignoreCase: true).IsNullOrDefault() == false
2636
&& p.IsAbstract == false
2737
&& p.GetCustomAttribute<ObsoleteAttribute>(inherit: false).IsNullOrDefault() == true);
28-
if (type.IsNullOrDefault())
38+
if (type.IsNullOrDefault())
2939
{
3040
var settings = new DefaultOpenApiConfigurationOptions();
3141

3242
return settings;
33-
}
43+
}
3444

3545
var options = Activator.CreateInstance(type);
3646

src/Microsoft.Azure.WebJobs.Extensions.OpenApi/OpenApiHttpTriggerContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class OpenApiHttpTriggerContext : IOpenApiHttpTriggerContext
3838
private IOpenApiCustomUIOptions _uiOptions;
3939

4040
/// <summary>
41-
/// Initializes a new instance of the <see cref="OpenApiTriggerFunctionProvider"/> class.
41+
/// Initializes a new instance of the <see cref="OpenApiHttpTriggerContext"/> class.
4242
/// </summary>
4343
public OpenApiHttpTriggerContext()
4444
{

test/Microsoft.Azure.Functions.Worker.Extensions.OpenApi.Tests/OpenApiHttpTriggerContextTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class OpenApiHttpTriggerContextTests
2525
public async Task Given_Type_When_Initiated_Then_It_Should_Return_ApplicationAssemblyWithGivenType(Type type)
2626
{
2727
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
28-
var context = new OpenApiHttpTriggerContext();
28+
var context = new OpenApiHttpTriggerContext(null);
2929

3030
var assembly = (await context.SetApplicationAssemblyAsync(location, false))
3131
.ApplicationAssembly;
@@ -39,7 +39,7 @@ public async Task Given_Type_When_Initiated_Then_It_Should_Return_ApplicationAss
3939
public async Task Given_Type_With_Referenced_Project_When_Initiated_Then_It_Should_Return_ApplicationAssemblyOfRootAssembly()
4040
{
4141
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
42-
var context = new OpenApiHttpTriggerContext();
42+
var context = new OpenApiHttpTriggerContext(null);
4343

4444
var assembly = (await context.SetApplicationAssemblyAsync(location, false))
4545
.ApplicationAssembly;
@@ -55,7 +55,7 @@ public async Task Given_Type_With_Referenced_Project_When_Initiated_Then_It_Shou
5555
public async Task Given_Type_When_Initiated_Then_It_Should_NotReturn_ApplicationAssemblyWithGivenType(Type type)
5656
{
5757
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
58-
var context = new OpenApiHttpTriggerContext();
58+
var context = new OpenApiHttpTriggerContext(null);
5959

6060
var assembly = (await context.SetApplicationAssemblyAsync(location, false))
6161
.ApplicationAssembly;
@@ -69,7 +69,7 @@ public async Task Given_Type_When_Initiated_Then_It_Should_NotReturn_Application
6969
public async Task Given_Type_When_Initiated_Then_It_Should_Return_PackageAssembly()
7070
{
7171
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
72-
var context = new OpenApiHttpTriggerContext();
72+
var context = new OpenApiHttpTriggerContext(null);
7373

7474
var assembly = (await context.SetApplicationAssemblyAsync(location, false))
7575
.PackageAssembly;
@@ -81,7 +81,7 @@ public async Task Given_Type_When_Initiated_Then_It_Should_Return_PackageAssembl
8181
public async Task Given_Type_When_Initiated_Then_It_Should_Return_OpenApiConfigurationOptions()
8282
{
8383
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
84-
var context = new OpenApiHttpTriggerContext();
84+
var context = new OpenApiHttpTriggerContext(null);
8585

8686
var options = (await context.SetApplicationAssemblyAsync(location, false))
8787
.OpenApiConfigurationOptions;
@@ -94,7 +94,7 @@ public async Task Given_Type_When_Initiated_Then_It_Should_Return_OpenApiConfigu
9494
public async Task Given_Type_When_Initiated_Then_It_Should_Return_OpenApiCustomUIOptions()
9595
{
9696
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
97-
var context = new OpenApiHttpTriggerContext();
97+
var context = new OpenApiHttpTriggerContext(null);
9898

9999
var options = (await context.SetApplicationAssemblyAsync(location, false))
100100
.OpenApiCustomUIOptions;
@@ -107,7 +107,7 @@ public async Task Given_Type_When_Initiated_Then_It_Should_Return_OpenApiCustomU
107107
public async Task Given_Type_When_Initiated_Then_It_Should_Return_HttpSettings()
108108
{
109109
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
110-
var context = new OpenApiHttpTriggerContext();
110+
var context = new OpenApiHttpTriggerContext(null);
111111

112112
var settings = (await context.SetApplicationAssemblyAsync(location, false))
113113
.HttpSettings;
@@ -121,7 +121,7 @@ public async Task Given_Authorization_When_AuthorizeAsync_Invoked_Then_It_Should
121121
{
122122
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
123123
var req = new Mock<IHttpRequestDataObject>();
124-
var context = new OpenApiHttpTriggerContext();
124+
var context = new OpenApiHttpTriggerContext(null);
125125

126126
var result = await context.SetApplicationAssemblyAsync(location, false)
127127
.AuthorizeAsync(req.Object);
@@ -137,7 +137,7 @@ public async Task Given_Authorization_When_AuthorizeAsync_Invoked_Then_It_Should
137137
public async Task Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string version, OpenApiSpecVersion expected)
138138
{
139139
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
140-
var context = new OpenApiHttpTriggerContext();
140+
var context = new OpenApiHttpTriggerContext(null);
141141

142142
var result = (await context.SetApplicationAssemblyAsync(location, false))
143143
.GetOpenApiSpecVersion(version);
@@ -151,7 +151,7 @@ public async Task Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_R
151151
public async Task Given_Type_When_GetOpenApiSpecVersion_Invoked_Then_It_Should_Return_Result(string format, OpenApiFormat expected)
152152
{
153153
var location = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
154-
var context = new OpenApiHttpTriggerContext();
154+
var context = new OpenApiHttpTriggerContext(null);
155155

156156
var result = (await context.SetApplicationAssemblyAsync(location, false))
157157
.GetOpenApiFormat(format);

0 commit comments

Comments
 (0)