Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip empty client generation #5218

Merged
merged 12 commits into from
Dec 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected override FieldProvider[] BuildFields()
// add sub-client caching fields
foreach (var subClient in SubClients)
{
if (subClient.Value._clientCachingField != null)
if (subClient.Value.Methods.Count != 0 && subClient.Value._clientCachingField != null)
live1206 marked this conversation as resolved.
Show resolved Hide resolved
{
fields.Add(subClient.Value._clientCachingField);
}
Expand Down Expand Up @@ -479,7 +479,7 @@ protected override MethodProvider[] BuildMethods()
foreach (var subClient in SubClients)
{
var subClientInstance = subClient.Value;
if (subClientInstance._clientCachingField is null)
if (subClientInstance._clientCachingField is null || subClientInstance.Methods.Count == 0)
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ private static TypeProvider[] BuildClients()
foreach (var inputClient in inputClients)
{
var client = ClientModelPlugin.Instance.TypeFactory.CreateClient(inputClient);
if (client.Methods.Count == 0)
ArcturusZhang marked this conversation as resolved.
Show resolved Hide resolved
{
continue;
}
clients.Add(client);
clients.Add(client.RestClient);
var clientOptions = client.ClientOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public async Task CanRenameSubClient()
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String))
]);
var inputClient = InputFactory.Client("TestClient", operations: [inputOperation]);
InputClient subClient = InputFactory.Client("custom", [], [], inputClient.Name);
InputClient subClient = InputFactory.Client("custom", [inputOperation], [], inputClient.Name);
var plugin = await MockHelpers.LoadMockPluginAsync(
clients: () => [inputClient, subClient],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.ClientProviders
public class ClientProviderSubClientTests
{
private const string TestClientName = "TestClient";
private static readonly InputClient _animalClient = new("animal", string.Empty, "AnimalClient description", [], [], TestClientName);
private static readonly InputClient _dogClient = new("dog", string.Empty, "DogClient description", [], [], _animalClient.Name);
private static readonly InputClient _catClient = new("cat", string.Empty, "CatClient description", [], [], _animalClient.Name);
private static readonly InputClient _hawkClient = new("hawkClient", string.Empty, "HawkClient description", [], [], _animalClient.Name);
private static readonly InputClient _huskyClient = new("husky", string.Empty, "HuskyClient description", [], [], _dogClient.Name);
private static readonly InputOperation _inputOperation = InputFactory.Operation("HelloAgain", parameters:
[
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String))
]);
private static readonly InputClient _animalClient = new("animal", string.Empty, "AnimalClient description", [_inputOperation], [], TestClientName);
private static readonly InputClient _dogClient = new("dog", string.Empty, "DogClient description", [_inputOperation], [], _animalClient.Name);
private static readonly InputClient _catClient = new("cat", string.Empty, "CatClient description", [_inputOperation], [], _animalClient.Name);
private static readonly InputClient _hawkClient = new("hawkClient", string.Empty, "HawkClient description", [_inputOperation], [], _animalClient.Name);
private static readonly InputClient _huskyClient = new("husky", string.Empty, "HuskyClient description", [_inputOperation], [], _dogClient.Name);

[SetUp]
public void SetUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Input;
Expand All @@ -26,9 +27,13 @@ public class ClientProviderTests
private const string KeyAuthCategory = "WithKeyAuth";
private const string OAuth2Category = "WithOAuth2";
private const string TestClientName = "TestClient";
private static readonly InputClient _animalClient = new("animal", "", "AnimalClient description", [], [], TestClientName);
private static readonly InputClient _dogClient = new("dog", "", "DogClient description", [], [], _animalClient.Name);
private static readonly InputClient _huskyClient = new("husky", "", "HuskyClient description", [], [], _dogClient.Name);
private static readonly InputOperation _inputOperation = InputFactory.Operation("HelloAgain", parameters:
[
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String))
]);
private static readonly InputClient _animalClient = new("animal", "", "AnimalClient description", [_inputOperation], [], TestClientName);
private static readonly InputClient _dogClient = new("dog", "", "DogClient description", [_inputOperation], [], _animalClient.Name);
private static readonly InputClient _huskyClient = new("husky", "", "HuskyClient description", [_inputOperation], [], _dogClient.Name);
private static readonly InputModelType _spreadModel = InputFactory.Model(
"spreadModel",
usage: InputModelTypeUsage.Spread,
Expand Down Expand Up @@ -63,6 +68,47 @@ public void SetUp()
clientPipelineApi: TestClientPipelineApi.Instance);
}

[Test]
public async Task TestEmptyClient()
{
var client = InputFactory.Client(TestClientName);
var plugin = await MockHelpers.LoadMockPluginAsync(
clients: () => [client]);

var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider && t.Name == TestClientName);
Assert.IsNull(clientProvider);
}

[Test]
public async Task TestNonEmptySubClient()
{
var client = InputFactory.Client(TestClientName);
var subClient = InputFactory.Client($"Sub{TestClientName}", [_inputOperation], [], client.Name);
var plugin = await MockHelpers.LoadMockPluginAsync(
clients: () => [client, subClient]);

var subClientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider && t.Name == subClient.Name);
Assert.IsNotNull(subClientProvider);

var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider && t.Name == TestClientName);
Assert.IsNotNull(clientProvider);
}

[Test]
public async Task TestEmptySubClient()
{
var client = InputFactory.Client(TestClientName);
var subClient = InputFactory.Client($"Sub{TestClientName}", [], [], client.Name);
var plugin = await MockHelpers.LoadMockPluginAsync(
clients: () => [client, subClient]);

var subClientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider && t.Name == subClient.Name);
Assert.IsNull(subClientProvider);

var clientProvider = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ClientProvider && t.Name == TestClientName);
Assert.IsNull(clientProvider);
}

[Test]
public void TestBuildProperties()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 All @@ -16,7 +17,11 @@ public class RestClientProviderCustomizationTests
[Test]
public async Task CanChangeClientNamespace()
{
var inputClient = InputFactory.Client("TestClient");
var inputOperation = InputFactory.Operation("HelloAgain", parameters:
[
InputFactory.Parameter("p1", InputFactory.Array(InputPrimitiveType.String))
]);
var inputClient = InputFactory.Client("TestClient", [inputOperation]);
var plugin = await MockHelpers.LoadMockPluginAsync(
clients: () => [inputClient],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@

#nullable disable

using System.ClientModel;
using System.ClientModel.Primitives;
using Sample;

namespace Sample.Custom
{
/// <summary></summary>
public partial class TestClient
{
private static global::System.ClientModel.Primitives.PipelineMessageClassifier _pipelineMessageClassifier200;

private static global::System.ClientModel.Primitives.PipelineMessageClassifier PipelineMessageClassifier200 => _pipelineMessageClassifier200 = global::System.ClientModel.Primitives.PipelineMessageClassifier.Create(stackalloc ushort[] { 200 });

internal global::System.ClientModel.Primitives.PipelineMessage CreateHelloAgainRequest(global::System.ClientModel.BinaryContent content, global::System.ClientModel.Primitives.RequestOptions options)
{
global::System.ClientModel.Primitives.PipelineMessage message = Pipeline.CreateMessage();
message.ResponseClassifier = PipelineMessageClassifier200;
global::System.ClientModel.Primitives.PipelineRequest request = message.Request;
request.Method = "GET";
global::Sample.ClientUriBuilder uri = new global::Sample.ClientUriBuilder();
uri.Reset(_endpoint);
request.Uri = uri.ToUri();
request.Content = content;
message.Apply(options);
return message;
}
}
}
Loading