Skip to content

Commit

Permalink
Fix method/constructor parameter name matching (#4600)
Browse files Browse the repository at this point in the history
Relax the matching to account for the type name containing the fully
qualified type name for customized methods/ctors.
  • Loading branch information
JoshLove-msft authored Oct 3, 2024
1 parent a5e8fad commit fa10eac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ private static bool IsMatch(MethodSignatureBase customMethod, MethodSignatureBas

for (int i = 0; i < customMethod.Parameters.Count; i++)
{
if (customMethod.Parameters[i].Type.Name != method.Parameters[i].Type.Name)
if (!customMethod.Parameters[i].Type.Name.EndsWith(method.Parameters[i].Type.Name))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,23 +329,30 @@ await MockHelpers.LoadMockPluginAsync(
[Test]
public async Task CanReplaceConstructor()
{
var subModel = InputFactory.Model(
"subModel",
usage: InputModelTypeUsage.Input,
properties: new[] { InputFactory.Property("SubProperty", InputPrimitiveType.Int32) });

var plugin = await MockHelpers.LoadMockPluginAsync(
inputModelTypes: new[] {
InputFactory.Model(
"mockInputModel",
// use Input so that we generate a public ctor
usage: InputModelTypeUsage.Input,
properties: new[] { InputFactory.Property("Prop1", InputPrimitiveType.String) })
properties: new[]
{
InputFactory.Property("Prop1", InputPrimitiveType.String),
InputFactory.Property("SubModel", subModel)
})
},
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
var csharpGen = new CSharpGen();

await csharpGen.ExecuteAsync();

// The generated code should only contain the single internal ctor containing the properties
var ctor = plugin.Object.OutputLibrary.TypeProviders.Single(t => t.Name == "MockInputModel").Constructors.Single();
Assert.IsTrue(ctor.Signature.Modifiers.HasFlag(MethodSignatureModifiers.Internal));
Assert.AreEqual("prop1", ctor.Signature.Parameters.First().Name);
// The generated code should not contain any ctors
Assert.IsEmpty(plugin.Object.OutputLibrary.TypeProviders.Single(t => t.Name == "MockInputModel").Constructors);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ public partial class MockInputModel
internal MockInputModel()
{
}

internal MockInputModel(string prop1, SubModel? subModel, IDictionary<string, BinaryData> serializedAdditionalRawData)
{
}
}

public readonly partial struct SubModel
{
}

0 comments on commit fa10eac

Please sign in to comment.