Skip to content

Commit

Permalink
4.4.1 - Fixing NullReferenceException in IsWebJobsAttribute extension…
Browse files Browse the repository at this point in the history
… method (#655)

* Adding null check to `IsWebJobsAttribute` extension method.

* Fix method summary.

* Updating E2E test to include a function with nullable parameter type.
  • Loading branch information
kshyju authored Jul 31, 2024
1 parent 03a17c8 commit 1876491
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<MajorProductVersion>4</MajorProductVersion>
<MinorProductVersion>4</MinorProductVersion>
<PatchProductVersion>0</PatchProductVersion>
<PatchProductVersion>1</PatchProductVersion>

<!-- Clear this value for non-preview releases -->
<PreviewProductVersion></PreviewProductVersion>
Expand Down
15 changes: 11 additions & 4 deletions src/Microsoft.NET.Sdk.Functions.Generator/AttributeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ private static string ToAttributeFriendlyName(string name)
};

/// <summary>
///
/// Checks if a custom attribute is a WebJobs attribute.
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
/// <param name="attribute">The custom attribute to check.</param>
/// <returns>True if the attribute is a WebJobs attribute; otherwise, False.</returns>
public static bool IsWebJobsAttribute(this CustomAttribute attribute)
{
return attribute.AttributeType.Resolve().CustomAttributes.Any(a => a.AttributeType.FullName == "Microsoft.Azure.WebJobs.Description.BindingAttribute")
var attributeTypeDefinition = attribute.AttributeType?.Resolve();

if (attributeTypeDefinition == null)
{
return false;
}

return attributeTypeDefinition.CustomAttributes.Any(a => a.AttributeType.FullName == "Microsoft.Azure.WebJobs.Description.BindingAttribute")
|| _supportedAttributes.Contains(attribute.AttributeType.FullName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public class FunctionsV4SdkTests
private string _testsDirectory;
private TestInitialize _testInitializer;
private ITestOutputHelper _testOutputHelper;
private const string _testVersion = "v4";
private const string TestVersion = "v4";

public FunctionsV4SdkTests(ITestOutputHelper testOutputHelper)
{
_testInitializer = new TestInitialize(testOutputHelper, _testVersion);
_testInitializer = new TestInitialize(testOutputHelper, TestVersion);
_testOutputHelper = testOutputHelper;
_functionsSdkPackageSource = _testInitializer.FunctionsSdkPackageSource;
_testsDirectory = _testInitializer.TestDirectory;
Expand Down Expand Up @@ -68,7 +68,7 @@ public void BuildAndPublish_CopiesRuntimesToAdditionalBinFolder()
Assert.True(Directory.Exists(additionalBinDir));
var files = Directory.EnumerateFiles(additionalBinDir, "*.dll", SearchOption.AllDirectories);
Assert.True(files.Count() > 1);
// Test addional runtimes
// Test additional runtimes
files = Directory.EnumerateFiles(Path.Combine(additionalBinDir, "runtimes"), "*.dll", SearchOption.AllDirectories);
Assert.True(files.Count() > 1);

Expand Down Expand Up @@ -99,7 +99,7 @@ public void BuildAndPublish_GeneratesFunctions()
string dotnetArgs = $"build {projectFileToTest}.csproj --configuration {TestInitialize.Configuration}";
int? exitCode = new ProcessWrapper().RunProcess(TestInitialize.DotNetExecutable, dotnetArgs, projectFileDirectory, out int? _, createDirectoryIfNotExists: false, testOutputHelper: _testOutputHelper);
Assert.True(exitCode.HasValue && exitCode.Value == 0);
// Test addional bin
// Test additional bin
string binDir = Path.Combine(projectFileDirectory, "bin", TestInitialize.Configuration, TestInitialize.NetCoreFramework);
string additionalBinDir = Path.Combine(binDir, "bin");
Assert.True(Directory.Exists(additionalBinDir));
Expand All @@ -108,6 +108,8 @@ public void BuildAndPublish_GeneratesFunctions()
// Test functions generator output
string httpTriggerFunctionpath = Path.Combine(binDir, "HttpFunction", "function.json");
Assert.True(File.Exists(httpTriggerFunctionpath));
string httpTrigger2Functionpath = Path.Combine(binDir, "HttpFunction2", "function.json");
Assert.True(File.Exists(httpTrigger2Functionpath));

// Publish
dotnetArgs = $"publish {projectFileToTest}.csproj --configuration {TestInitialize.Configuration}";
Expand All @@ -122,6 +124,8 @@ public void BuildAndPublish_GeneratesFunctions()
// Test functions generator output
httpTriggerFunctionpath = Path.Combine(publishDir, "HttpFunction", "function.json");
Assert.True(File.Exists(httpTriggerFunctionpath));
httpTrigger2Functionpath = Path.Combine(publishDir, "HttpFunction2", "function.json");
Assert.True(File.Exists(httpTrigger2Functionpath));
}

private void UpdatePackageReference(string projectFileToTest, string projectFileDirectory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ public static async Task<IActionResult> Run(

return new OkObjectResult(responseMessage);
}

// A function which uses nullable type for parameter.
[FunctionName("HttpFunction2")]
public static async Task<IActionResult> Run2(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "hello/{id}")] HttpRequest req, ILogger log, string? id)
{
return new OkObjectResult($"Hello {id}");
}
}
}

0 comments on commit 1876491

Please sign in to comment.