Skip to content

Commit eb61c68

Browse files
Rename List methods to Get (#7588)
Fixes #7587
1 parent 93a22ee commit eb61c68

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/ScmMethodProviderCollection.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public ScmMethodProviderCollection(InputServiceMethod serviceMethod, TypeProvide
6363
{
6464
ServiceMethod = serviceMethod;
6565
EnclosingType = enclosingType;
66-
_cleanOperationName = serviceMethod.Operation.Name.ToIdentifierName();
66+
_cleanOperationName = GetCleanOperationName(serviceMethod);
67+
6768
Client = enclosingType as ClientProvider ?? throw new InvalidOperationException("Scm methods can only be built for client types.");
6869
_createRequestMethod = Client.RestClient.GetCreateRequestMethod(ServiceMethod.Operation);
6970

@@ -73,6 +74,17 @@ public ScmMethodProviderCollection(InputServiceMethod serviceMethod, TypeProvide
7374
}
7475
}
7576

77+
private static string GetCleanOperationName(InputServiceMethod serviceMethod)
78+
{
79+
var operationName = serviceMethod.Operation.Name.ToIdentifierName();
80+
// Replace List with Get as .NET convention is to use Get for list operations.
81+
if (operationName.StartsWith("List", StringComparison.Ordinal))
82+
{
83+
operationName = $"Get{operationName.Substring(4)}";
84+
}
85+
return operationName;
86+
}
87+
7688
protected virtual IReadOnlyList<ScmMethodProvider> BuildMethods()
7789
{
7890
var syncProtocol = BuildProtocolMethod(_createRequestMethod, false);

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/ScmMethodProviderCollectionTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,35 @@ public void RequestBodyConstructedRespectingRequestContentApi(InputType inputTyp
459459
}
460460
}
461461

462+
[Test]
463+
public void ListMethodIsRenamedToGet()
464+
{
465+
MockHelpers.LoadMockGenerator(requestContentApi: TestRequestContentApi.Instance);
466+
467+
var inputOperation = InputFactory.Operation(
468+
"ListCats");
469+
470+
var inputServiceMethod = InputFactory.BasicServiceMethod("ListCats", inputOperation);
471+
472+
var inputClient = InputFactory.Client("TestClient", methods: [inputServiceMethod]);
473+
474+
var client = ScmCodeModelGenerator.Instance.TypeFactory.CreateClient(inputClient);
475+
476+
var methodCollection = new ScmMethodProviderCollection(inputServiceMethod, client!);
477+
Assert.IsNotNull(methodCollection);
478+
foreach (var method in methodCollection)
479+
{
480+
if (method.Signature.Modifiers.HasFlag(MethodSignatureModifiers.Async))
481+
{
482+
Assert.AreEqual("GetCatsAsync", method.Signature.Name);
483+
}
484+
else
485+
{
486+
Assert.AreEqual("GetCats", method.Signature.Name);
487+
}
488+
}
489+
}
490+
462491
public static IEnumerable<TestCaseData> DefaultCSharpMethodCollectionTestCases
463492
{
464493
get

packages/http-client-csharp/generator/TestProjects/Local/Sample-TypeSpec/src/Generated/SampleTypeSpecClient.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ public virtual async Task<ClientResult> WithApiVersionAsync(string p1, Cancellat
12841284
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
12851285
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
12861286
/// <returns> The response returned from the service. </returns>
1287-
public virtual CollectionResult ListWithNextLink(RequestOptions options)
1287+
public virtual CollectionResult GetWithNextLink(RequestOptions options)
12881288
{
12891289
return new SampleTypeSpecClientListWithNextLinkCollectionResult(this, null, options);
12901290
}
@@ -1300,23 +1300,23 @@ public virtual CollectionResult ListWithNextLink(RequestOptions options)
13001300
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
13011301
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
13021302
/// <returns> The response returned from the service. </returns>
1303-
public virtual AsyncCollectionResult ListWithNextLinkAsync(RequestOptions options)
1303+
public virtual AsyncCollectionResult GetWithNextLinkAsync(RequestOptions options)
13041304
{
13051305
return new SampleTypeSpecClientListWithNextLinkAsyncCollectionResult(this, null, options);
13061306
}
13071307

13081308
/// <summary> List things with nextlink. </summary>
13091309
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
13101310
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1311-
public virtual CollectionResult<Thing> ListWithNextLink(CancellationToken cancellationToken = default)
1311+
public virtual CollectionResult<Thing> GetWithNextLink(CancellationToken cancellationToken = default)
13121312
{
13131313
return new SampleTypeSpecClientListWithNextLinkCollectionResultOfT(this, null, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
13141314
}
13151315

13161316
/// <summary> List things with nextlink. </summary>
13171317
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
13181318
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1319-
public virtual AsyncCollectionResult<Thing> ListWithNextLinkAsync(CancellationToken cancellationToken = default)
1319+
public virtual AsyncCollectionResult<Thing> GetWithNextLinkAsync(CancellationToken cancellationToken = default)
13201320
{
13211321
return new SampleTypeSpecClientListWithNextLinkAsyncCollectionResultOfT(this, null, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
13221322
}
@@ -1333,7 +1333,7 @@ public virtual AsyncCollectionResult<Thing> ListWithNextLinkAsync(CancellationTo
13331333
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
13341334
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
13351335
/// <returns> The response returned from the service. </returns>
1336-
public virtual CollectionResult ListWithContinuationToken(string token, RequestOptions options)
1336+
public virtual CollectionResult GetWithContinuationToken(string token, RequestOptions options)
13371337
{
13381338
return new SampleTypeSpecClientListWithContinuationTokenCollectionResult(this, token, options);
13391339
}
@@ -1350,7 +1350,7 @@ public virtual CollectionResult ListWithContinuationToken(string token, RequestO
13501350
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
13511351
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
13521352
/// <returns> The response returned from the service. </returns>
1353-
public virtual AsyncCollectionResult ListWithContinuationTokenAsync(string token, RequestOptions options)
1353+
public virtual AsyncCollectionResult GetWithContinuationTokenAsync(string token, RequestOptions options)
13541354
{
13551355
return new SampleTypeSpecClientListWithContinuationTokenAsyncCollectionResult(this, token, options);
13561356
}
@@ -1359,7 +1359,7 @@ public virtual AsyncCollectionResult ListWithContinuationTokenAsync(string token
13591359
/// <param name="token"></param>
13601360
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
13611361
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1362-
public virtual CollectionResult<Thing> ListWithContinuationToken(string token = default, CancellationToken cancellationToken = default)
1362+
public virtual CollectionResult<Thing> GetWithContinuationToken(string token = default, CancellationToken cancellationToken = default)
13631363
{
13641364
return new SampleTypeSpecClientListWithContinuationTokenCollectionResultOfT(this, token, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
13651365
}
@@ -1368,7 +1368,7 @@ public virtual CollectionResult<Thing> ListWithContinuationToken(string token =
13681368
/// <param name="token"></param>
13691369
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
13701370
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1371-
public virtual AsyncCollectionResult<Thing> ListWithContinuationTokenAsync(string token = default, CancellationToken cancellationToken = default)
1371+
public virtual AsyncCollectionResult<Thing> GetWithContinuationTokenAsync(string token = default, CancellationToken cancellationToken = default)
13721372
{
13731373
return new SampleTypeSpecClientListWithContinuationTokenAsyncCollectionResultOfT(this, token, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
13741374
}
@@ -1385,7 +1385,7 @@ public virtual AsyncCollectionResult<Thing> ListWithContinuationTokenAsync(strin
13851385
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
13861386
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
13871387
/// <returns> The response returned from the service. </returns>
1388-
public virtual CollectionResult ListWithContinuationTokenHeaderResponse(string token, RequestOptions options)
1388+
public virtual CollectionResult GetWithContinuationTokenHeaderResponse(string token, RequestOptions options)
13891389
{
13901390
return new SampleTypeSpecClientListWithContinuationTokenHeaderResponseCollectionResult(this, token, options);
13911391
}
@@ -1402,7 +1402,7 @@ public virtual CollectionResult ListWithContinuationTokenHeaderResponse(string t
14021402
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
14031403
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
14041404
/// <returns> The response returned from the service. </returns>
1405-
public virtual AsyncCollectionResult ListWithContinuationTokenHeaderResponseAsync(string token, RequestOptions options)
1405+
public virtual AsyncCollectionResult GetWithContinuationTokenHeaderResponseAsync(string token, RequestOptions options)
14061406
{
14071407
return new SampleTypeSpecClientListWithContinuationTokenHeaderResponseAsyncCollectionResult(this, token, options);
14081408
}
@@ -1411,7 +1411,7 @@ public virtual AsyncCollectionResult ListWithContinuationTokenHeaderResponseAsyn
14111411
/// <param name="token"></param>
14121412
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
14131413
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1414-
public virtual CollectionResult<Thing> ListWithContinuationTokenHeaderResponse(string token = default, CancellationToken cancellationToken = default)
1414+
public virtual CollectionResult<Thing> GetWithContinuationTokenHeaderResponse(string token = default, CancellationToken cancellationToken = default)
14151415
{
14161416
return new SampleTypeSpecClientListWithContinuationTokenHeaderResponseCollectionResultOfT(this, token, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
14171417
}
@@ -1420,7 +1420,7 @@ public virtual CollectionResult<Thing> ListWithContinuationTokenHeaderResponse(s
14201420
/// <param name="token"></param>
14211421
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
14221422
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1423-
public virtual AsyncCollectionResult<Thing> ListWithContinuationTokenHeaderResponseAsync(string token = default, CancellationToken cancellationToken = default)
1423+
public virtual AsyncCollectionResult<Thing> GetWithContinuationTokenHeaderResponseAsync(string token = default, CancellationToken cancellationToken = default)
14241424
{
14251425
return new SampleTypeSpecClientListWithContinuationTokenHeaderResponseAsyncCollectionResultOfT(this, token, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
14261426
}
@@ -1436,7 +1436,7 @@ public virtual AsyncCollectionResult<Thing> ListWithContinuationTokenHeaderRespo
14361436
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
14371437
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
14381438
/// <returns> The response returned from the service. </returns>
1439-
public virtual CollectionResult ListWithPaging(RequestOptions options)
1439+
public virtual CollectionResult GetWithPaging(RequestOptions options)
14401440
{
14411441
return new SampleTypeSpecClientListWithPagingCollectionResult(this, options);
14421442
}
@@ -1452,23 +1452,23 @@ public virtual CollectionResult ListWithPaging(RequestOptions options)
14521452
/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
14531453
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
14541454
/// <returns> The response returned from the service. </returns>
1455-
public virtual AsyncCollectionResult ListWithPagingAsync(RequestOptions options)
1455+
public virtual AsyncCollectionResult GetWithPagingAsync(RequestOptions options)
14561456
{
14571457
return new SampleTypeSpecClientListWithPagingAsyncCollectionResult(this, options);
14581458
}
14591459

14601460
/// <summary> List things with paging. </summary>
14611461
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
14621462
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1463-
public virtual CollectionResult<Thing> ListWithPaging(CancellationToken cancellationToken = default)
1463+
public virtual CollectionResult<Thing> GetWithPaging(CancellationToken cancellationToken = default)
14641464
{
14651465
return new SampleTypeSpecClientListWithPagingCollectionResultOfT(this, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
14661466
}
14671467

14681468
/// <summary> List things with paging. </summary>
14691469
/// <param name="cancellationToken"> The cancellation token that can be used to cancel the operation. </param>
14701470
/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
1471-
public virtual AsyncCollectionResult<Thing> ListWithPagingAsync(CancellationToken cancellationToken = default)
1471+
public virtual AsyncCollectionResult<Thing> GetWithPagingAsync(CancellationToken cancellationToken = default)
14721472
{
14731473
return new SampleTypeSpecClientListWithPagingAsyncCollectionResultOfT(this, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
14741474
}

0 commit comments

Comments
 (0)