Skip to content
Open
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
Expand Up @@ -28,3 +28,4 @@ AWSLambda0142 | AWSLambdaCSharpGenerator | Error | Invalid DurableExecution meth
AWSLambda0143 | AWSLambdaCSharpGenerator | Info | DurableExecution function with explicit Role needs checkpoint permissions
AWSLambda0144 | AWSLambdaCSharpGenerator | Error | Invalid DurableExecutionAttribute
AWSLambda0145 | AWSLambdaCSharpGenerator | Warning | Durable execution envelope types not registered with JsonSerializerContext
AWSLambda0146 | AWSLambdaCSharpGenerator | Error | Invalid BuildServiceProvider method
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,12 @@ public static class DiagnosticDescriptors
category: "AWSLambdaCSharpGenerator",
DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor InvalidBuildServiceProviderMethod = new DiagnosticDescriptor(id: "AWSLambda0146",
title: "Invalid BuildServiceProvider method",
messageFormat: "The BuildServiceProvider method on the Startup class must take a single IServiceCollection parameter and return IServiceProvider or a type implementing it: {0}",
category: "AWSLambdaCSharpGenerator",
DiagnosticSeverity.Error,
isEnabledByDefault: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,26 @@ public void Execute(GeneratorExecutionContext context)
var configureHostBuilderMethodSymbol = semanticModelProvider.GetConfigureHostBuilderMethodModel(receiver.StartupClasses.FirstOrDefault());
var configureServicesMethodSymbol = semanticModelProvider.GetConfigureServicesMethodModel(receiver.StartupClasses.FirstOrDefault());
var configureMethodSymbol = configureServicesMethodSymbol;
var buildServiceProviderMethodSymbol = semanticModelProvider.GetBuildServiceProviderMethodModel(receiver.StartupClasses.FirstOrDefault());

if (configureServicesMethodSymbol != null && configureHostBuilderMethodSymbol != null)
{
diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.MultipleConfigureMethodsNotAllowed, configureServicesMethodSymbol.Locations.FirstOrDefault(), configureServicesMethodSymbol.Name, configureHostBuilderMethodSymbol.Name));
}

if (buildServiceProviderMethodSymbol != null && !semanticModelProvider.IsValidBuildServiceProviderMethod(buildServiceProviderMethodSymbol))
{
diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.InvalidBuildServiceProviderMethod, buildServiceProviderMethodSymbol.Locations.FirstOrDefault(), buildServiceProviderMethodSymbol.Name));
buildServiceProviderMethodSymbol = null;
}

if (configureHostBuilderMethodSymbol != null)
{
configureMethodSymbol = configureHostBuilderMethodSymbol;
// The BuildServiceProvider hook only applies to the ConfigureServices path.
// With a host builder the container is configured through ConfigureHostBuilder
// and the generated code uses host.Services, so the hook is ignored.
buildServiceProviderMethodSymbol = null;
}

// Check for methods that have secondary attributes (HttpApi, RestApi, HttpApiAuthorizer, etc.)
Expand All @@ -171,7 +182,7 @@ public void Execute(GeneratorExecutionContext context)
var lambdaMethodSymbol = semanticModelProvider.GetMethodSemanticModel(lambdaMethodDeclarationSyntax);
var lambdaMethodLocation = lambdaMethodDeclarationSyntax.GetLocation();

var lambdaFunctionModel = LambdaFunctionModelBuilder.BuildAndValidate(lambdaMethodSymbol, lambdaMethodLocation, configureMethodSymbol, context, isExecutable, defaultRuntime, diagnosticReporter);
var lambdaFunctionModel = LambdaFunctionModelBuilder.BuildAndValidate(lambdaMethodSymbol, lambdaMethodLocation, configureMethodSymbol, buildServiceProviderMethodSymbol, context, isExecutable, defaultRuntime, diagnosticReporter);
if (!lambdaFunctionModel.IsValid)
{
// If the model is not valid then skip it from further processing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ namespace Amazon.Lambda.Annotations.SourceGenerator.Models
/// </summary>
public static class LambdaFunctionModelBuilder
{
public static LambdaFunctionModel BuildAndValidate(IMethodSymbol lambdaMethodSymbol, Location LambdamethodLocation, IMethodSymbol configureMethodSymbol, GeneratorExecutionContext context, bool isExecutable, string runtime, DiagnosticReporter diagnosticReporter)
public static LambdaFunctionModel BuildAndValidate(IMethodSymbol lambdaMethodSymbol, Location LambdamethodLocation, IMethodSymbol configureMethodSymbol, IMethodSymbol buildServiceProviderMethodSymbol, GeneratorExecutionContext context, bool isExecutable, string runtime, DiagnosticReporter diagnosticReporter)
{
// We need to check for the necessary dependencies before attempting to build the LambdaFunctionModel otherwise the generator blows up with unknown exceptions.
if (!LambdaFunctionValidator.ValidateDependencies(context, lambdaMethodSymbol, LambdamethodLocation, diagnosticReporter))
{
return new LambdaFunctionModel() { IsValid = false };
}

var lambdaFunctionModel = Build(lambdaMethodSymbol, configureMethodSymbol, context, isExecutable, runtime);
var lambdaFunctionModel = Build(lambdaMethodSymbol, configureMethodSymbol, buildServiceProviderMethodSymbol, context, isExecutable, runtime);
lambdaFunctionModel.IsValid = LambdaFunctionValidator.ValidateFunction(context, lambdaMethodSymbol, LambdamethodLocation, lambdaFunctionModel, diagnosticReporter);
return lambdaFunctionModel;
}

private static LambdaFunctionModel Build(IMethodSymbol lambdaMethodSymbol, IMethodSymbol configureMethodSymbol, GeneratorExecutionContext context, bool isExecutable, string runtime)
private static LambdaFunctionModel Build(IMethodSymbol lambdaMethodSymbol, IMethodSymbol configureMethodSymbol, IMethodSymbol buildServiceProviderMethodSymbol, GeneratorExecutionContext context, bool isExecutable, string runtime)
{
var lambdaMethod = LambdaMethodModelBuilder.Build(lambdaMethodSymbol, configureMethodSymbol, context);
var lambdaMethod = LambdaMethodModelBuilder.Build(lambdaMethodSymbol, configureMethodSymbol, buildServiceProviderMethodSymbol, context);
var generatedMethod = GeneratedMethodModelBuilder.Build(lambdaMethodSymbol, configureMethodSymbol, lambdaMethod, context);
var model = new LambdaFunctionModel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ public bool ReturnsVoidTaskOrSqsBatchResponse

public bool UsingHostBuilderForDependencyInjection { get; set; }

/// <summary>
/// Returns true if the Startup class defines a BuildServiceProvider method that
/// the generated code should use to create the <see cref="System.IServiceProvider"/>
/// instead of the default <see cref="Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(Microsoft.Extensions.DependencyInjection.IServiceCollection)"/>.
/// This allows using an alternate IoC container, for example Autofac.
/// </summary>
public bool UsingCustomServiceProvider { get; set; }

/// <summary>
/// Gets or sets the namespace for the nearest enclosing namespace. Returns null if the
/// symbol isn't contained in a namespace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class LambdaMethodModelBuilder
{
public static LambdaMethodModel Build(IMethodSymbol lambdaMethodSymbol,
IMethodSymbol configureMethodSymbol,
IMethodSymbol buildServiceProviderMethodSymbol,
GeneratorExecutionContext context)
{
var model = new LambdaMethodModel
Expand All @@ -28,6 +29,7 @@ public static LambdaMethodModel Build(IMethodSymbol lambdaMethodSymbol,
ContainingType = TypeModelBuilder.Build(lambdaMethodSymbol.ContainingType, context),
UsingDependencyInjection = configureMethodSymbol != null,
UsingHostBuilderForDependencyInjection = configureMethodSymbol != null && configureMethodSymbol.Name == "ConfigureHostBuilder",
UsingCustomServiceProvider = buildServiceProviderMethodSymbol != null,
Attributes = lambdaMethodSymbol.GetAttributes().Select(att => AttributeModelBuilder.Build(att, context)).ToList()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,75 @@ public IMethodSymbol GetConfigureHostBuilderMethodModel(ClassDeclarationSyntax s
return configureHostBuilderMethodSymbol;
}

/// <summary>
/// Returns the BuildServiceProvider method's semantic model in the LambdaStartup attributed class,
/// if the class defines one. The method is expected to take a single IServiceCollection parameter
/// and return an IServiceProvider, allowing an alternate IoC container to be used.
/// The signature is validated by the caller; this method returns the symbol even when the
/// signature does not match so an invalid definition can be reported.
/// If <see cref="startupSyntax"/> is null, returns null.
/// </summary>
/// <param name="startupSyntax">LambdaStartup attributed class syntax</param>
public IMethodSymbol GetBuildServiceProviderMethodModel(ClassDeclarationSyntax startupSyntax)
{
if (startupSyntax == null)
{
return null;
}

var classModel = _context.Compilation.GetSemanticModel(startupSyntax.SyntaxTree);

foreach (var member in startupSyntax.Members.Where(member => member.Kind() == SyntaxKind.MethodDeclaration))
{
var methodSyntax = (MethodDeclarationSyntax)member;
var methodSymbol = classModel.GetDeclaredSymbol(methodSyntax);
if (methodSymbol != null && methodSymbol.Name == "BuildServiceProvider")
{
return methodSymbol;
}
}

return null;
}

/// <summary>
/// Returns true if <paramref name="buildServiceProviderMethodSymbol"/> is a valid
/// BuildServiceProvider hook: a non-private method taking a single IServiceCollection
/// parameter and returning IServiceProvider or a type implementing it.
/// </summary>
public bool IsValidBuildServiceProviderMethod(IMethodSymbol buildServiceProviderMethodSymbol)
{
if (buildServiceProviderMethodSymbol == null)
{
return false;
}

if (buildServiceProviderMethodSymbol.DeclaredAccessibility == Accessibility.Private)
{
return false;
}

if (buildServiceProviderMethodSymbol.Parameters.Length != 1)
{
return false;
}

var iServiceCollectionSymbol = _context.Compilation.GetTypeByMetadataName("Microsoft.Extensions.DependencyInjection.IServiceCollection");
if (!buildServiceProviderMethodSymbol.Parameters[0].Type.Equals(iServiceCollectionSymbol, SymbolEqualityComparer.Default))
{
return false;
}

var iServiceProviderSymbol = _context.Compilation.GetTypeByMetadataName("System.IServiceProvider");
var returnType = buildServiceProviderMethodSymbol.ReturnType;
if (returnType.Equals(iServiceProviderSymbol, SymbolEqualityComparer.Default))
{
return true;
}

return returnType.AllInterfaces.Any(i => i.Equals(iServiceProviderSymbol, SymbolEqualityComparer.Default));
}

/// <summary>
/// Returns semantic model of <see cref="syntax"/>
/// </summary>
Expand Down
Loading