Skip to content
Open
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 @@ -459,6 +459,13 @@ private static IReadOnlyList<ParameterProvider> GetParameters(
continue;
}

// Skip parameters for properties that were customized via CodeGenMember to be non-public
if (param.Property?.OriginalName != null
&& !param.Property!.Modifiers.HasFlag(MethodSignatureModifiers.Public))
{
continue;
}

parameters.Add(GetModelFactoryParam(param));
}
return [.. parameters];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,36 @@ public void RequiredConstantPropertiesAreNotExposedAsParameters()
Assert.AreEqual("return new global::Sample.Models.MockInputModel(\"constant\", prop2, additionalBinaryDataProperties: null);\n", factoryMethod.BodyStatements!.ToDisplayString());
}

[Test]
public async Task SkipsCodeGenMemberInternalProperty()
{
var inputModel = InputFactory.Model(
"mockInputModel",
properties:
[
InputFactory.Property("prop1", InputPrimitiveType.String),
InputFactory.Property("prop2", InputPrimitiveType.String)
]);

var mockGenerator = await MockHelpers.LoadMockGeneratorAsync(
inputModelTypes: [inputModel],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());

var modelFactoryProvider = mockGenerator.Object.OutputLibrary.TypeProviders.OfType<ModelFactoryProvider>().SingleOrDefault();
Assert.IsNotNull(modelFactoryProvider);

var factoryMethod = modelFactoryProvider!.Methods.FirstOrDefault(m => m.Signature.Name == "MockInputModel");
Assert.IsNotNull(factoryMethod);

// prop1 is customized via CodeGenMember to be internal, so it should not appear in the factory method
Assert.AreEqual(1, factoryMethod!.Signature.Parameters.Count);
Assert.AreEqual("prop2", factoryMethod.Signature.Parameters[0].Name);

// The body should pass default for the internal property
var body = factoryMethod.BodyStatements!.ToDisplayString();
StringAssert.Contains("default", body);
}

private static InputModelType[] GetTestModels()
{
InputType additionalPropertiesUnknown = InputPrimitiveType.Any;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#nullable disable

using Microsoft.TypeSpec.Generator.Customizations;

namespace Sample.Models
{
public partial class MockInputModel
{
[CodeGenMember("Prop1")]
internal string Prop1Internal { get; set; }
}
}
Loading