Skip to content

Commit

Permalink
Fix to model factory invoking of full constructor (#5108)
Browse files Browse the repository at this point in the history
Fixes #5107
  • Loading branch information
JoshLove-msft authored Nov 14, 2024
1 parent b242e5e commit 90bbc22
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,8 @@ protected override MethodProvider[] BuildMethods()
binaryDataParam = constructor.Signature.Parameters
.FirstOrDefault(p => p?.Type.Equals(typeof(IDictionary<string, BinaryData>)) == true, binaryDataParam);

if (customCtorParamCount > fullCtorParamCount)
{
fullConstructor = constructor;
break;
}
fullConstructor = constructor;
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ public async Task CanChangeAccessibilityOfModelFactory()
ValidateModelFactoryCommon(modelFactory);
}

[Test]
public async Task CanCustomizeModelFullConstructor()
[TestCase(true)]
[TestCase(false)]
public async Task CanCustomizeModelFullConstructor(bool extraParameters)
{
var plugin = await MockHelpers.LoadMockPluginAsync(
inputModelTypes: [
Expand All @@ -137,7 +138,7 @@ public async Task CanCustomizeModelFullConstructor()
InputFactory.Property("Prop1", InputPrimitiveType.String, isRequired: true),
])
],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync(),
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync(extraParameters.ToString()),
additionalMetadataReferences: [MetadataReference.CreateFromFile(typeof(BinaryData).Assembly.Location)]);

var csharpGen = new CSharpGen();
Expand All @@ -159,13 +160,22 @@ public async Task CanCustomizeModelFullConstructor()
var modelFactoryMethod = modelFactoryMethods[0];
Assert.AreEqual("MockInputModel", modelFactoryMethod.Signature.Name);

Assert.AreEqual(2, modelFactoryMethod.Signature.Parameters.Count);
Assert.AreEqual("data", modelFactoryMethod.Signature.Parameters[0].Name);
Assert.AreEqual("prop1", modelFactoryMethod.Signature.Parameters[1].Name);

Assert.IsTrue(modelFactoryMethod.BodyStatements!.ToDisplayString()
.Contains("return new global::Sample.Models.MockInputModel(data?.ToList(), prop1, additionalBinaryDataProperties: null);"),
modelFactoryMethod.BodyStatements!.ToDisplayString());
Assert.AreEqual(extraParameters ? 2 : 1, modelFactoryMethod.Signature.Parameters.Count);
if (extraParameters)
{
Assert.AreEqual("data", modelFactoryMethod.Signature.Parameters[0].Name);
Assert.AreEqual("prop1", modelFactoryMethod.Signature.Parameters[1].Name);
Assert.IsTrue(modelFactoryMethod.BodyStatements!.ToDisplayString()
.Contains("return new global::Sample.Models.MockInputModel(data?.ToList(), prop1, additionalData: null);"),
modelFactoryMethod.BodyStatements!.ToDisplayString());
}
else
{
Assert.AreEqual("prop1", modelFactoryMethod.Signature.Parameters[0].Name);
Assert.IsTrue(modelFactoryMethod.BodyStatements!.ToDisplayString()
.Contains("return new global::Sample.Models.MockInputModel(prop1, additionalData: null);"),
modelFactoryMethod.BodyStatements!.ToDisplayString());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Generator.CSharp.Customization;

#nullable disable

namespace Sample.Models;

public partial class MockInputModel
{
internal MockInputModel(string prop1, IDictionary<string, BinaryData> additionalData)
{
Prop1 = prop1;
_additionalBinaryDataProperties = additionalData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public partial class MockInputModel
{
private readonly IReadOnlyList<MockInputModel> _data;

internal MockInputModel(IReadOnlyList<MockInputModel> data, string prop1, IDictionary<string, BinaryData> additionalBinaryDataProperties)
internal MockInputModel(IReadOnlyList<MockInputModel> data, string prop1, IDictionary<string, BinaryData> additionalData)
{
Prop1 = prop1;
_data = data;
_additionalBinaryDataProperties = additionalBinaryDataProperties;
_additionalBinaryDataProperties = additionalData;
}
}

0 comments on commit 90bbc22

Please sign in to comment.