Skip to content

Commit 7923b7b

Browse files
authored
fix the default API version in rest client issue. (Azure#51827)
* fix the default api version in rest client issue. * fix unit tests. * remove debug code commited by accident. * address review comments. * refine. * fix ut.
1 parent 53dfe4e commit 7923b7b

File tree

16 files changed

+22
-18
lines changed

16 files changed

+22
-18
lines changed

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/ManagementInputLibrary.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ private InputNamespace BuildInputNamespaceInternal()
7474

7575
private IReadOnlyDictionary<InputModelType, string> ResourceUpdateModelToResourceNameMap => _resourceUpdateModelToResourceNameMap ??= BuildResourceUpdateModelToResourceNameMap();
7676

77+
// If there're multiple API versions in the input namespace, use the last one as the default.
78+
internal string DefaultApiVersion => InputNamespace.ApiVersions.Last();
79+
7780
private IReadOnlyDictionary<InputModelType, string> BuildResourceUpdateModelToResourceNameMap()
7881
{
7982
Dictionary<InputModelType, string> map = new();

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Providers/MockableResourceProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ private static Dictionary<InputClient, RestClientInfo> BuildRestClientInfos(IRea
8585
ResourceHelpers.GetRestClientPropertyName(restClientProvider.Name),
8686
new ExpressionPropertyBody(
8787
restClientField.Assign(
88-
// TODO -- need to add apiVersion here in the future https://github.com/Azure/azure-sdk-for-net/issues/51791
89-
New.Instance(restClientProvider.Type, clientDiagnosticsProperty, thisResource.Pipeline(), thisResource.Endpoint(), Null),
88+
New.Instance(restClientProvider.Type, clientDiagnosticsProperty, thisResource.Pipeline(), thisResource.Endpoint(), Literal(ManagementClientGenerator.Instance.InputLibrary.DefaultApiVersion)),
9089
nullCoalesce: true)),
9190
enclosingType);
9291

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Providers/ResourceClientProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ private ConstructorProvider BuildResourceIdentifierConstructor()
239239
foreach (var (inputClient, clientInfo) in _clientInfos)
240240
{
241241
bodyStatements.Add(clientInfo.DiagnosticsField.Assign(New.Instance(typeof(ClientDiagnostics), Literal(Type.Namespace), _resourceTypeField.As<ResourceType>().Namespace(), thisResource.Diagnostics())).Terminate());
242-
bodyStatements.Add(clientInfo.RestClientField.Assign(New.Instance(clientInfo.RestClientProvider.Type, clientInfo.DiagnosticsField, thisResource.Pipeline(), thisResource.Endpoint(), apiVersion)).Terminate());
242+
var effectiveApiVersion = apiVersion.NullCoalesce(Literal(ManagementClientGenerator.Instance.InputLibrary.DefaultApiVersion));
243+
bodyStatements.Add(clientInfo.RestClientField.Assign(New.Instance(clientInfo.RestClientProvider.Type, clientInfo.DiagnosticsField, thisResource.Pipeline(), thisResource.Endpoint(), effectiveApiVersion)).Terminate());
243244
}
244245

245246
bodyStatements.Add(Static(Type).As<ArmResource>().ValidateResourceId(idParameter).Terminate());

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Providers/ResourceCollectionClientProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.TypeSpec.Generator.Providers;
1717
using Microsoft.TypeSpec.Generator.Snippets;
1818
using Microsoft.TypeSpec.Generator.Statements;
19+
using Microsoft.TypeSpec.Generator.Expressions;
1920
using System;
2021
using System.Collections;
2122
using System.Collections.Generic;
@@ -178,7 +179,8 @@ private ConstructorProvider BuildResourceIdentifierConstructor()
178179
foreach (var (inputClient, clientInfo) in _clientInfos)
179180
{
180181
bodyStatements.Add(clientInfo.DiagnosticsField.Assign(New.Instance(typeof(ClientDiagnostics), Literal(Type.Namespace), _resourceTypeExpression.Namespace(), thisCollection.Diagnostics())).Terminate());
181-
bodyStatements.Add(clientInfo.RestClientField.Assign(New.Instance(clientInfo.RestClientProvider.Type, clientInfo.DiagnosticsField, thisCollection.Pipeline(), thisCollection.Endpoint(), apiVersion)).Terminate());
182+
var effectiveApiVersion = apiVersion.NullCoalesce(Literal(ManagementClientGenerator.Instance.InputLibrary.DefaultApiVersion));
183+
bodyStatements.Add(clientInfo.RestClientField.Assign(New.Instance(clientInfo.RestClientProvider.Type, clientInfo.DiagnosticsField, thisCollection.Pipeline(), thisCollection.Endpoint(), effectiveApiVersion)).Terminate());
182184
}
183185

184186
bodyStatements.Add(Static(Type).As<ArmCollection>().ValidateResourceId(idParameter).Terminate());

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Snippets/ResourceMethodSnippets.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using Azure.Core;
55
using Azure.Core.Pipeline;
6-
using Azure.Generator.Management.Providers;
76
using Azure.Generator.Management.Visitors;
87
using Microsoft.TypeSpec.Generator.Expressions;
98
using Microsoft.TypeSpec.Generator.Primitives;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
this.TryGetApiVersion(ResourceType, out string responseTypeApiVersion);
22
_testClientClientDiagnostics = new global::Azure.Core.Pipeline.ClientDiagnostics("Samples", ResourceType.Namespace, this.Diagnostics);
3-
_testClientRestClient = new global::Samples.TestClient(_testClientClientDiagnostics, this.Pipeline, this.Endpoint, responseTypeApiVersion);
3+
_testClientRestClient = new global::Samples.TestClient(_testClientClientDiagnostics, this.Pipeline, this.Endpoint, (responseTypeApiVersion ?? "2023-01-01"));
44
global::Samples.ResponseTypeResource.ValidateResourceId(id);

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/test/TestHelpers/ManagementMockHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static Mock<ManagementClientGenerator> LoadMockPlugin(
3434
HttpMessageApi? httpMessageApi = null,
3535
string? primaryNamespace = null)
3636
{
37-
IReadOnlyList<string> inputNsApiVersions = apiVersions?.Invoke() ?? [];
37+
IReadOnlyList<string> inputNsApiVersions = apiVersions?.Invoke() ?? ["2023-01-01"];
3838
IReadOnlyList<InputLiteralType> inputNsLiterals = inputLiterals?.Invoke() ?? [];
3939
IReadOnlyList<InputEnumType> inputNsEnums = inputEnums?.Invoke() ?? [];
4040
IReadOnlyList<InputClient> inputNsClients = clients?.Invoke() ?? [];

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BarCollection.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BarResource.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BarSettingsResource.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)