-
Notifications
You must be signed in to change notification settings - Fork 294
Refactor XmlDocProvider
#7904
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
Open
ArcturusZhang
wants to merge
17
commits into
microsoft:main
Choose a base branch
from
ArcturusZhang:refactor-xml-doc-to-lazy-load
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor XmlDocProvider
#7904
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
eb58a10
initial commit, it will not build
ArcturusZhang 8beb435
make the exception statement to defer its calculation
ArcturusZhang 7869f42
revert some too aggressive changes
ArcturusZhang 9dc5f16
some updates
ArcturusZhang b217788
a few updates on test cases
ArcturusZhang 1e97044
it turns out the xmldocprovidertests must be written in the scm gener…
ArcturusZhang 104272d
Merge remote-tracking branch 'origin/main' into refactor-xml-doc-to-l…
ArcturusZhang de306a0
make the test working properly
ArcturusZhang 6687b15
fix test cases
ArcturusZhang f30be10
switch to a less aggressive way once again
ArcturusZhang e59bd57
add some convenience stuff
ArcturusZhang a970463
fix part of the issue
ArcturusZhang 9941601
fix the issues
ArcturusZhang 8ba71e2
Merge branch 'main' into refactor-xml-doc-to-lazy-load
ArcturusZhang 351a482
change the scenario to be more realistic
ArcturusZhang 77d8bc7
Merge branch 'main' into refactor-xml-doc-to-lazy-load
ArcturusZhang 935f54f
Merge branch 'main' into refactor-xml-doc-to-lazy-load
ArcturusZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
76 changes: 76 additions & 0 deletions
76
...del/test/Providers/TestData/XmlDocProviderTests/ValidateXmlDocShouldChangeFromVisitors.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.ClientModel; | ||
using System.ClientModel.Primitives; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Sample | ||
{ | ||
/// <summary> TestClient description. </summary> | ||
public partial class TestClient | ||
{ | ||
/// <summary> | ||
/// [Protocol Method] Operation description | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <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. </description> | ||
/// </item> | ||
/// </list> | ||
/// </summary> | ||
/// <param name="queryParam_modified"> queryParam description. </param> | ||
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> | ||
/// <exception cref="global::System.ArgumentNullException"> <paramref name="queryParam_modified"/> is null. </exception> | ||
/// <exception cref="global::System.ArgumentException"> <paramref name="queryParam_modified"/> is an empty string, and was expected to be non-empty. </exception> | ||
/// <exception cref="global::System.ClientModel.ClientResultException"> Service returned a non-success status code. </exception> | ||
/// <returns> The response returned from the service. </returns> | ||
public virtual global::System.ClientModel.ClientResult Operation(string queryParam_modified, global::System.ClientModel.Primitives.RequestOptions options) | ||
{ | ||
global::Sample.Argument.AssertNotNullOrEmpty(queryParam_modified, nameof(queryParam_modified)); | ||
|
||
using global::System.ClientModel.Primitives.PipelineMessage message = this.CreateOperationRequest(queryParam_modified, options); | ||
return global::System.ClientModel.ClientResult.FromResponse(Pipeline.ProcessMessage(message, options)); | ||
} | ||
|
||
/// <summary> | ||
/// [Protocol Method] Operation description | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <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. </description> | ||
/// </item> | ||
/// </list> | ||
/// </summary> | ||
/// <param name="queryParam_modified"> queryParam description. </param> | ||
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> | ||
/// <exception cref="global::System.ArgumentNullException"> <paramref name="queryParam_modified"/> is null. </exception> | ||
/// <exception cref="global::System.ArgumentException"> <paramref name="queryParam_modified"/> is an empty string, and was expected to be non-empty. </exception> | ||
/// <exception cref="global::System.ClientModel.ClientResultException"> Service returned a non-success status code. </exception> | ||
/// <returns> The response returned from the service. </returns> | ||
public virtual async global::System.Threading.Tasks.Task<global::System.ClientModel.ClientResult> OperationAsync(string queryParam_modified, global::System.ClientModel.Primitives.RequestOptions options) | ||
{ | ||
global::Sample.Argument.AssertNotNullOrEmpty(queryParam_modified, nameof(queryParam_modified)); | ||
|
||
using global::System.ClientModel.Primitives.PipelineMessage message = this.CreateOperationRequest(queryParam_modified, options); | ||
return global::System.ClientModel.ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); | ||
} | ||
|
||
/// <summary> Operation description. </summary> | ||
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param> | ||
/// <exception cref="global::System.ClientModel.ClientResultException"> Service returned a non-success status code. </exception> | ||
public virtual global::System.ClientModel.ClientResult Operation(global::System.Threading.CancellationToken cancellationToken = default) | ||
{ | ||
return this.Operation(cancellationToken.CanBeCanceled ? new global::System.ClientModel.Primitives.RequestOptions { CancellationToken = cancellationToken } : null); | ||
} | ||
|
||
/// <summary> Operation description. </summary> | ||
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param> | ||
/// <exception cref="global::System.ClientModel.ClientResultException"> Service returned a non-success status code. </exception> | ||
public virtual async global::System.Threading.Tasks.Task<global::System.ClientModel.ClientResult> OperationAsync(global::System.Threading.CancellationToken cancellationToken = default) | ||
{ | ||
return await this.OperationAsync(cancellationToken.CanBeCanceled ? new global::System.ClientModel.Primitives.RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false); | ||
} | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
.../generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/XmlDocProviderTests.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Linq; | ||
using Microsoft.TypeSpec.Generator.ClientModel; | ||
using Microsoft.TypeSpec.Generator.ClientModel.Providers; | ||
using Microsoft.TypeSpec.Generator.ClientModel.Tests; | ||
using Microsoft.TypeSpec.Generator.Input; | ||
using Microsoft.TypeSpec.Generator.Providers; | ||
using Microsoft.TypeSpec.Generator.Tests.Common; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.TypeSpec.Generator.Tests.Providers | ||
{ | ||
public class XmlDocProviderTests | ||
{ | ||
private const string TestClientName = "TestClient"; | ||
private static readonly InputClient _testClient = InputFactory.Client(TestClientName, | ||
methods: [ | ||
InputFactory.BasicServiceMethod( | ||
"Operation", | ||
InputFactory.Operation( | ||
"Operation", | ||
parameters: | ||
[ | ||
InputFactory.Parameter( | ||
"queryParam", | ||
InputPrimitiveType.String, | ||
isRequired: true, | ||
location: InputRequestLocation.Query) | ||
]))]); | ||
|
||
[Test] | ||
public void ValidateXmlDocShouldChangeFromVisitors() | ||
{ | ||
MockHelpers.LoadMockGenerator( | ||
createClientCore: inputClient => new MockClientProvider(inputClient), | ||
clients: () => [_testClient], | ||
includeXmlDocs: true | ||
); | ||
var testVisitor = new TestVisitor(); | ||
ScmCodeModelGenerator.Instance.AddVisitor(testVisitor); | ||
|
||
// visit the library | ||
testVisitor.DoVisitLibrary(CodeModelGenerator.Instance.OutputLibrary); | ||
|
||
// check if the parameter names in xml docs are changed accordingly | ||
// find the client in outputlibrary | ||
var client = ScmCodeModelGenerator.Instance.OutputLibrary.TypeProviders.OfType<ClientProvider>().FirstOrDefault()!; | ||
Assert.IsNotNull(client); | ||
var writer = ScmCodeModelGenerator.Instance.GetWriter(client); | ||
var file = writer.Write(); | ||
|
||
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content); | ||
} | ||
|
||
private class TestVisitor : ScmLibraryVisitor | ||
{ | ||
public void DoVisitLibrary(OutputLibrary library) | ||
{ | ||
VisitLibrary(library); | ||
} | ||
|
||
protected internal override ScmMethodProvider? VisitMethod(ScmMethodProvider method) | ||
{ | ||
// modify the parameter names in-place | ||
foreach (var parameter in method.Signature.Parameters) | ||
{ | ||
if (parameter.Name == "queryParam") | ||
{ | ||
// modify the parameter name | ||
parameter.Update(name: "queryParam_modified"); | ||
} | ||
} | ||
return method; | ||
} | ||
} | ||
|
||
private class MockClientProvider : ClientProvider | ||
{ | ||
public MockClientProvider(InputClient client) : base(client) | ||
{ } | ||
|
||
// ignore all the ctors to make the output more clear | ||
protected override ConstructorProvider[] BuildConstructors() => []; | ||
|
||
// ignore all the fields to make the output more clear | ||
protected override FieldProvider[] BuildFields() => []; | ||
|
||
protected override PropertyProvider[] BuildProperties() => []; | ||
} | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.TypeSpec.Generator.Expressions; | ||
using Microsoft.TypeSpec.Generator.Input; | ||
using Microsoft.TypeSpec.Generator.Input.Extensions; | ||
|
@@ -158,13 +159,7 @@ public bool Equals(ParameterProvider? y) | |
|
||
public override int GetHashCode() | ||
{ | ||
return GetHashCode(this); | ||
} | ||
|
||
private int GetHashCode([DisallowNull] ParameterProvider obj) | ||
{ | ||
// remove type as part of the hash code generation as the type might have changes between versions | ||
return HashCode.Combine(obj.Name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i change this because the
|
||
return RuntimeHelpers.GetHashCode(this); // gets the hash code based on object reference | ||
} | ||
|
||
private string GetDebuggerDisplay() | ||
|
@@ -333,5 +328,27 @@ public void Update( | |
Validation = validation.Value; | ||
} | ||
} | ||
|
||
internal static IEqualityComparer<ParameterProvider> EqualityByNameAndType = new ParameterProviderEqualityComparer(); | ||
|
||
private struct ParameterProviderEqualityComparer : IEqualityComparer<ParameterProvider> | ||
{ | ||
public bool Equals(ParameterProvider? x, ParameterProvider? y) | ||
{ | ||
if (ReferenceEquals(x, y)) | ||
{ | ||
return true; | ||
} | ||
if (x is null || y is null) | ||
{ | ||
return false; | ||
} | ||
return x.Equals(y); | ||
} | ||
public int GetHashCode([DisallowNull] ParameterProvider obj) | ||
{ | ||
return HashCode.Combine(obj.Name); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since in this context, we are trying to compare the parameters by their names and types, we used a comparer here to do that, and its default behavior in a dictionary is by reference equality.