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 clients #5652

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

live1206
Copy link
Contributor

@live1206 live1206 commented Jan 17, 2025

Based on the e2e experiment in #5651, the user can implement an empty client in TypeSpec and MGC still generates it.
So, this is not a specific case for Azure plugin only, and then we should skip the empty client generation in MGC as a general behavior.

The criteria of an empty client:

  • There is no methods within clientProvider and its custom code
  • There is no methods in the corresponding sub-clients and its custom code

Resolves #5624

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jan 17, 2025
@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

}
}

if (client.CustomCodeView?.Methods.Count > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for the Methods isn't enough because the custom code may be using Properties, Fields, or even just doing something in the constructor. We should simply check whether there is custom code or not, and not worry about the individual members.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, it seems like custom code has a different criteria that if there is any custom code exists we should not skip the client?
Because for client and sub-client, we only check the methods.


foreach (var subclient in client.SubClients)
{
if (subclient.Methods.Count > 0)
Copy link
Contributor

@JoshLove-msft JoshLove-msft Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for the Methods for the subclient isn't enough - we either need to check for the operations on the input client or the methods on the client and its REST client. I think adding an internal property like HasOperations or HasMethods to ClientProvider should be added to encapsulate this logic.

Copy link
Contributor Author

@live1206 live1206 Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial thought is that, we should have the same criteria for client, sub-client and custom code, which is any of them should have some methods.

For clientProvider, if there is no methods which means there is no operation or helper methods. Given RestClientProvider only contains methods of CreateRequestMethod, we should not need to check RestClientProvider.

Same thing for subClient, which is also a clientProvider.

For custom code, I don't understand why user would like to have a custom property or field to hold the empty client.
Do you have a use case for this?

{
if (subclient.Methods.Count > 0)
{
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to also check to see if the subclients have custom code defined.

Copy link
Contributor Author

@live1206 live1206 Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the check of custom code but only check methods of custom code for now.

@@ -63,6 +67,54 @@ public void SetUp()
clientPipelineApi: TestClientPipelineApi.Instance);
}

[Test]
public void TestEmptyClient()
Copy link
Contributor

@JoshLove-msft JoshLove-msft Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test that validates the behavior when custom code is added for the client and for a sub client. See https://github.com/microsoft/typespec/blob/main/packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Providers/ClientProviders/ClientProviderCustomizationTests.cs for examples of custom code tests. Probably the custom code tests should go in that file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests, but only for custom code with methods for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rescope empty client suppression
3 participants