-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Source-Gen][Bug Fix] Array input in SignalR generated correctly (#2344)
- Loading branch information
Showing
4 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/SignalRTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
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 SignalRTests | ||
{ | ||
private readonly Assembly[] _referencedExtensionAssemblies; | ||
|
||
public SignalRTests() | ||
{ | ||
var abstractionsExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Abstractions.dll"); | ||
var hostingExtension = typeof(HostBuilder).Assembly; | ||
var diExtension = typeof(DefaultServiceProviderFactory).Assembly; | ||
var hostingAbExtension = typeof(IHost).Assembly; | ||
var diAbExtension = typeof(IServiceCollection).Assembly; | ||
var signalRExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.SignalRService.dll"); | ||
var httpExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Http.dll"); | ||
|
||
_referencedExtensionAssemblies = new[] | ||
{ | ||
abstractionsExtension, | ||
hostingExtension, | ||
hostingAbExtension, | ||
diExtension, | ||
diAbExtension, | ||
signalRExtension, | ||
httpExtension | ||
}; | ||
} | ||
|
||
[Theory] | ||
[InlineData(LanguageVersion.CSharp7_3)] | ||
[InlineData(LanguageVersion.Latest)] | ||
public async Task SignalRFunctionWithClaimsList(LanguageVersion languageVersion) | ||
{ | ||
string inputCode = """ | ||
using System; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
namespace FunctionApp | ||
{ | ||
public class SignalRFunction | ||
{ | ||
[Function("negotiate")] | ||
public string Negotiate([HttpTrigger(AuthorizationLevel.Function)] HttpRequestData req, [SignalRConnectionInfoInput(HubName = "TestFunctions", UserId = "{headers.user}", | ||
IdToken = "{headers.token}", | ||
ClaimTypeList = new []{"user_id"})] string connectionInfo) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
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"":""Function""}"); | ||
Function0RawBindings.Add(@"{""name"":""connectionInfo"",""type"":""signalRConnectionInfo"",""direction"":""In"",""hubName"":""TestFunctions"",""userId"":""{headers.user}"",""idToken"":""{headers.token}"",""claimTypeList"":[""user_id""],""dataType"":""String""}"); | ||
Function0RawBindings.Add(@"{""name"":""$return"",""type"":""http"",""direction"":""Out""}"); | ||
var Function0 = new DefaultFunctionMetadata | ||
{ | ||
Language = "dotnet-isolated", | ||
Name = "negotiate", | ||
EntryPoint = "FunctionApp.SignalRFunction.Negotiate", | ||
RawBindings = Function0RawBindings, | ||
ScriptFile = "TestProject.dll" | ||
}; | ||
metadataList.Add(Function0); | ||
return 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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters