Skip to content
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

Fix to model factory invoking of full constructor #5108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading