diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/MethodProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/MethodProvider.cs index 07c8c211a7..710b28591d 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/MethodProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/MethodProvider.cs @@ -114,6 +114,8 @@ public void Update( return updated.Accept(visitor); } + Signature = updated.Signature; + if (BodyExpression != null) { var expression = BodyExpression.Accept(visitor, this); diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ParameterProvider.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ParameterProvider.cs index 8cb57c49bf..4f17b987aa 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ParameterProvider.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ParameterProvider.cs @@ -19,28 +19,28 @@ namespace Microsoft.TypeSpec.Generator.Providers [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] public sealed class ParameterProvider : IEquatable { - public string Name { get; } - public FormattableString Description { get; } + public string Name { get; private set; } + public FormattableString Description { get; private set; } public CSharpType Type { get; set; } /// /// The default value of the parameter. /// public ValueExpression? DefaultValue { get; set; } - public ValueExpression? InitializationValue { get; init; } - public ParameterValidationType Validation { get; init; } = ParameterValidationType.None; - public bool IsRef { get; } - public bool IsOut { get; } - public bool IsParams { get; } + public ValueExpression? InitializationValue { get; private set; } + public ParameterValidationType Validation { get; set; } = ParameterValidationType.None; + public bool IsRef { get; private set; } + public bool IsOut { get; private set; } + public bool IsParams { get; private set; } - internal IReadOnlyList Attributes { get; } = []; - public WireInformation WireInfo { get; } - public ParameterLocation Location { get; } + internal IReadOnlyList Attributes { get; private set; } = []; + public WireInformation WireInfo { get; private set; } + public ParameterLocation Location { get; private set; } /// /// This property tracks which property this parameter is constructed from. /// - public PropertyProvider? Property { get; } + public PropertyProvider? Property { get; private set; } /// /// This property tracks which field this parameter is constructed from. @@ -237,5 +237,98 @@ internal ParameterProvider WithRef() _asVariable = AsExpression, }; } + + /// + /// Updates the parameter with the given name. + /// + public void Update( + string? name = null, + FormattableString? description = null, + CSharpType? type = null, + ValueExpression? defaultValue = null, + bool? isRef = null, + bool? isOut = null, + bool? isParams = null, + IReadOnlyList? attributes = null, + PropertyProvider? property = null, + FieldProvider? field = null, + ValueExpression? initializationValue = null, + ParameterLocation? location = null, + WireInformation? wireInfo = null, + ParameterValidationType? validation = null) + { + if (name is not null) + { + Name = name; + _asVariable?.Update(name: name); + } + + if (description is not null) + { + Description = description; + } + + if (type is not null) + { + Type = type; + _asVariable?.Update(type: type); + } + + if (defaultValue is not null) + { + DefaultValue = defaultValue; + } + + if (isRef is not null) + { + IsRef = isRef.Value; + _asVariable?.Update(isRef: IsRef); + } + + if (isOut is not null) + { + IsOut = isOut.Value; + } + + if (isParams is not null) + { + IsParams = isParams.Value; + } + + if (attributes is not null) + { + Attributes = attributes; + } + + if (property is not null) + { + Property = property; + } + + if (field is not null) + { + Field = field; + } + + if (initializationValue is not null) + { + InitializationValue = initializationValue; + } + + if (location is not null) + { + Location = location.Value; + } + + if (wireInfo is not null) + { + WireInfo = wireInfo; + } + + if (validation is not null) + { + Validation = validation.Value; + } + } } } diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/OutputLibraryVisitorTests.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/OutputLibraryVisitorTests.cs index 64ffac457e..b6c156dfea 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/OutputLibraryVisitorTests.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/OutputLibraryVisitorTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Linq; using Microsoft.TypeSpec.Generator.Primitives; using Microsoft.TypeSpec.Generator.Providers; using Microsoft.TypeSpec.Generator.Snippets; @@ -185,5 +186,35 @@ public void DoesNotVisitFieldsWhenTypeIsNulledOut() _mockVisitor.Protected().Verify("VisitType", Times.Once(), _mockTypeProvider.Object); _mockVisitor.Protected().Verify("VisitField", Times.Never(), mockFieldProvider.Object); } + + [Test] + public void VisitMethodToRenameParameterName() + { + var parameter = new ParameterProvider("oldName", $"", typeof(string)); + var testMethod = new MethodProvider( + new MethodSignature("Test", $"", MethodSignatureModifiers.Public, null, $"", [parameter]), + Snippet.Return(parameter), new TestTypeProvider()); + + testMethod.Accept(new MethodVisitor()); + + Assert.AreEqual("newName", testMethod.Signature.Parameters.First().Name); + Assert.AreEqual("return newName;\n", testMethod?.BodyStatements!.ToDisplayString()); + } + + private class MethodVisitor : LibraryVisitor + { + protected internal override MethodProvider? VisitMethod(MethodProvider method) + { + // Rename the parameter to "newName" + foreach (var parameter in method.Signature.Parameters) + { + if (parameter.Name == "oldName") + { + parameter.Update("newName"); + } + } + return base.VisitMethod(method); + } + } } }