Skip to content

fix: generate parameterized accessor for subclients with subclient-specific parameters#9919

Open
Copilot wants to merge 7 commits intomainfrom
copilot/add-subclient-parameters-accessors
Open

fix: generate parameterized accessor for subclients with subclient-specific parameters#9919
Copilot wants to merge 7 commits intomainfrom
copilot/add-subclient-parameters-accessors

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

When a subclient (initialized by parent, or individually+parent) has parameters absent from the parent, the parent was generating a non-functional caching field and an accessor with no parameters — making it impossible to pass the subclient-specific values.

Root cause

BuildFields() always added _clientCachingField for any parent-initialized subclient. BuildMethods() had the accessorMethodParams > 0 branch to generate parameterized accessors, but the spurious caching field remained.

Changes

  • ClientProvider.HasAccessorOnlyParameters() — new method that detects non-endpoint, non-apiversion subclient parameters absent from the parent
  • BuildFields() — skips the caching field when HasAccessorOnlyParameters() is true (different param values → different instances → caching is wrong)
  • New unit testsTestBuildMethods_ForParent_InitializedByParentOnly_WithSubClientParams_HasParameterizedAccessor and TestBuildFields_ForParent_InitializedByParentOnly_WithSubClientParams_NoCachingField

Generated output

Before (wrong — blobName unreachable, dead caching field):

private IndividuallyParentNestedWithQueryClient _cachedIndividuallyParentNestedWithQueryClient;

public virtual IndividuallyParentNestedWithQueryClient GetIndividuallyParentNestedWithQueryClient()
    => Volatile.Read(ref _cachedIndividuallyParentNestedWithQueryClient) ?? ...;

After (correct):

public virtual IndividuallyParentNestedWithQueryClient GetIndividuallyParentNestedWithQueryClient(string blobName)
    => new IndividuallyParentNestedWithQueryClient(Pipeline, _endpoint, blobName);

Test coverage

  • Updated SampleService/main.tsp to give Metrics a metricsNamespace client parameter not on the parent; SampleTypeSpecClient.GetMetricsClient(string metricsNamespace) now demonstrates the fix end-to-end
  • Unit tests cover both the IndividuallyParent and Parent-only subclient scenarios with accessor-only parameters
Original prompt

This section details on the original issue you should resolve

<issue_title>Subclient parameters need to be included in accessors</issue_title>
<issue_description>For subclients that can be created individually or from the parent, the subclient parameters are not included in the subclient accessors currently. </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:js Issue for the JS client emitter: @typespec/http-client-js label Mar 4, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 4, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@9919

commit: d253394

…hods

Reverts incorrect JS emitter changes and implements the fix in the
C# emitter (ClientProvider.cs) where the issue actually belongs.

When a subclient has parameters not present on the parent client,
the parent's accessor method now accepts those extra parameters and
passes them to the subclient constructor. Caching is bypassed for
parameterized accessors since different parameter values may produce
different client instances.

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Mar 4, 2026
Copy link
Contributor Author

Copilot AI commented Mar 4, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: /usr/bin/curl curl -L REDACTED -o /tmp/install-dotnet.sh act-api |accessor\|Accessor de/node/bin/bash (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Add subclient parameters to accessors Add subclient parameters to accessors in the C# emitter Mar 4, 2026
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot AI changed the title Add subclient parameters to accessors in the C# emitter Add subclient-specific parameters to parent accessor methods in C# emitter Mar 4, 2026
…cessor-only parameters

- Add HasAccessorOnlyParameters() to ClientProvider to detect subclient-specific params
- BuildFields(): skip caching field when subclient has accessor-only params
- BuildMethods(): generate parameterized accessor (no caching) when accessorMethodParams > 0
- Add unit tests for Parent-only subclients with accessor-only parameters
- Add MetricsClientParams to SampleService/main.tsp giving Metrics a metricsNamespace param
- Regenerate Sample-TypeSpec (SampleTypeSpecClient.GetMetricsClient now takes metricsNamespace)
- Add individuallyParent Spector test project from azure-http-specs showing all 6 subclient
  accessors correctly parameterized (e.g. GetIndividuallyParentNestedWithQueryClient(string blobName))

Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
Copilot AI changed the title Add subclient-specific parameters to parent accessor methods in C# emitter fix: generate parameterized accessor for subclients with subclient-specific parameters Mar 4, 2026
Co-authored-by: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com>
@JoshLove-msft
Copy link
Contributor

@copilot run prettier on the tsp file being changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp emitter:client:js Issue for the JS client emitter: @typespec/http-client-js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Subclient parameters need to be included in accessors

3 participants