Skip to content

Commit

Permalink
Support customizing client accessibility (#4605)
Browse files Browse the repository at this point in the history
Fixes #4602
  • Loading branch information
JoshLove-msft authored Oct 4, 2024
1 parent 5e12678 commit 5145b90
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,7 @@ protected override PropertyProvider[] BuildProperties()

return [.. properties];
}

protected override TypeSignatureModifiers GetDeclarationModifiers() => GetCustomCodeModifiers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ protected override MethodProvider[] BuildMethods()
return [.. methods];
}

protected override TypeSignatureModifiers GetDeclarationModifiers() => GetCustomCodeModifiers();

private ParameterProvider BuildClientEndpointParameter()
{
var endpointParam = _inputClient.Parameters.FirstOrDefault(p => p.IsEndpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ protected override MethodProvider[] BuildMethods()
return [.. methods];
}

protected override TypeSignatureModifiers GetDeclarationModifiers() => GetCustomCodeModifiers();

private bool IsCreateRequest(MethodProvider method)
{
var span = method.Signature.Name.AsSpan();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ .. GetStackVariablesForReturnValueConversion(result, responseBodyType, isAsync,
}

var convenienceMethod = new ScmMethodProvider(methodSignature, methodBody, EnclosingType);
convenienceMethod.XmlDocs!.Exceptions.Add(new(typeof(ClientResultException), "Service returned a non-success status code.", []));
// XmlDocs will be null if the method isn't public
convenienceMethod.XmlDocs?.Exceptions.Add(new(typeof(ClientResultException), "Service returned a non-success status code.", []));
return convenienceMethod;
}

Expand Down Expand Up @@ -406,13 +407,19 @@ private MethodProvider BuildProtocolMethod(MethodProvider createRequestMethod, b

var protocolMethod =
new ScmMethodProvider(methodSignature, methodBody, EnclosingType) { IsServiceCall = true };
protocolMethod.XmlDocs!.Exceptions.Add(new(typeof(ClientResultException), "Service returned a non-success status code.", []));
List<XmlDocStatement> listItems =
[
new XmlDocStatement("item", [], new XmlDocStatement("description", [$"This <see href=\"https://aka.ms/azsdk/net/protocol-methods\">protocol method</see> allows explicit creation of the request and processing of the response for advanced scenarios."]))
];
XmlDocStatement listXmlDoc = new XmlDocStatement("<list type=\"bullet\">", "</list>", [], innerStatements: [.. listItems]);
protocolMethod.XmlDocs.Summary = new XmlDocSummaryStatement([$"[Protocol Method] {Operation.Description}"], listXmlDoc);

// XmlDocs will be null if the method isn't public
if (protocolMethod.XmlDocs != null)
{
protocolMethod.XmlDocs?.Exceptions.Add(
new(typeof(ClientResultException), "Service returned a non-success status code.", []));
List<XmlDocStatement> listItems =
[
new XmlDocStatement("item", [], new XmlDocStatement("description", [$"This <see href=\"https://aka.ms/azsdk/net/protocol-methods\">protocol method</see> allows explicit creation of the request and processing of the response for advanced scenarios."]))
];
XmlDocStatement listXmlDoc = new XmlDocStatement("<list type=\"bullet\">", "</list>", [], innerStatements: [.. listItems]);
protocolMethod.XmlDocs!.Summary = new XmlDocSummaryStatement([$"[Protocol Method] {Operation.Description}"], listXmlDoc);
}
return protocolMethod;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.ClientModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Tests.Common;
using NUnit.Framework;

Expand Down Expand Up @@ -192,5 +192,76 @@ public async Task CanReplaceStructMethod()
Assert.IsTrue(customMethodParams[0].Type.IsStruct);
Assert.IsTrue(customMethodParams[0].Type.IsNullable);
}

[Test]
public async Task CanChangeClientAccessibility()
{
var inputOperation = InputFactory.Operation("HelloAgain", parameters:
[
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String))
]);
var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]);
var plugin = await MockHelpers.LoadMockPluginAsync(
clients: () => [inputClient],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());

// Find the client provider
var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider);
Assert.IsNotNull(clientProvider);
Assert.IsTrue(clientProvider!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal));

// Find the REST client provider
var restClientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is RestClientProvider);
Assert.IsNotNull(restClientProvider);
Assert.IsTrue(restClientProvider!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal));

// Find the client options provider
var clientOptionsProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientOptionsProvider);
Assert.IsNotNull(clientOptionsProvider);
// The client options were not customized
Assert.IsTrue(clientOptionsProvider!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public));

// The docs should not be generated for the methods as the client is internal
foreach (var method in clientProvider.Methods)
{
Assert.IsNull(method.XmlDocs);
}
}

[Test]
public async Task CanChangeClientOptionsAccessibility()
{
var inputOperation = InputFactory.Operation("HelloAgain", parameters:
[
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String))
]);
var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]);
var plugin = await MockHelpers.LoadMockPluginAsync(
clients: () => [inputClient],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());

// Find the client provider - we customize both to be internal because otherwise the build would fail as the client options
// would be less accessible than the client
var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider);
Assert.IsNotNull(clientProvider);
Assert.IsTrue(clientProvider!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal));

// Find the REST client provider
var restClientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is RestClientProvider);
Assert.IsNotNull(restClientProvider);
Assert.IsTrue(restClientProvider!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal));

// Find the client options provider
var clientOptionsProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientOptionsProvider);
Assert.IsNotNull(clientOptionsProvider);
// The client options were not customized
Assert.IsTrue(clientOptionsProvider!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal));

// The docs should not be generated for the methods as the client is internal
foreach (var method in clientProvider.Methods)
{
Assert.IsNull(method.XmlDocs);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace Sample
{
/// <summary></summary>
internal partial class TestClient
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace Sample
{
/// <summary></summary>
internal partial class TestClient
{
}
/// <summary></summary>
internal partial class TestClientOptions
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
Expand All @@ -16,7 +13,7 @@ public partial class TestClient
{
public virtual ClientResult HelloAgain(BinaryContent content, RequestOptions options)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
Expand All @@ -16,7 +13,7 @@ public partial class TestClient
{
public virtual ClientResult HelloAgain(MyStruct? p1)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable

using Microsoft.Generator.CSharp.Customization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable

using Microsoft.Generator.CSharp.Customization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable

using Microsoft.Generator.CSharp.Customization;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable

using Microsoft.Generator.CSharp.Customization;
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public TypeSignatureModifiers DeclarationModifiers

protected virtual TypeSignatureModifiers GetDeclarationModifiers() => TypeSignatureModifiers.None;

internal TypeSignatureModifiers GetCustomCodeModifiers() => CustomCodeView?.DeclarationModifiers ?? TypeSignatureModifiers.None;
protected TypeSignatureModifiers GetCustomCodeModifiers() => CustomCodeView?.DeclarationModifiers ?? TypeSignatureModifiers.None;

private TypeSignatureModifiers GetDeclarationModifiersInternal()
{
Expand Down

0 comments on commit 5145b90

Please sign in to comment.