Skip to content

Commit 626d2bd

Browse files
authored
Hotfix: OpenAPI Methods (#100)
1 parent 0a432fb commit 626d2bd

File tree

12 files changed

+144
-57
lines changed

12 files changed

+144
-57
lines changed

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.Models/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.Models.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
<RootNamespace>Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.Models</RootNamespace>
77
</PropertyGroup>
88

9+
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
10+
<!-- <ItemGroup>
11+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi.Core" Version="0.7.0-preview" />
12+
</ItemGroup> -->
13+
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
14+
15+
<!-- Comment this block if you want to use NuGet package from https://nuget.org -->
916
<ItemGroup>
1017
<ProjectReference Include="..\..\src\Microsoft.Azure.WebJobs.Extensions.OpenApi.Core\Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.csproj" />
1118
</ItemGroup>
19+
<!-- Comment this block if you want to use NuGet package from https://nuget.org -->
1220

1321
</Project>

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V1Proxy/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V1Proxy.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1111
<!-- <ItemGroup>
12-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.5.1-preview" />
12+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.7.0-preview" />
1313
</ItemGroup> -->
1414
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1515

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2IoC/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2IoC.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1111
<!-- <ItemGroup>
12-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.5.1-preview" />
12+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.7.0-preview" />
1313
</ItemGroup> -->
1414
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1515

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2Static/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V2Static.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1111
<!-- <ItemGroup>
12-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.5.1-preview" />
12+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.7.0-preview" />
1313
</ItemGroup> -->
1414
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1515

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1111
<!-- <ItemGroup>
12-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.5.1-preview" />
12+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.7.0-preview" />
1313
</ItemGroup> -->
1414
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1515

samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3Static/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3Static.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1111
<!-- <ItemGroup>
12-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.5.1-preview" />
12+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="0.7.0-preview" />
1313
</ItemGroup> -->
1414
<!-- Uncomment this block if you want to use NuGet package from https://nuget.org -->
1515

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Collections.Generic;
2+
3+
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi
4+
{
5+
/// <summary>
6+
/// This represents the dependency manifest entity.
7+
/// </summary>
8+
public class DependencyManifest
9+
{
10+
/// <summary>
11+
/// Gets or sets the runtime target that indicates the current runtime;
12+
/// </summary>
13+
public virtual RuntimeTarget RuntimeTarget { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the collection of runtime packages.
17+
/// </summary>
18+
public virtual Dictionary<string, Dictionary<string, RuntimePackage>> Targets { get; set; }
19+
}
20+
21+
/// <summary>
22+
/// This represents the runtime target entity.
23+
/// </summary>
24+
public class RuntimeTarget
25+
{
26+
/// <summary>
27+
/// Gets or sets the runtime target name.
28+
/// </summary>
29+
public virtual string Name { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the runtime target signature.
33+
/// </summary>
34+
public virtual string Signature { get; set; }
35+
}
36+
37+
/// <summary>
38+
/// This represents the runtime package entity.
39+
/// </summary>
40+
public class RuntimePackage
41+
{
42+
/// <summary>
43+
/// Gets or sets the collection of dependent packages.
44+
/// </summary>
45+
public virtual Dictionary<string, string> Dependencies { get; set; }
46+
47+
/// <summary>
48+
/// Gets or sets the collection of runtime libraries.
49+
/// </summary>
50+
public virtual Dictionary<string, object> Runtime { get; set; }
51+
}
52+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Reflection;
3+
using System.Threading.Tasks;
34

45
using Microsoft.Azure.WebJobs.Extensions.Http;
56
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
@@ -74,7 +75,7 @@ public interface IOpenApiHttpTriggerContext
7475
/// </summary>
7576
/// <param name="functionAppDirectory">Function app directory.</param>
7677
/// <param name="appendBin">Value indicating whether to append the "bin" directory or not.</param>
77-
IOpenApiHttpTriggerContext SetApplicationAssembly(string functionAppDirectory, bool appendBin = true);
78+
Task<IOpenApiHttpTriggerContext> SetApplicationAssemblyAsync(string functionAppDirectory, bool appendBin = true);
7879

7980
/// <summary>
8081
/// Gets the <see cref="VisitorCollection"/> instance.

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

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Threading.Tasks;
67

78
using Microsoft.Azure.WebJobs.Extensions.Http;
89
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core;
@@ -14,6 +15,7 @@
1415
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors;
1516
using Microsoft.OpenApi;
1617

18+
using Newtonsoft.Json;
1719
using Newtonsoft.Json.Serialization;
1820

1921
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi
@@ -122,28 +124,16 @@ public virtual Assembly GetExecutingAssembly()
122124
}
123125

124126
/// <inheritdoc />
125-
public virtual IOpenApiHttpTriggerContext SetApplicationAssembly(string functionAppDirectory, bool appendBin = true)
127+
public virtual async Task<IOpenApiHttpTriggerContext> SetApplicationAssemblyAsync(string functionAppDirectory, bool appendBin = true)
126128
{
127129
if (!this._dllpath.IsNullOrWhiteSpace())
128130
{
129131
return this;
130132
}
131133

132-
var file = Directory.GetFiles(functionAppDirectory, "*.deps.json", SearchOption.TopDirectoryOnly).FirstOrDefault();
133-
if (file.IsNullOrWhiteSpace())
134-
{
135-
throw new InvalidOperationException("Invalid function app directory");
136-
}
137-
138-
var pattern = functionAppDirectory;
139-
var replacement = $"{functionAppDirectory.TrimEnd(Path.DirectorySeparatorChar)}";
140-
if (appendBin)
141-
{
142-
replacement += $"{Path.DirectorySeparatorChar}bin";
143-
}
144-
145-
var dllpath = file.Replace(pattern, replacement)
146-
.Replace("deps.json", "dll");
134+
var runtimepath = this.GetRuntimePath(functionAppDirectory, appendBin);
135+
var runtimename = await this.GetRuntimeFilenameAsync(functionAppDirectory);
136+
var dllpath = $"{runtimepath}{Path.DirectorySeparatorChar}{runtimename}";
147137

148138
this._dllpath = dllpath;
149139

@@ -221,19 +211,54 @@ public virtual string GetSwaggerAuthKey(string key = "OpenApi__ApiKey")
221211
return value ?? string.Empty;
222212
}
223213

224-
/// <inheritdoc />
214+
private string GetRuntimePath(string functionAppDirectory, bool appendBin)
215+
{
216+
var path = functionAppDirectory;
217+
if (appendBin)
218+
{
219+
path += $"{Path.DirectorySeparatorChar}bin";
220+
}
221+
222+
return path;
223+
}
224+
225+
// **NOTE**:
226+
// This method relies on the dependency manifest file to find the function app runtime dll file.
227+
// It can be either <your_runtime>.deps.json or function.deps.json. In most cases, at least the
228+
// function.deps.json should exist, but in case no manifest exists, it will throw the exception.
229+
private async Task<string> GetRuntimeFilenameAsync(string functionAppDirectory)
230+
{
231+
var files = Directory.GetFiles(functionAppDirectory, "*.deps.json", SearchOption.AllDirectories);
232+
var file = files.FirstOrDefault();
233+
if (file.IsNullOrWhiteSpace())
234+
{
235+
throw new InvalidOperationException("Invalid function app directory");
236+
}
237+
238+
var serialised = default(string);
239+
using (var reader = File.OpenText(file))
240+
{
241+
serialised = await reader.ReadToEndAsync();
242+
}
243+
244+
var manifesto = JsonConvert.DeserializeObject<DependencyManifest>(serialised);
245+
var runtimeTarget = manifesto.RuntimeTarget.Name;
246+
var runtimes = manifesto.Targets[runtimeTarget].Values;
247+
var runtime = runtimes.First().Runtime.First().Key;
248+
249+
return runtime;
250+
}
251+
225252
private Assembly GetAssembly(object instance)
226253
{
227254
return this.GetAssembly(instance.GetType());
228255
}
229256

230-
/// <inheritdoc />
231257
private Assembly GetAssembly<T>()
232258
{
233259
return this.GetAssembly(typeof(T));
234260
}
235261

236-
/// <inheritdoc />
237262
private Assembly GetAssembly(Type type)
238263
{
239264
var assembly = type.Assembly;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ private Dictionary<string, HttpBindingMetadata> SetupOpenApiHttpBindings()
5050
{
5151
var renderSwaggerDocument = new HttpBindingMetadata()
5252
{
53-
Methods = { HttpMethods.Get },
53+
Methods = new List<string>() { HttpMethods.Get },
5454
Route = "swagger.{extension}",
5555
AuthLevel = this._settings.AuthLevel?.Document ?? AuthorizationLevel.Anonymous,
5656
};
5757

5858
var renderOpenApiDocument = new HttpBindingMetadata()
5959
{
60-
Methods = { HttpMethods.Get },
60+
Methods = new List<string>() { HttpMethods.Get },
6161
Route = "openapi/{version}.{extension}",
6262
AuthLevel = this._settings.AuthLevel?.Document ?? AuthorizationLevel.Anonymous,
6363
};
6464

6565
var renderOAuth2Redirect = new HttpBindingMetadata()
6666
{
67-
Methods = { HttpMethods.Get },
67+
Methods = new List<string>() { HttpMethods.Get },
6868
Route = "oauth2-redirect.html",
6969
AuthLevel = this._settings.AuthLevel?.UI ?? AuthorizationLevel.Anonymous,
7070
};
@@ -80,7 +80,7 @@ private Dictionary<string, HttpBindingMetadata> SetupOpenApiHttpBindings()
8080
{
8181
var renderSwaggerUI = new HttpBindingMetadata()
8282
{
83-
Methods = { HttpMethods.Get },
83+
Methods = new List<string>() { HttpMethods.Get },
8484
Route = "swagger/ui",
8585
AuthLevel = this._settings.AuthLevel?.UI ?? AuthorizationLevel.Anonymous,
8686
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static async Task<IActionResult> RenderSwaggerDocument(HttpRequest req, s
3434
var content = default(ContentResult);
3535
try
3636
{
37-
result = await context.SetApplicationAssembly(ctx.FunctionAppDirectory)
37+
result = await (await context.SetApplicationAssemblyAsync(ctx.FunctionAppDirectory))
3838
.Document
3939
.InitialiseDocument()
4040
.AddMetadata(context.OpenApiConfigurationOptions.Info)
@@ -91,7 +91,7 @@ public static async Task<IActionResult> RenderOpenApiDocument(HttpRequest req, s
9191
var content = default(ContentResult);
9292
try
9393
{
94-
result = await context.SetApplicationAssembly(ctx.FunctionAppDirectory)
94+
result = await (await context.SetApplicationAssemblyAsync(ctx.FunctionAppDirectory))
9595
.Document
9696
.InitialiseDocument()
9797
.AddMetadata(context.OpenApiConfigurationOptions.Info)
@@ -146,7 +146,7 @@ public static async Task<IActionResult> RenderSwaggerUI(HttpRequest req, Executi
146146
var content = default(ContentResult);
147147
try
148148
{
149-
result = await context.SetApplicationAssembly(ctx.FunctionAppDirectory)
149+
result = await (await context.SetApplicationAssemblyAsync(ctx.FunctionAppDirectory))
150150
.SwaggerUI
151151
.AddMetadata(context.OpenApiConfigurationOptions.Info)
152152
.AddServer(req, context.HttpSettings.RoutePrefix, context.OpenApiConfigurationOptions)
@@ -198,7 +198,7 @@ public static async Task<IActionResult> RenderOAuth2Redirect(HttpRequest req, Ex
198198
var content = default(ContentResult);
199199
try
200200
{
201-
result = await context.SetApplicationAssembly(ctx.FunctionAppDirectory)
201+
result = await (await context.SetApplicationAssemblyAsync(ctx.FunctionAppDirectory))
202202
.SwaggerUI
203203
.AddServer(req, context.HttpSettings.RoutePrefix, context.OpenApiConfigurationOptions)
204204
.BuildOAuth2RedirectAsync(context.PackageAssembly)

0 commit comments

Comments
 (0)