diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/TypeProvider.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/TypeProvider.cs index 210c7046eb..e8e608cb3f 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/TypeProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/TypeProvider.cs @@ -6,7 +6,6 @@ using System.Linq; using Microsoft.Generator.CSharp.Expressions; using Microsoft.Generator.CSharp.Primitives; -using Microsoft.Generator.CSharp.SourceInput; using Microsoft.Generator.CSharp.Statements; namespace Microsoft.Generator.CSharp.Providers @@ -151,23 +150,6 @@ private TypeSignatureModifiers GetDeclarationModifiersInternal() protected virtual PropertyProvider[] BuildProperties() => []; - private HashSet BuildPropertyNames() - { - var propertyNames = new HashSet(); - foreach (var property in Properties) - { - propertyNames.Add(property.Name); - foreach (var attribute in property.Attributes ?? []) - { - if (CodeGenAttributes.TryGetCodeGenMemberAttributeValue(attribute, out var name)) - { - propertyNames.Add(name); - } - } - } - return propertyNames; - } - protected virtual FieldProvider[] BuildFields() => []; protected virtual CSharpType[] BuildImplements() => []; 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 3df6aedbb8..6aa064b5d3 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 @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Linq; using System.Threading.Tasks; using Microsoft.Generator.CSharp.Input; using Microsoft.Generator.CSharp.Primitives; @@ -202,5 +203,45 @@ public async Task CanChangeExtensibleEnumToEnum() Assert.AreEqual("Val2", enumProvider.EnumValues[1].Name); Assert.AreEqual("Val3", enumProvider.EnumValues[2].Name); } + + [Test] + public async Task CanAddProperties() + { + await MockHelpers.LoadMockPluginAsync(compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); + + 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 custom properties shouldn't be added to the model provider + Assert.AreEqual(1, modelTypeProvider.Properties.Count); + Assert.AreEqual("Prop1", modelTypeProvider.Properties[0].Name); + + // the custom properties shouldn't be parameters of the model's ctor + var modelCtors = modelTypeProvider.Constructors; + foreach (var ctor in modelCtors) + { + Assert.IsFalse(ctor.Signature.Parameters.Any(p => p.Name == "newProperty" || p.Name == "newProperty2")); + } + + // the custom properties should be added to the custom code view + Assert.AreEqual(2, customCodeView!.Properties.Count); + Assert.AreEqual("NewProperty", customCodeView.Properties[0].Name); + Assert.AreEqual(new CSharpType(typeof(int)), customCodeView.Properties[0].Type); + Assert.AreEqual(MethodSignatureModifiers.Public, customCodeView.Properties[0].Modifiers); + Assert.IsTrue(customCodeView.Properties[0].Body.HasSetter); + + Assert.AreEqual("NewProperty2", customCodeView.Properties[1].Name); + Assert.AreEqual(new CSharpType(typeof(string)), customCodeView.Properties[1].Type); + Assert.AreEqual(MethodSignatureModifiers.Public, customCodeView.Properties[1].Modifiers); + Assert.IsFalse(customCodeView.Properties[1].Body.HasSetter); + } } } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviders/TestData/ModelCustomizationTests/CanAddProperties/MockInputModel.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviders/TestData/ModelCustomizationTests/CanAddProperties/MockInputModel.cs new file mode 100644 index 0000000000..a596351ecd --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelProviders/TestData/ModelCustomizationTests/CanAddProperties/MockInputModel.cs @@ -0,0 +1,9 @@ +#nullable disable + +namespace Sample.Models; + +internal partial class MockInputModel +{ + public int NewProperty { get; set; } + public string NewProperty2 { get; } +}