Skip to content

Commit

Permalink
[Bug Fix] Parse HTTP result bindings in legacy generator (#2342)
Browse files Browse the repository at this point in the history
  • Loading branch information
satvu authored Apr 11, 2024
1 parent de33408 commit 3a9a560
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 5 deletions.
3 changes: 2 additions & 1 deletion extensions/Worker.Extensions.Http/src/HttpResultAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.Azure.Functions.Worker.Extensions.Abstractions;

namespace Microsoft.Azure.Functions.Worker
{
/// <summary>
/// Attribute used to mark an HTTP Response on an HTTP Trigger function with multiple output bindings.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class HttpResultAttribute : Attribute
public sealed class HttpResultAttribute : OutputBindingAttribute
{
}
}
1 change: 1 addition & 0 deletions sdk/Sdk/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal static class Constants
internal const string FunctionNameType = "Microsoft.Azure.Functions.Worker.FunctionAttribute";
internal const string ExtensionsInformationType = "Microsoft.Azure.Functions.Worker.Extensions.Abstractions.ExtensionInformationAttribute";
internal const string HttpResponseType = "Microsoft.Azure.Functions.Worker.Http.HttpResponseData";
internal const string HttpResultAttributeType = "Microsoft.Azure.Functions.Worker.HttpResultAttribute";
internal const string DefaultValueAttributeType = "Microsoft.Azure.Functions.Worker.Extensions.Abstractions.DefaultValueAttribute";
internal const string FixedDelayRetryAttributeType = "Microsoft.Azure.Functions.Worker.FixedDelayRetryAttribute";
internal const string ExponentialBackoffRetryAttributeType = "Microsoft.Azure.Functions.Worker.ExponentialBackoffRetryAttribute";
Expand Down
17 changes: 15 additions & 2 deletions sdk/Sdk/FunctionMetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private bool TryAddOutputBindingsFromProperties(IList<ExpandoObject> bindingMeta

foreach (PropertyDefinition property in typeDefinition.Properties)
{
if (string.Equals(property.PropertyType.FullName, Constants.HttpResponseType, StringComparison.Ordinal))
if (string.Equals(property.PropertyType.FullName, Constants.HttpResponseType, StringComparison.Ordinal) || HasHttpResultAttribute(property))
{
if (foundHttpOutput)
{
Expand All @@ -319,6 +319,19 @@ private bool TryAddOutputBindingsFromProperties(IList<ExpandoObject> bindingMeta
return bindingMetadata.Count > beforeCount;
}

private bool HasHttpResultAttribute(PropertyDefinition property)
{
foreach (var attribute in property.CustomAttributes)
{
if (string.Equals(attribute.AttributeType.FullName, Constants.HttpResultAttributeType, StringComparison.Ordinal))
{
return true;
}
}

return false;
}

private void AddOutputBindingFromProperty(IList<ExpandoObject> bindingMetadata, PropertyDefinition property, string typeName)
{
bool foundOutputAttribute = false;
Expand Down Expand Up @@ -351,7 +364,7 @@ private bool TryAddOutputBindingFromMethod(IList<ExpandoObject> bindingMetadata,
{
if (foundBinding)
{
throw new FunctionsMetadataGenerationException($"Found multiple Output bindings on method '{method.FullName}'. " +
throw new FunctionsMetadataGenerationException($"Found multiple output bindings on method '{method.FullName}'. " +
"Please use an encapsulation to define the bindings in properties. For more information: https://aka.ms/dotnet-worker-poco-binding.");
}

Expand Down
14 changes: 13 additions & 1 deletion sdk/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@

- Re-add SDK refactors that were reverted in #2313 (#2347)
- Address assembly scanning regression (#2347)
- Updating to use `Microsoft.NET.Sdk.Functions.Generators` 1.2.2 (#2247)
- Updating to use `Microsoft.NET.Sdk.Functions.Generators` 1.3.0 (#2322)
- Update legacy generator to handle `HttpResultAttribute` (#2342), which is used on HTTP response properties in [multiple output-binding scenarios](https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=windows#multiple-output-bindings). Example:

```csharp
public class MyOutputType
{
[QueueOutput("myQueue")]
public string Name { get; set; }

[HttpResult]
public IActionResult HttpResponse { get; set; }
}
```

### Microsoft.Azure.Functions.Worker.Sdk.Generators 1.3.0

Expand Down
Loading

0 comments on commit 3a9a560

Please sign in to comment.