Skip to content

Commit 1e0684c

Browse files
Recompute the XmlDocs when method signature is updated (#7240)
Contributes to Azure/azure-sdk-for-net#49632
1 parent cb5f88d commit 1e0684c

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ConstructorProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ConstructorProvider(ConstructorSignature signature, MethodBodyStatement b
3737
{
3838
Signature = signature;
3939
bool skipParamValidation = !signature.Modifiers.HasFlag(MethodSignatureModifiers.Public);
40-
var paramHash = MethodProviderHelpers.GetParamhash(signature.Parameters, skipParamValidation);
40+
var paramHash = MethodProviderHelpers.GetParamHash(signature.Parameters, skipParamValidation);
4141
BodyStatements = MethodProviderHelpers.GetBodyStatementWithValidation(signature.Parameters, bodyStatements, paramHash);
4242
XmlDocs = xmlDocProvider ?? MethodProviderHelpers.BuildXmlDocs(signature.Parameters, signature.Description, null, paramHash);
4343
EnclosingType = enclosingType;

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/MethodProvider.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class MethodProvider
1515
public MethodSignature Signature { get; private set; }
1616
public MethodBodyStatement? BodyStatements { get; private set;}
1717
public ValueExpression? BodyExpression { get; private set;}
18-
public XmlDocProvider? XmlDocs { get; private set;}
18+
19+
public XmlDocProvider? XmlDocs => _xmlDocs ??= BuildXmlDocs();
20+
private XmlDocProvider? _xmlDocs;
1921

2022
public TypeProvider EnclosingType { get; }
2123

@@ -37,9 +39,9 @@ public MethodProvider(MethodSignature signature, MethodBodyStatement bodyStateme
3739
{
3840
Signature = signature;
3941
bool skipParamValidation = !signature.Modifiers.HasFlag(MethodSignatureModifiers.Public);
40-
var paramHash = MethodProviderHelpers.GetParamhash(signature.Parameters, skipParamValidation);
42+
var paramHash = MethodProviderHelpers.GetParamHash(signature.Parameters, skipParamValidation);
4143
BodyStatements = MethodProviderHelpers.GetBodyStatementWithValidation(signature.Parameters, bodyStatements, paramHash);
42-
XmlDocs = xmlDocProvider ?? MethodProviderHelpers.BuildXmlDocs(signature.Parameters, signature.Description, signature.ReturnDescription, paramHash);
44+
_xmlDocs = xmlDocProvider;
4345
EnclosingType = enclosingType;
4446
}
4547

@@ -54,10 +56,23 @@ public MethodProvider(MethodSignature signature, ValueExpression bodyExpression,
5456
{
5557
Signature = signature;
5658
BodyExpression = bodyExpression;
57-
XmlDocs = xmlDocProvider ?? MethodProviderHelpers.BuildXmlDocs(signature.Parameters, signature.Description, signature.ReturnDescription, null);
59+
_xmlDocs = xmlDocProvider;
5860
EnclosingType = enclosingType;
5961
}
6062

63+
private XmlDocProvider? BuildXmlDocs()
64+
{
65+
return MethodProviderHelpers.BuildXmlDocs(
66+
Signature.Parameters,
67+
Signature.Description,
68+
Signature.ReturnDescription,
69+
BodyStatements != null ?
70+
MethodProviderHelpers.GetParamHash(
71+
Signature.Parameters,
72+
!Signature.Modifiers.HasFlag(MethodSignatureModifiers.Public)) :
73+
null);
74+
}
75+
6176
public void Update(
6277
MethodSignature? signature = null,
6378
MethodBodyStatement? bodyStatements = null,
@@ -67,6 +82,8 @@ public void Update(
6782
if (signature != null)
6883
{
6984
Signature = signature;
85+
// rebuild the XML docs if the signature changes
86+
_xmlDocs = BuildXmlDocs();
7087
}
7188
if (bodyStatements != null)
7289
{
@@ -80,7 +97,7 @@ public void Update(
8097
}
8198
if (xmlDocProvider != null)
8299
{
83-
XmlDocs = xmlDocProvider;
100+
_xmlDocs = xmlDocProvider;
84101
}
85102
}
86103
}

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Utilities/MethodProviderHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.TypeSpec.Generator
1212
{
1313
internal static class MethodProviderHelpers
1414
{
15-
public static Dictionary<ParameterValidationType, List<ParameterProvider>>? GetParamhash(IEnumerable<ParameterProvider> parameters, bool skipParamValidation)
15+
public static Dictionary<ParameterValidationType, List<ParameterProvider>>? GetParamHash(IEnumerable<ParameterProvider> parameters, bool skipParamValidation)
1616
{
1717
Dictionary<ParameterValidationType, List<ParameterProvider>>? paramHash = null;
1818
if (!skipParamValidation)

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/MethodProviderTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,26 @@ public void TestUpdate()
6161
typeProvider.Update(methods: [methodProvider]);
6262

6363
// now update the method and check if the type provider reflects the change
64-
methodProvider.Update(new MethodSignature("Updated", $"", MethodSignatureModifiers.Public, null, $"", []));
64+
methodProvider.Update(new MethodSignature("Updated", $"", MethodSignatureModifiers.Public, null, $"", [new ParameterProvider("foo", $"Foo description", typeof(int))]));
6565
var updatedMethods = typeProvider.CanonicalView.Methods;
6666
Assert.IsNotNull(updatedMethods);
6767
Assert.AreEqual(1, updatedMethods!.Count);
6868

69+
// Validate that the method name is updated
6970
var updatedMethod = updatedMethods[0];
7071
Assert.AreEqual("Updated", updatedMethod.Signature.Name);
72+
73+
// Validate that the parameter description is updated
74+
var parameter = updatedMethod.Signature.Parameters[0];
75+
Assert.AreEqual("Foo description", parameter.Description.ToString());
76+
Assert.IsTrue(parameter.Type.Equals(typeof(int)));
77+
78+
// Validate that the xml docs are updated
79+
Assert.IsNotNull(updatedMethod.XmlDocs);
80+
var xmlDocParamStatement = updatedMethod.XmlDocs!.Parameters[0];
81+
Assert.IsNotNull(xmlDocParamStatement);
82+
Assert.AreEqual(parameter, xmlDocParamStatement.Parameter);
83+
Assert.AreEqual("/// <param name=\"foo\"> Foo description. </param>\n", xmlDocParamStatement.ToDisplayString());
7184
}
7285

7386
private class TestTypeProvider : TypeProvider

0 commit comments

Comments
 (0)