Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: OpenAPI attributes are not getting picked up if they are in a referenced class library project #298 #301

Merged
merged 6 commits into from
Feb 9, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class DocumentHelperExtensions
/// <returns>List of <see cref="MethodInfo"/> instances representing HTTP triggers.</returns>
public static List<MethodInfo> GetHttpTriggerMethods(this IDocumentHelper helper, Assembly assembly, IEnumerable<string> tags = null)
{
var methods = assembly.GetLoadableTypes(includeReferenced: true)
var methods = assembly.GetLoadableTypes()
justinyoo marked this conversation as resolved.
Show resolved Hide resolved
.SelectMany(p => p.GetMethods())
.Where(p => p.ExistsCustomAttribute<FunctionAttribute>())
.Where(p => p.ExistsCustomAttribute<OpenApiOperationAttribute>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions
public static class AssemblyExtensions
{
/// <summary>
/// Loads the <see cref="Assembly"/>'s <see cref="Type"/>s that can be loaded ignoring others.
/// Loads the <see cref="Assembly"/>'s <see cref="Type"/>s that can be loaded ignoring others (includes referenced assemblies).
/// </summary>
/// <param name="assembly"><see cref="Assembly"/> instance.</param>
/// <param name="includeReferenced">Set to true to load <see cref="Type"/>s from referenced assemblies too</param>
/// <returns>Returns the list of <see cref="Type"/>s that can be loaded.</returns>
public static Type[] GetLoadableTypes(this Assembly assembly, bool includeReferenced = false)
public static Type[] GetLoadableTypes(this Assembly assembly)
{
try
{
return includeReferenced
? assembly.GetTypes()
.Union(assembly
.GetReferencedAssemblies()
.SelectMany(x => Assembly.Load(x).GetTypes()))
.Distinct()
.ToArray()
: assembly.GetTypes();
return assembly.GetTypes()
.Union(assembly
.GetReferencedAssemblies()
.Where(x => !x.FullName.StartsWith("Microsoft.Azure.WebJobs.Extensions.OpenApi"))
justinyoo marked this conversation as resolved.
Show resolved Hide resolved
.SelectMany(x => Assembly.Load(x).GetTypes()))
.Distinct()
.ToArray();
}
catch (ReflectionTypeLoadException exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static VisitorCollection CreateInstance()
var collection = new VisitorCollection();
collection.Visitors = typeof(IVisitor).Assembly
.GetLoadableTypes()
.Where(p => p.Name.EndsWith("Visitor") && p.IsClass && !p.IsAbstract)
.Where(p => p.GetInterface(nameof(IVisitor)) != null && p.IsClass && !p.IsAbstract)
justinyoo marked this conversation as resolved.
Show resolved Hide resolved
.Select(p => (IVisitor)Activator.CreateInstance(p, collection)).ToList(); // NOTE: there is no direct enforcement on the constructor arguments of the visitors
return collection;
}
Expand Down