Skip to content

Commit

Permalink
Use full namespace for Task.FromResult in function metadata provider …
Browse files Browse the repository at this point in the history
…generator to avoid namespace conflict (#2681)
  • Loading branch information
DL444 authored Oct 16, 2024
1 parent 30fd904 commit b1a94a1
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
{
var metadataList = new List<IFunctionMetadata>();
{{functionMetadataInfo}}
return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

- Changed `FunctionExecutorGenerator` to avoid generation of long `if`/`else` chains for apps with a large number of functions.

- Use full namespace for `Task.FromResult` in function metadata provider generator to avoid namespace conflict (#2681)

- <entry>
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker.Sdk.Generators;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Microsoft.Azure.Functions.SdkGeneratorTests
{
public partial class FunctionMetadataProviderGeneratorTests
{
public class AmbiguousNamespaceTests
{
private readonly Assembly[] _referencedExtensionAssemblies;

public AmbiguousNamespaceTests()
{
// load all extensions used in tests (match extensions tested on E2E app? Or include ALL extensions?)
var abstractionsExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Abstractions.dll");
var httpExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Http.dll");
var hostingExtension = typeof(HostBuilder).Assembly;
var diExtension = typeof(DefaultServiceProviderFactory).Assembly;
var hostingAbExtension = typeof(IHost).Assembly;
var diAbExtension = typeof(IServiceCollection).Assembly;

_referencedExtensionAssemblies = new[]
{
abstractionsExtension,
httpExtension,
hostingExtension,
hostingAbExtension,
diExtension,
diAbExtension
};
}

[Theory]
[InlineData(LanguageVersion.CSharp7_3)]
[InlineData(LanguageVersion.CSharp8)]
[InlineData(LanguageVersion.CSharp9)]
[InlineData(LanguageVersion.CSharp10)]
[InlineData(LanguageVersion.CSharp11)]
[InlineData(LanguageVersion.Latest)]
public async Task NamespaceEndingWithTask(LanguageVersion languageVersion)
{
string inputCode = """
using System;
using System.Collections.Generic;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace MyCompany.Task
{
public static class HttpTriggerSimple
{
[Function(nameof(HttpTriggerSimple))]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.User, "get")] HttpRequestData req, FunctionContext c)
{
return Run(req);
}

public static HttpResponseData Run(HttpRequestData req)
=> req.CreateResponse(System.Net.HttpStatusCode.OK);
}
}
""";

string expectedGeneratedFileName = $"GeneratedFunctionMetadataProvider.g.cs";
string expectedOutput = """
// <auto-generated/>
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Core.FunctionMetadata;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace TestProject
{
/// <summary>
/// Custom <see cref="IFunctionMetadataProvider"/> implementation that returns function metadata definitions for the current worker."/>
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class GeneratedFunctionMetadataProvider : IFunctionMetadataProvider
{
/// <inheritdoc/>
public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string directory)
{
var metadataList = new List<IFunctionMetadata>();
var Function0RawBindings = new List<string>();
Function0RawBindings.Add(@"{""name"":""req"",""type"":""httpTrigger"",""direction"":""In"",""authLevel"":""User"",""methods"":[""get""]}");
Function0RawBindings.Add(@"{""name"":""$return"",""type"":""http"",""direction"":""Out""}");

var Function0 = new DefaultFunctionMetadata
{
Language = "dotnet-isolated",
Name = "HttpTriggerSimple",
EntryPoint = "MyCompany.Task.HttpTriggerSimple.Run",
RawBindings = Function0RawBindings,
ScriptFile = "TestProject.dll"
};
metadataList.Add(Function0);

return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

/// <summary>
/// Extension methods to enable registration of the custom <see cref="IFunctionMetadataProvider"/> implementation generated for the current worker.
/// </summary>
public static class WorkerHostBuilderFunctionMetadataProviderExtension
{
///<summary>
/// Adds the GeneratedFunctionMetadataProvider to the service collection.
/// During initialization, the worker will return generated function metadata instead of relying on the Azure Functions host for function indexing.
///</summary>
public static IHostBuilder ConfigureGeneratedFunctionMetadataProvider(this IHostBuilder builder)
{
builder.ConfigureServices(s =>
{
s.AddSingleton<IFunctionMetadataProvider, GeneratedFunctionMetadataProvider>();
});
return builder;
}
}
}
""";

await TestHelpers.RunTestAsync<FunctionMetadataProviderGenerator>(
_referencedExtensionAssemblies,
inputCode,
expectedGeneratedFileName,
expectedOutput,
languageVersion: languageVersion);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
}};
metadataList.Add(Function0);
return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function2);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function5);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -307,7 +307,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function4);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -258,7 +258,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -361,7 +361,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -552,7 +552,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function3);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -693,7 +693,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function1);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -821,7 +821,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function1);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -315,7 +315,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -442,7 +442,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function1);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function1);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -299,7 +299,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function1);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -423,7 +423,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -532,7 +532,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -627,7 +627,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -723,7 +723,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -854,7 +854,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function2);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -987,7 +987,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function2);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down Expand Up @@ -216,7 +216,7 @@ public Task<ImmutableArray<IFunctionMetadata>> GetFunctionMetadataAsync(string d
};
metadataList.Add(Function0);

return Task.FromResult(metadataList.ToImmutableArray());
return global::System.Threading.Tasks.Task.FromResult(metadataList.ToImmutableArray());
}
}

Expand Down
Loading

0 comments on commit b1a94a1

Please sign in to comment.