Make extension clients public to enable mocking for extension methods#3483
Merged
ArcturusZhang merged 68 commits intoAzure:feature/v3from Nov 2, 2023
Merged
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
6 tasks
…lic and regenerate
93168fe to
54aef20
Compare
… specified generate-arm-resource-extension
Member
Author
|
@m-nash this PR is ready to review. I have an idea that since we need to collection feedback for some customers, we might cannot merge this PR in a short time. In summary, my plan is to take two steps:
|
This comment was marked as resolved.
This comment was marked as resolved.
ArthurMa1978
approved these changes
Oct 31, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3493
Description
This is a simplified version of #3175This PR enables a general way for our customer to mock the extension methods.
In general, to mock an extension method in out .Net mgmt SDK takes several unified steps of mocking:
extendeebelow), follow the patternMockable{RPName}{ExtendeeName}, you will get a type which contains the real method to mock (referred asextension classbelow). For instance if the extendee isResourceGroupResourceinAzure.ResourceManager.Compute, you will getMockableComputeResourceGroupResource.extension classwith the same parameter list as the extension method you would like to mock. For instance, to mock the extension methodpublic static Response DoSomething(this ResourceGroupResource resourceGroup, string someParameter, CancellationToken cancellationToken), you should mock the methodpublic virtual Response DoSomething(string someParameter, CancellationToken cancellationToken)on theextension classMockableComputeResourceGroupResource.GetCachedClienton the extendee to make it return the above mockedextension class.By following the above three steps, your mock now should work in the same way that you usually call it.
This PR unifies the previous internal implementation to ensure we have the same pattern everywhere so that our customer gets a unified experience of mocking the extension methods.
This PR also tries to remove all the validations in the public extension class to make mocking more feasible. For instance, previously we have the code:
This will cause the mocking invocation has to pass in a non-null argument as well - but when we are mocking those instance method, since the implementation of the body has been completely replaced, we never have this issue.
Therefore this PR removes all these validations in the static class. But we still enforces those parameter to be non-null or non-empty because the same check will exist in the corresponding Mocking Extensions.
A follow up issue of this is to make a test or analyzer rule to enforce the pattern in all of our SDKs including the generated code and customized code. Because changing the internal implementation here will change the way of mocking things, changing it would be regarded as a behavioral breaking change and we should never change that once the
extension classesare public.Checklist
To ensure a quick review and merge, please ensure:
Ready to Land?