-
Notifications
You must be signed in to change notification settings - Fork 201
[SourceGen] Fix for duplicate properties generated #3227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
586e88d
initial iteration of fix and test
satvu 943b0ea
add test, renames
satvu bc3d083
Merge branch 'main' into satvu/source-gen-prop-fix
satvu 38e161a
bump generator version, add to release notes
satvu a0b1efe
add more to release notes
satvu e93c1c6
Merge branch 'satvu/source-gen-prop-fix' of https://github.com/Azure/…
satvu 4e46f78
test if package upgrade fixes build problem
satvu 94fc569
resolve comments
satvu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
266 changes: 266 additions & 0 deletions
266
test/Sdk.Generator.Tests/FunctionMetadataProvider/BindingPropertiesParsingTests.cs
This file contains hidden or 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,266 @@ | ||
| // 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.Sdk.Generator.Tests; | ||
| 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.Sdk.Generator.FunctionMetadataProvider.Tests | ||
| { | ||
| public partial class FunctionMetadataProviderGeneratorTests | ||
| { | ||
| public class BindingPropertiesParsingTests | ||
| { | ||
| private readonly Assembly[] _referencedExtensionAssemblies; | ||
|
|
||
| public BindingPropertiesParsingTests() | ||
| { | ||
| 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; | ||
| var mcpExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Mcp.dll"); | ||
| var blobExtension = Assembly.LoadFrom("Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs.dll"); | ||
|
|
||
| _referencedExtensionAssemblies = new[] | ||
| { | ||
| abstractionsExtension, | ||
| httpExtension, | ||
| hostingExtension, | ||
| hostingAbExtension, | ||
| diExtension, | ||
| diAbExtension, | ||
| mcpExtension, | ||
| blobExtension | ||
| }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LanguageVersion.CSharp7_3)] | ||
| [InlineData(LanguageVersion.CSharp8)] | ||
| [InlineData(LanguageVersion.CSharp9)] | ||
| [InlineData(LanguageVersion.CSharp10)] | ||
| [InlineData(LanguageVersion.CSharp11)] | ||
| [InlineData(LanguageVersion.Latest)] | ||
| public async Task BindingPropertiesWithDefaultValueDoeNotCreateDuplicatesWhenSetAsNamedArgument(LanguageVersion languageVersion) | ||
satvu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| string inputCode = """ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Http; | ||
| using Microsoft.Azure.Functions.Worker.Extensions.Mcp; | ||
| namespace MyCompany.Task | ||
| { | ||
| public static class SaveSnippetFunction | ||
| { | ||
| [Function(nameof(SaveSnippetFunction))] | ||
| [BlobOutput("blobPath")] | ||
| public static string SaveSnippet( | ||
| [McpToolTrigger("someString", "someString")] | ||
| ToolInvocationContext context, | ||
| [McpToolProperty("someString", "someString", IsRequired = true)] | ||
| string name | ||
| ) | ||
| { | ||
| 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)] | ||
| {{Constants.GeneratedCodeAttribute}} | ||
| 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"":""$return"",""type"":""blob"",""direction"":""Out"",""blobPath"":""blobPath""}"); | ||
| Function0RawBindings.Add(@"{""name"":""context"",""type"":""mcpToolTrigger"",""direction"":""In"",""toolName"":""someString"",""description"":""someString""}"); | ||
| Function0RawBindings.Add(@"{""name"":""name"",""type"":""mcpToolProperty"",""direction"":""In"",""propertyName"":""someString"",""description"":""someString"",""isRequired"":true,""dataType"":""String""}"); | ||
| var Function0 = new DefaultFunctionMetadata | ||
| { | ||
| Language = "dotnet-isolated", | ||
| Name = "SaveSnippetFunction", | ||
| EntryPoint = "MyCompany.Task.SaveSnippetFunction.SaveSnippet", | ||
| 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> | ||
| {{Constants.GeneratedCodeAttribute}} | ||
| 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); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(LanguageVersion.CSharp7_3)] | ||
| [InlineData(LanguageVersion.CSharp8)] | ||
| [InlineData(LanguageVersion.CSharp9)] | ||
| [InlineData(LanguageVersion.CSharp10)] | ||
| [InlineData(LanguageVersion.CSharp11)] | ||
| [InlineData(LanguageVersion.Latest)] | ||
| public async Task BindingPropertyWithDefaultValueIsSet(LanguageVersion languageVersion) | ||
| { | ||
| string inputCode = """ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Http; | ||
| using Microsoft.Azure.Functions.Worker.Extensions.Mcp; | ||
| namespace MyCompany.Task | ||
| { | ||
| public static class SaveSnippetFunction | ||
| { | ||
| [Function(nameof(SaveSnippetFunction))] | ||
| [BlobOutput("blobPath")] | ||
| public static string SaveSnippet( | ||
| [McpToolTrigger("someString", "someString")] | ||
| ToolInvocationContext context, | ||
| [McpToolProperty("someString", "someString")] | ||
| string name | ||
| ) | ||
| { | ||
| 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)] | ||
| {{Constants.GeneratedCodeAttribute}} | ||
| 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"":""$return"",""type"":""blob"",""direction"":""Out"",""blobPath"":""blobPath""}"); | ||
| Function0RawBindings.Add(@"{""name"":""context"",""type"":""mcpToolTrigger"",""direction"":""In"",""toolName"":""someString"",""description"":""someString""}"); | ||
| Function0RawBindings.Add(@"{""name"":""name"",""type"":""mcpToolProperty"",""direction"":""In"",""propertyName"":""someString"",""description"":""someString"",""isRequired"":false,""dataType"":""String""}"); | ||
| var Function0 = new DefaultFunctionMetadata | ||
| { | ||
| Language = "dotnet-isolated", | ||
| Name = "SaveSnippetFunction", | ||
| EntryPoint = "MyCompany.Task.SaveSnippetFunction.SaveSnippet", | ||
| 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> | ||
| {{Constants.GeneratedCodeAttribute}} | ||
| 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.