From 0d98d60e10c22ee38cdf18bf8f010511059a2fc2 Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Tue, 27 May 2025 13:49:43 +0800 Subject: [PATCH 1/5] Apply VisitMethod to rename method parameter --- .../src/Providers/MethodProvider.cs | 2 ++ .../src/Providers/ParameterProvider.cs | 15 ++++++++- .../test/OutputLibraryVisitorTests.cs | 33 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) 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 07c8c211a79..710b28591d0 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 8cb57c49bff..95bbc797bdd 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,7 +19,7 @@ namespace Microsoft.TypeSpec.Generator.Providers [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] public sealed class ParameterProvider : IEquatable { - public string Name { get; } + public string Name { get; private set; } public FormattableString Description { get; } public CSharpType Type { get; set; } @@ -237,5 +237,18 @@ internal ParameterProvider WithRef() _asVariable = AsExpression, }; } + + /// + /// Updates the parameter with the given name. + /// + /// Name to update. + public void Update(string? name = null) + { + if (name is not null) + { + Name = name; + _asVariable?.Update(name: name); + } + } } } 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 64ffac457e3..16334a2e6c3 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,37 @@ 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()); + + var updatedMethod = testMethod.Accept(new MethodVisitor()); + Assert.IsNotNull(updatedMethod?.BodyStatements); + Assert.AreEqual("newName", updatedMethod?.Signature.Parameters.First().Name); + + var bodyStatements = updatedMethod?.BodyStatements!.ToDisplayString(); + Assert.AreEqual("return newName;\n", bodyStatements); + } + + 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); + } + } } } From aa0fe33af454f0052b4e688ac319d9cb6d7b0976 Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Tue, 27 May 2025 17:19:58 +0800 Subject: [PATCH 2/5] update test --- .../test/OutputLibraryVisitorTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 16334a2e6c3..143d5175980 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 @@ -196,7 +196,8 @@ public void VisitMethodToRenameParameterName() Snippet.Return(parameter), new TestTypeProvider()); var updatedMethod = testMethod.Accept(new MethodVisitor()); - Assert.IsNotNull(updatedMethod?.BodyStatements); + + Assert.AreEqual("newName", testMethod.Signature.Parameters.First().Name); Assert.AreEqual("newName", updatedMethod?.Signature.Parameters.First().Name); var bodyStatements = updatedMethod?.BodyStatements!.ToDisplayString(); From 5cb58884edbcf0590072f94e058045a38cf2d26a Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Tue, 27 May 2025 17:30:52 +0800 Subject: [PATCH 3/5] update test --- .../test/OutputLibraryVisitorTests.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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 143d5175980..359e2962d9d 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 @@ -198,10 +198,7 @@ public void VisitMethodToRenameParameterName() var updatedMethod = testMethod.Accept(new MethodVisitor()); Assert.AreEqual("newName", testMethod.Signature.Parameters.First().Name); - Assert.AreEqual("newName", updatedMethod?.Signature.Parameters.First().Name); - - var bodyStatements = updatedMethod?.BodyStatements!.ToDisplayString(); - Assert.AreEqual("return newName;\n", bodyStatements); + Assert.AreEqual("return newName;\n", testMethod?.BodyStatements!.ToDisplayString()); } private class MethodVisitor : LibraryVisitor From 66d4694a945c43515f1279c5c2bc3a45cc7da647 Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Tue, 27 May 2025 17:37:11 +0800 Subject: [PATCH 4/5] minor --- .../test/OutputLibraryVisitorTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 359e2962d9d..b6c156dfea5 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 @@ -195,7 +195,7 @@ public void VisitMethodToRenameParameterName() new MethodSignature("Test", $"", MethodSignatureModifiers.Public, null, $"", [parameter]), Snippet.Return(parameter), new TestTypeProvider()); - var updatedMethod = testMethod.Accept(new MethodVisitor()); + testMethod.Accept(new MethodVisitor()); Assert.AreEqual("newName", testMethod.Signature.Parameters.First().Name); Assert.AreEqual("return newName;\n", testMethod?.BodyStatements!.ToDisplayString()); From 022075ba28edf5837f44924b24e00dc46d001ae4 Mon Sep 17 00:00:00 2001 From: Wei Hu Date: Wed, 28 May 2025 09:29:06 +0800 Subject: [PATCH 5/5] update all properties for ParameterProvider --- .../src/Providers/ParameterProvider.cs | 104 ++++++++++++++++-- 1 file changed, 92 insertions(+), 12 deletions(-) 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 95bbc797bdd..4f17b987aa0 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 @@ -20,27 +20,27 @@ namespace Microsoft.TypeSpec.Generator.Providers public sealed class ParameterProvider : IEquatable { public string Name { get; private set; } - public FormattableString Description { get; } + 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. @@ -241,14 +241,94 @@ internal ParameterProvider WithRef() /// /// Updates the parameter with the given name. /// - /// Name to update. - public void Update(string? name = null) + 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; + } } } }