diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviders/ModelCustomizationTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviders/ModelCustomizationTests.cs index f18551947d..af9449bad7 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviders/ModelCustomizationTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviders/ModelCustomizationTests.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using Microsoft.Generator.CSharp.Input; +using Microsoft.Generator.CSharp.Primitives; using Microsoft.Generator.CSharp.Providers; using Microsoft.Generator.CSharp.Tests.Common; using NUnit.Framework; @@ -25,11 +26,7 @@ public void TestCustomization_CanChangeModelName() var modelTypeProvider = new ModelProvider(inputModel); var customCodeView = modelTypeProvider.CustomCodeView; - Assert.IsNotNull(customCodeView); - Assert.AreEqual("CustomizedModel", modelTypeProvider.Type.Name); - Assert.AreEqual("NewNamespace.Models", modelTypeProvider.Type.Namespace); - Assert.AreEqual(customCodeView?.Name, modelTypeProvider.Type.Name); - Assert.AreEqual(customCodeView?.Type.Namespace, modelTypeProvider.Type.Namespace); + AssertCommon(customCodeView, modelTypeProvider, "NewNamespace.Models", "CustomizedModel"); } [Test] @@ -46,11 +43,7 @@ public void TestCustomization_CanChangePropertyName() var modelTypeProvider = new ModelProvider(inputModel); var customCodeView = modelTypeProvider.CustomCodeView; - Assert.IsNotNull(customCodeView); - Assert.AreEqual("MockInputModel", modelTypeProvider.Type.Name); - Assert.AreEqual("Sample.Models", modelTypeProvider.Type.Namespace); - Assert.AreEqual(customCodeView?.Name, modelTypeProvider.Type.Name); - Assert.AreEqual(customCodeView?.Type.Namespace, modelTypeProvider.Type.Namespace); + AssertCommon(customCodeView, modelTypeProvider, "Sample.Models", "MockInputModel"); // the property should be filtered from the model provider Assert.AreEqual(0, modelTypeProvider.Properties.Count); @@ -59,5 +52,64 @@ public void TestCustomization_CanChangePropertyName() Assert.AreEqual(1, customCodeView!.Properties.Count); Assert.AreEqual("Prop2", customCodeView.Properties[0].Name); } + + [Test] + public void TestCustomization_CanChangePropertyType() + { + MockHelpers.LoadMockPlugin(customization: Helpers.GetCompilationFromFile()); + + var props = new[] + { + InputFactory.Property("Prop1", InputFactory.Array(InputPrimitiveType.String)) + }; + + var inputModel = InputFactory.Model("mockInputModel", properties: props); + var modelTypeProvider = new ModelProvider(inputModel); + var customCodeView = modelTypeProvider.CustomCodeView; + + AssertCommon(customCodeView, modelTypeProvider, "Sample.Models", "MockInputModel"); + + // the property should be filtered from the model provider + Assert.AreEqual(0, modelTypeProvider.Properties.Count); + + // the property should be added to the custom code view + Assert.AreEqual(1, customCodeView!.Properties.Count); + // the property type should be changed + Assert.AreEqual(new CSharpType(typeof(int[])), customCodeView.Properties[0].Type); + } + + [Test] + public void TestCustomization_CanChangePropertyAccessibility() + { + MockHelpers.LoadMockPlugin(customization: Helpers.GetCompilationFromFile()); + + var props = new[] + { + InputFactory.Property("Prop1", InputFactory.Array(InputPrimitiveType.String)) + }; + + var inputModel = InputFactory.Model("mockInputModel", properties: props); + var modelTypeProvider = new ModelProvider(inputModel); + var customCodeView = modelTypeProvider.CustomCodeView; + + AssertCommon(customCodeView, modelTypeProvider, "Sample.Models", "MockInputModel"); + + // the property should be filtered from the model provider + Assert.AreEqual(0, modelTypeProvider.Properties.Count); + + // the property should be added to the custom code view + Assert.AreEqual(1, customCodeView!.Properties.Count); + // the property accessibility should be changed + Assert.IsTrue(customCodeView.Properties[0].Modifiers.HasFlag(MethodSignatureModifiers.Internal)); + } + + private static void AssertCommon(TypeProvider? customCodeView, ModelProvider modelTypeProvider, string expectedNamespace, string expectedName) + { + Assert.IsNotNull(customCodeView); + Assert.AreEqual(expectedNamespace, modelTypeProvider.Type.Namespace); + Assert.AreEqual(expectedName, modelTypeProvider.Type.Name); + Assert.AreEqual(customCodeView?.Name, modelTypeProvider.Type.Name); + Assert.AreEqual(customCodeView?.Type.Namespace, modelTypeProvider.Type.Namespace); + } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/TestData/ModelCustomizationTests/TestCustomization_CanChangePropertyAccessibility.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/TestData/ModelCustomizationTests/TestCustomization_CanChangePropertyAccessibility.cs new file mode 100644 index 0000000000..825f168817 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/TestData/ModelCustomizationTests/TestCustomization_CanChangePropertyAccessibility.cs @@ -0,0 +1,27 @@ +#nullable disable + +using System; + +namespace Sample +{ + // TODO: if we decide to use the public APIs, we do not have to define this attribute here. Tracking: https://github.com/Azure/autorest.csharp/issues/4551 + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] + internal class CodeGenMemberAttribute : Attribute + { + public CodeGenMemberAttribute() : base(null) + { + } + + public CodeGenMemberAttribute(string originalName) : base(originalName) + { + } + } +} +namespace Sample.Models +{ + public partial class MockInputModel + { + [CodeGenMember("Prop1")] + internal string[] Prop2 { get; set; } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/TestData/ModelCustomizationTests/TestCustomization_CanChangePropertyType.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/TestData/ModelCustomizationTests/TestCustomization_CanChangePropertyType.cs new file mode 100644 index 0000000000..e1d8feb757 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/TestData/ModelCustomizationTests/TestCustomization_CanChangePropertyType.cs @@ -0,0 +1,27 @@ +#nullable disable + +using System; + +namespace Sample +{ + // TODO: if we decide to use the public APIs, we do not have to define this attribute here. Tracking: https://github.com/Azure/autorest.csharp/issues/4551 + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] + internal class CodeGenMemberAttribute : Attribute + { + public CodeGenMemberAttribute() : base(null) + { + } + + public CodeGenMemberAttribute(string originalName) : base(originalName) + { + } + } +} +namespace Sample.Models +{ + public partial class MockInputModel + { + [CodeGenMember("Prop1")] + public int[] Prop2 { get; set; } + } +}