Skip to content

Commit 9db53e1

Browse files
authored
Skip empty client (Azure#47859)
1 parent bcd4faa commit 9db53e1

File tree

12 files changed

+29
-138
lines changed

12 files changed

+29
-138
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258
</ItemGroup>
259259

260260
<ItemGroup Condition="'$(IsGeneratorLibrary)' == 'true'">
261-
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20250114.2" />
261+
<PackageReference Update="Microsoft.Generator.CSharp.ClientModel" Version="1.0.0-alpha.20250117.1" />
262262
</ItemGroup>
263263

264264
<!--

eng/packages/http-client-csharp/generator/Azure.Generator/src/AzureClientPlugin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using Azure.Generator.Utilities;
45
using Microsoft.CodeAnalysis;
56
using Microsoft.Generator.CSharp;
67
using Microsoft.Generator.CSharp.ClientModel;
@@ -29,6 +30,8 @@ public class AzureClientPlugin : ClientModelPlugin
2930
/// <inheritdoc/>
3031
public override AzureOutputLibrary OutputLibrary => _azureOutputLibrary ??= new();
3132

33+
internal ResourceDetection ResourceDetection { get; } = new();
34+
3235
/// <summary>
3336
/// The Azure client plugin to generate the Azure client SDK.
3437
/// </summary>

eng/packages/http-client-csharp/generator/Azure.Generator/src/AzureOutputLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private Dictionary<string, HashSet<OperationSet>> EnsureResourceDataMap()
2929
var result = new Dictionary<string, HashSet<OperationSet>>();
3030
foreach (var operationSet in _pathToOperationSetMap.Values)
3131
{
32-
if (operationSet.TryGetResourceDataSchema(out var resourceSpecName, out var resourceSchema))
32+
if (AzureClientPlugin.Instance.ResourceDetection.TryGetResourceDataSchema(operationSet, out var resourceSpecName, out var resourceSchema))
3333
{
3434
// if this operation set corresponds to a SDK resource, we add it to the map
3535
if (!result.TryGetValue(resourceSpecName!, out HashSet<OperationSet>? value))

eng/packages/http-client-csharp/generator/Azure.Generator/src/AzureTypeFactory.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,16 @@ public override MethodBodyStatement SerializeJsonValue(Type valueType, ValueExpr
109109
}
110110

111111
/// <inheritdoc/>
112-
protected override ClientProvider CreateClientCore(InputClient inputClient)
113-
=> AzureClientPlugin.Instance.IsAzureArm.Value ? base.CreateClientCore(InputClientTransformer.TransformInputClient(inputClient)) : base.CreateClientCore(inputClient);
112+
protected override ClientProvider? CreateClientCore(InputClient inputClient)
113+
{
114+
if (!AzureClientPlugin.Instance.IsAzureArm.Value)
115+
{
116+
return base.CreateClientCore(inputClient);
117+
}
118+
119+
var transformedClient = InputClientTransformer.TransformInputClient(inputClient);
120+
return transformedClient is null ? null : base.CreateClientCore(transformedClient);
121+
}
114122

115123
/// <inheritdoc/>
116124
protected override IReadOnlyList<TypeProvider> CreateSerializationsCore(InputType inputType, TypeProvider typeProvider)

eng/packages/http-client-csharp/generator/Azure.Generator/src/InputTransformation/InputClientTransformer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Azure.Generator.InputTransformation
99
{
1010
internal static class InputClientTransformer
1111
{
12-
public static InputClient TransformInputClient(InputClient client)
12+
public static InputClient? TransformInputClient(InputClient client)
1313
{
1414
var operationsToKeep = new List<InputOperation>();
1515
foreach (var operation in client.Operations)
@@ -21,6 +21,11 @@ public static InputClient TransformInputClient(InputClient client)
2121
operationsToKeep.Add(transformedOperation);
2222
}
2323
}
24+
25+
// We removed the list operation above, we should skip the empty client afterwards
26+
// There is no need to check sub-clients or custom code since it is specific to handle the above removing
27+
if (operationsToKeep.Count == 0) return null;
28+
2429
return new InputClient(client.Name, client.Summary, client.Doc, operationsToKeep, client.Parameters, client.Parent);
2530
}
2631

eng/packages/http-client-csharp/generator/Azure.Generator/src/Providers/RequestContextExtensionsDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Azure.Generator.Providers
1414
{
1515
internal class RequestContextExtensionsDefinition : TypeProvider
1616
{
17-
protected override TypeSignatureModifiers GetDeclarationModifiers() => TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
17+
protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
1818

1919
protected override string BuildName() => "RequestContextExtensions";
2020

eng/packages/http-client-csharp/generator/Azure.Generator/src/Utilities/ResourceDetection.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,15 @@
1111

1212
namespace Azure.Generator.Utilities
1313
{
14-
internal static class ResourceDetection
14+
internal class ResourceDetection
1515
{
1616
private const string ProvidersSegment = "/providers/";
17-
private static ConcurrentDictionary<string, (string Name, InputModelType? InputModel)?> _resourceDataSchemaCache = new ConcurrentDictionary<string, (string Name, InputModelType? InputModel)?>();
18-
19-
public static bool IsResource(this OperationSet set)
20-
{
21-
return set.TryGetResourceDataSchema(out _, out _);
22-
}
17+
private ConcurrentDictionary<string, (string Name, InputModelType? InputModel)?> _resourceDataSchemaCache = new ConcurrentDictionary<string, (string Name, InputModelType? InputModel)?>();
2318

2419
private static InputModelType? FindObjectSchemaWithName(string name)
2520
=> AzureClientPlugin.Instance.InputLibrary.InputNamespace.Models.OfType<InputModelType>().FirstOrDefault(inputModel => inputModel.Name == name);
2621

27-
public static bool TryGetResourceDataSchema(this OperationSet set, [MaybeNullWhen(false)] out string resourceSpecName, out InputModelType? inputModel)
22+
public bool TryGetResourceDataSchema(OperationSet set, [MaybeNullWhen(false)] out string resourceSpecName, out InputModelType? inputModel)
2823
{
2924
resourceSpecName = null;
3025
inputModel = null;
@@ -48,15 +43,15 @@ public static bool TryGetResourceDataSchema(this OperationSet set, [MaybeNullWhe
4843
return false;
4944

5045
// try put operation to get the resource name
51-
if (set.TryOperationWithMethod(RequestMethod.Put, out inputModel))
46+
if (TryOperationWithMethod(set, RequestMethod.Put, out inputModel))
5247
{
5348
resourceSpecName = inputModel.Name;
5449
_resourceDataSchemaCache.TryAdd(set.RequestPath, (resourceSpecName, inputModel));
5550
return true;
5651
}
5752

5853
// try get operation to get the resource name
59-
if (set.TryOperationWithMethod(RequestMethod.Get, out inputModel))
54+
if (TryOperationWithMethod(set, RequestMethod.Get, out inputModel))
6055
{
6156
resourceSpecName = inputModel.Name;
6257
_resourceDataSchemaCache.TryAdd(set.RequestPath, (resourceSpecName, inputModel));
@@ -80,7 +75,7 @@ private static bool CheckEvenSegments(string requestPath)
8075
return segments.Length % 2 == 0;
8176
}
8277

83-
private static bool TryOperationWithMethod(this OperationSet set, RequestMethod method, [MaybeNullWhen(false)] out InputModelType inputModel)
78+
private bool TryOperationWithMethod(OperationSet set, RequestMethod method, [MaybeNullWhen(false)] out InputModelType inputModel)
8479
{
8580
inputModel = null;
8681

eng/packages/http-client-csharp/generator/Azure.Generator/test/Providers/Abstractions/AzureClientResponseProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static ClientProvider CreateMockClientProvider()
5151
{
5252
var client = InputFactory.Client("TestClient", [InputFactory.Operation("foo")]);
5353
MockHelpers.LoadMockPlugin(clientResponseApi: AzureClientResponseProvider.Instance);
54-
var clientProvider = AzureClientPlugin.Instance.TypeFactory.CreateClient(client);
54+
var clientProvider = AzureClientPlugin.Instance.TypeFactory.CreateClient(client)!;
5555
return clientProvider;
5656
}
5757
}

eng/packages/http-client-csharp/generator/Azure.Generator/test/Providers/ClientProviderTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.Generator.CSharp.Providers;
88
using NUnit.Framework;
99
using System;
10-
using System.ClientModel;
1110
using System.Collections.Generic;
1211
using System.Linq;
1312
using System.Runtime.CompilerServices;

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)