Skip to content

Commit 2d7d3ed

Browse files
Adopt clientInitialization spector scenarios (#54227)
* Initial plan * Adopt clientInitialization spector scenarios - add generated project and tests Co-authored-by: JoshLove-msft <[email protected]> * Add tspconfig.yaml with package-name for proper namespace configuration Co-authored-by: JoshLove-msft <[email protected]> * Improve comment clarity in ParamAlias test Co-authored-by: JoshLove-msft <[email protected]> * Pass null for options parameter to match other tests Co-authored-by: JoshLove-msft <[email protected]> * Add Ignore attribute to ParentClient_ChildClient test - InitializedBy flag not respected Co-authored-by: JoshLove-msft <[email protected]> * regen --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: JoshLove-msft <[email protected]> Co-authored-by: jolov <[email protected]>
1 parent 4744252 commit 2d7d3ed

31 files changed

+3078
-1
lines changed

eng/packages/http-client-csharp/eng/scripts/Spector-Helper.psm1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ $failingSpecs = @(
66
Join-Path 'http' 'response' 'status-code-range' # Response namespace conflicts with Azure.Response
77
# Azure scenarios not yet buildable
88
Join-Path 'http' 'azure' 'client-generator-core' 'alternate-type'
9-
Join-Path 'http' 'azure' 'client-generator-core' 'client-initialization'
109
Join-Path 'http' 'azure' 'client-generator-core' 'deserialize-empty-string-as-null' # long path issue and also not needed for Azure emitter
1110
# These scenarios will be covered in Azure.Generator.Management
1211
Join-Path 'http' 'azure' 'resource-manager' 'common-properties'

eng/packages/http-client-csharp/generator/Azure.Generator/src/Properties/launchSettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
"commandName": "Executable",
4646
"executablePath": "dotnet"
4747
},
48+
"http-azure-client-generator-core-client-initialization": {
49+
"commandLineArgs": "$(SolutionDir)/../dist/generator/Microsoft.TypeSpec.Generator.dll $(SolutionDir)/TestProjects/Spector/http/azure/client-generator-core/client-initialization -g AzureStubGenerator",
50+
"commandName": "Executable",
51+
"executablePath": "dotnet"
52+
},
4853
"http-azure-client-generator-core-client-location": {
4954
"commandLineArgs": "$(SolutionDir)/../dist/generator/Microsoft.TypeSpec.Generator.dll $(SolutionDir)/TestProjects/Spector/http/azure/client-generator-core/client-location -g AzureStubGenerator",
5055
"commandName": "Executable",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using NUnit.Framework;
7+
using Service;
8+
using Specs.Azure.ClientGeneratorCore.ClientInitialization;
9+
using Specs.Azure.ClientGeneratorCore.ClientInitialization._ParentClient;
10+
11+
namespace TestProjects.Spector.Tests.Http.Azure.ClientGeneratorCore.ClientInitialization
12+
{
13+
public class ClientInitializationTests : SpectorTestBase
14+
{
15+
[SpectorTest]
16+
public Task Azure_ClientGenerator_Core_ClientInitialization_HeaderParam() => Test(async (host) =>
17+
{
18+
var client = new HeaderParamClient(host, "test-name-value", null);
19+
20+
// Test WithQuery - name is elevated to client constructor, only id is passed
21+
await client.WithQueryAsync("test-id");
22+
23+
// Test WithBody - name is elevated to client constructor, body has name property
24+
await client.WithBodyAsync(new Input("test-name"));
25+
});
26+
27+
[SpectorTest]
28+
public Task Azure_ClientGenerator_Core_ClientInitialization_MultipleParams() => Test(async (host) =>
29+
{
30+
var client = new MultipleParamsClient(host, "test-name-value", "us-west", null);
31+
32+
// Test WithQuery - name and region are elevated to client constructor, only id is passed
33+
await client.WithQueryAsync("test-id");
34+
35+
// Test WithBody - name and region are elevated to client constructor, body has name property
36+
await client.WithBodyAsync(new Input("test-name"));
37+
});
38+
39+
[SpectorTest]
40+
public Task Azure_ClientGenerator_Core_ClientInitialization_MixedParams() => Test(async (host) =>
41+
{
42+
var client = new MixedParamsClient(host, "test-name-value", null);
43+
44+
// Test WithQuery - name is elevated to client constructor, region and id are method params
45+
await client.WithQueryAsync("us-west", "test-id");
46+
47+
// Test WithBody - name is elevated to client constructor, region is method param
48+
await client.WithBodyAsync("us-west", new WithBodyRequest("test-name"));
49+
});
50+
51+
[SpectorTest]
52+
public Task Azure_ClientGenerator_Core_ClientInitialization_PathParam() => Test(async (host) =>
53+
{
54+
var client = new PathParamClient(host, "sample-blob", null);
55+
56+
// Test WithQuery - blobName is elevated to client constructor, format is method param
57+
await client.WithQueryAsync("text");
58+
59+
// Test GetStandalone - blobName is elevated to client constructor
60+
var response = await client.GetStandaloneAsync();
61+
Assert.AreEqual("sample-blob", response.Value.Name);
62+
Assert.AreEqual(42, response.Value.Size);
63+
Assert.AreEqual("text/plain", response.Value.ContentType);
64+
Assert.AreEqual(new DateTimeOffset(2025, 4, 1, 12, 0, 0, TimeSpan.Zero), response.Value.CreatedOn);
65+
66+
// Test DeleteStandalone - blobName is elevated to client constructor
67+
await client.DeleteStandaloneAsync();
68+
});
69+
70+
[SpectorTest]
71+
public Task Azure_ClientGenerator_Core_ClientInitialization_ParamAlias() => Test(async (host) =>
72+
{
73+
// blobName is used for routes with original name (/blobName/with-original-name)
74+
// blob is used for routes with aliased name (/blob/with-aliased-name)
75+
// Both params point to the same path segment value "sample-blob" in this scenario
76+
var client = new ParamAliasClient(host, "sample-blob", "sample-blob", null);
77+
78+
// Test WithAliasedName - blob parameter is elevated to client constructor
79+
await client.WithAliasedNameAsync();
80+
81+
// Test WithOriginalName - blobName parameter is elevated to client constructor
82+
await client.WithOriginalNameAsync();
83+
});
84+
85+
[SpectorTest]
86+
[Ignore("The InitializedBy flag is not being respected. The Child client should have a public constructor.")]
87+
public Task Azure_ClientGenerator_Core_ClientInitialization_ParentClient_ChildClient() => Test(async (host) =>
88+
{
89+
// Create ParentClient with blobName elevated to client constructor
90+
var parentClient = new ParentClient(host, "sample-blob", null);
91+
92+
// Get ChildClient from ParentClient
93+
var childClient = parentClient.GetChildClient();
94+
95+
// Test WithQuery - blobName is elevated to parent client, format is method param
96+
await childClient.WithQueryAsync("text");
97+
98+
// Test GetStandalone - blobName is elevated to parent client
99+
var response = await childClient.GetStandaloneAsync();
100+
Assert.AreEqual("sample-blob", response.Value.Name);
101+
Assert.AreEqual(42, response.Value.Size);
102+
Assert.AreEqual("text/plain", response.Value.ContentType);
103+
Assert.AreEqual(new DateTimeOffset(2025, 4, 1, 12, 0, 0, TimeSpan.Zero), response.Value.CreatedOn);
104+
105+
// Test DeleteStandalone - blobName is elevated to parent client
106+
await childClient.DeleteStandaloneAsync();
107+
});
108+
}
109+
}

eng/packages/http-client-csharp/generator/TestProjects/Spector.Tests/TestProjects.Spector.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<ProjectReference Include="..\Spector\http\azure\client-generator-core\usage\src\_Specs_.Azure.ClientGenerator.Core.Usage.csproj" />
2424
<ProjectReference Include="..\Spector\http\azure\client-generator-core\client-location\src\_Specs_.Azure.ClientGenerator.Core.ClientLocation.csproj" />
2525
<ProjectReference Include="..\Spector\http\azure\client-generator-core\hierarchy-building\src\_Specs_.Azure.ClientGenerator.Core.HierarchyBuilding.csproj" />
26+
<ProjectReference Include="..\Spector\http\azure\client-generator-core\client-initialization\src\_Specs_.Azure.ClientGenerator.Core.ClientInitialization.csproj" />
2627
<!-- not yet buildable <ProjectReference Include="..\Spector\http\azure\client-generator-core\override\src\_Specs_.Azure.ClientGenerator.Core.Override.csproj" />-->
2728
<ProjectReference Include="..\Spector\http\azure\special-headers\client-request-id\src\Azure.SpecialHeaders.XmsClientRequestId.csproj" />
2829
<ProjectReference Include="..\Spector\http\azure\core\lro\standard\src\_Specs_.Azure.Core.Lro.Standard.csproj" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"package-name": "_Specs_.Azure.ClientGenerator.Core.ClientInitialization",
3+
"license": {
4+
"name": "MIT License",
5+
"company": "Microsoft Corporation",
6+
"link": "https://mit-license.org",
7+
"header": "Copyright (c) Microsoft Corporation. All rights reserved.\nLicensed under the MIT License.",
8+
"description": "Copyright (c) Microsoft Corporation\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the “Software”), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE."
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.0.31903.59
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "_Specs_.Azure.ClientGenerator.Core.ClientInitialization", "src\_Specs_.Azure.ClientGenerator.Core.ClientInitialization.csproj", "{28FF4005-4467-4E36-92E7-DEA27DEB1519}"
6+
EndProject
7+
Global
8+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9+
Debug|Any CPU = Debug|Any CPU
10+
Release|Any CPU = Release|Any CPU
11+
EndGlobalSection
12+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
13+
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14+
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
15+
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
16+
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.Build.0 = Release|Any CPU
21+
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.Build.0 = Release|Any CPU
41+
EndGlobalSection
42+
GlobalSection(SolutionProperties) = preSolution
43+
HideSolutionNode = FALSE
44+
EndGlobalSection
45+
GlobalSection(ExtensibilityGlobals) = postSolution
46+
SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}
47+
EndGlobalSection
48+
EndGlobal

eng/packages/http-client-csharp/generator/TestProjects/Spector/http/azure/client-generator-core/client-initialization/src/Generated/ChildClient.cs

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp/generator/TestProjects/Spector/http/azure/client-generator-core/client-initialization/src/Generated/HeaderParamClient.cs

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp/generator/TestProjects/Spector/http/azure/client-generator-core/client-initialization/src/Generated/HeaderParamClientOptions.cs

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp/generator/TestProjects/Spector/http/azure/client-generator-core/client-initialization/src/Generated/MixedParamsClient.cs

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)