Skip to content

Fix C# reference-map accessibility for hidden union types#11288

Merged
JoshLove-msft merged 13 commits into
microsoft:mainfrom
live1206:fix/csharp-reference-map-sdk-accessibility
Jul 22, 2026
Merged

Fix C# reference-map accessibility for hidden union types#11288
JoshLove-msft merged 13 commits into
microsoft:mainfrom
live1206:fix/csharp-reference-map-sdk-accessibility

Conversation

@live1206

@live1206 live1206 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Summary

  • restore the provider-graph reference map after its main-branch rollback in Revert "Use provider C# reference map with pre-write internalization" #11282, preserving the performance improvement over Roslyn-based generated-source analysis
  • keep all follow-up compatibility fixes already developed in this PR:
    • preserve concrete and nested discriminator variants
    • distinguish metadata-only union items erased to BinaryData from semantic public and implementation dependencies
    • retain generated types that were public in the configured last GA contract, including effectively public nested types
    • preserve the owning model's accessibility when serialization partial providers have different default modifiers
    • derive collection-header helper dependencies from the selected HttpRequestApi, avoiding unused PipelineRequestHeadersExtensions output
  • reconcile Preserve internal C# helper types during trimming #11285 with the restored provider graph:
    • retain its shared InternalHelperProvider modifiers and lazy helper construction
    • preserve its intentional non-root registration so generator-owned internal helpers remain emitted without becoming accessibility roots
  • add positive and negative coverage for accessibility, retention, GA contracts, nested effective accessibility, serialization ownership, metadata-only references, and request-helper dependencies

Fixes #11291.

Main-branch reconciliation

This branch merges current main and explicitly reverts squash commit 05406488b from #11282. The existing #11288 commits remain intact as follow-up commits rather than being rebased or squashed away.

Validation

  • passed 1,712 core generator tests and 1,504 ClientModel tests
  • passed package build, full C# test-library regeneration, formatting, Cop, and type-aware oxlint
  • regenerated all 46 Azure and unbranded data-plane libraries with RegenPreview.ps1: 46 passed, 0 failed, with no untracked files or unexpected request helpers
  • repeated the full SDK regeneration from latest Azure SDK main; the resulting 72-file expected patch was byte-for-byte identical
  • reproduced the service-wide Purview generation command with the local generator; it completed successfully and produced only the expected DataMap helper removal
  • regenerated App Configuration and Projects.Agents after restoring Preserve internal C# helper types during trimming #11285 helper retention; representative internal helper files remain byte-for-byte unchanged while the valid reference-map cleanup remains

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
@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 Jul 17, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 15face8

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
live1206 added 2 commits July 17, 2026 10:08
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
@live1206

live1206 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Expected Azure SDK regeneration diff

The combined RegenPreview.ps1 -Azure -Unbranded run completed with 46 passed, 0 failed. The resulting SDK diff is 72 files, +11/-3,661. There are no untracked files.

Service/package Diff Expected reason
Azure.AI.Agents.Persistent 1 modified, -2 Remove unused imports
Azure.AI.Extensions.OpenAI 1 modified, +1 Restore an experimental-warning pragma
Azure.AI.Projects 2 modified, +3/-3 Remove an unused import and correct MRW warning pragmas
Azure.AI.Projects.Agents 38 files, +4/-2,834 Remove 35 duplicate internal tool-choice files and update related MRW registrations, docs, and imports
Azure.AI.AnomalyDetector 1 deleted, -60 Remove unused URI helper
Azure.Data.AppConfiguration 1 modified, -2 Remove stale ResponseError MRW registration/import
Azure.AI.Language.Conversations 1 deleted, -60 Remove unused URI helper
Azure.AI.Language.QuestionAnswering.Inference 1 deleted, -60 Remove unused URI helper
Azure.AI.Language.Text 1 deleted, -60 Remove unused URI helper
Azure.Communication.JobRouter 1 modified, -2 Remove stale ResponseError MRW registration/import
Azure.Security.CodeTransparency 1 deleted, -60 Remove unused URI helper
Azure.AI.ContentSafety 2 modified, -3 Remove stale MRW registration and unused imports
Azure.Messaging.EventGrid.Namespaces 1 deleted, -60 Remove unused URI helper
Azure.Messaging.EventGrid.SystemEvents 1 deleted, -60 Remove unused URI helper
Azure.Monitor.Ingestion 1 deleted, -60 Remove unused URI helper
Azure.Monitor.OpenTelemetry.Exporter 1 deleted, -60 Remove unused URI helper
Azure.Monitor.Query.Logs 2 files, -65 Remove unused URI helper and model-factory imports
Azure.Analytics.OnlineExperimentation 1 modified, -2 Remove stale MRW registration/import
Azure.Analytics.Purview.DataMap 1 deleted, -60 Remove unused URI helper
Azure.Data.SchemaRegistry 1 modified, -2 Remove stale MRW registration/import
Azure.Template 1 modified, -2 Remove unused model-factory imports
Azure.AI.Speech.Transcription 1 modified, -1 Remove unused model-factory import
Azure.AI.Translation.Document 2 modified, -3 Remove stale MRW registration and unused imports
Azure.AI.Translation.Text 1 deleted, -60 Remove unused URI helper
Azure.AI.Vision.ImageAnalysis 2 modified, -4 Remove stale MRW registration and unused imports
Azure.AI.VoiceLive 5 files, +3/-76 Remove unused URI helper; internalize two preview-only models and remove their factory method

There are no GA public API changes. The only accessibility change is the VoiceLive preview-only surface that was not present in GA 1.1.0.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the C# generator’s provider reference-map analysis to preserve established public API surface (including last GA contract/public nested types) while preventing BinaryData-erased unions from incorrectly “publicizing” internal/abstract union members, and it fixes request-header helper retention by deriving collection-header helper dependencies from the selected HttpRequestApi.

Changes:

  • Extend reference-map graph construction to distinguish union item references that affect public surface vs metadata-only union items (erased to BinaryData) and to root types that were public in the last contract.
  • Improve discriminator-derived reachability handling for “known variant” cases (including intermediate bases and nested scenarios).
  • Make collection-header helper retention conditional on HttpRequestApi capabilities, avoiding generation/retention of unused PipelineRequestHeadersExtensions for non-pipeline request APIs; add positive/negative test coverage.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/ReferenceMap/ProviderReferenceMapAnalyzerTests.cs Adds coverage for BinaryData/metadata-only unions, discriminator/nested accessibility, and last-contract rooting.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ModelProviderTests.cs Verifies unverifiable additional-properties types are treated as metadata-only unions.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/ReferenceMap/ProviderReferenceMapAnalyzer.Helpers.cs Adds last-contract public rooting helpers and threads union-item exclusions into custom-code rooting.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/ReferenceMap/ProviderReferenceMapAnalyzer.cs Integrates union-item exclusions into public graph analysis and roots last-contract public types.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/ReferenceMap/ProviderReferenceMapAnalyzer.Candidates.cs Roots last-contract public types during removal; refines discriminator reachability propagation.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/ReferenceMap/ProviderReferenceMapAnalyzer.BodyReferences.cs Adds union-item exclusion handling and respects UnionItemTypeReferenceKind during reference traversal.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs Marks unverifiable types as metadata-only unions (to avoid retaining them as real dependencies).
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Primitives/CSharpType.cs Introduces UnionItemTypeReferenceKind and plumbs it through union construction/cloning.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/TestHelpers/MockHelpers.cs Allows injecting a custom HttpRequestApi in ClientModel tests.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/ReferenceMap/ClientBodyDependencyReferenceMapTests.cs Adds negative test ensuring scalar headers don’t retain PipelineRequestHeadersExtensions.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/RestClientProviders/RestClientProviderTests.cs Adds coverage for request-API-dependent collection-header helper retention.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/RestClientProvider.cs Uses HttpRequestApi capability to decide which collection-header helper type (if any) to retain.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/PipelineRequestProvider.cs Implements the request-API helper capability for the default pipeline request API.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/Abstractions/HttpRequestApi.cs Adds GetCollectionHeaderHelperType() capability hook (default: none).

Co-authored-by: jolov <jolov@microsoft.com>

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
@live1206

Copy link
Copy Markdown
Contributor Author

Revalidated the combined Azure/unbranded regeneration from a fresh worktree at latest azure-sdk-for-net main commit d75bb4711893c84e7e3c4ec3062404f33ad693b2: 46 passed, 0 failed. The same 72 files changed with the same +11/-3,661 totals, and the complete SDK patch is byte-for-byte identical to the previously reviewed result.

live1206 and others added 2 commits July 20, 2026 02:03
Preserve the reference-map follow-up changes while taking the latest main branch before restoring the reverted implementation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
Revert the main-branch rollback from microsoft#11282 while preserving the accessibility and SDK-compatibility fixes already on microsoft#11288. Keep the shared internal helper provider from microsoft#11285, but let provider-graph reachability determine whether helpers are emitted.

This reverts commit 0540648.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
@live1206

Copy link
Copy Markdown
Contributor Author

latest regen: Azure/azure-sdk-for-net#61111

@jorgerangel-msft jorgerangel-msft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is at least 1 change in the regen preview that doesn't compile: details

live1206 and others added 2 commits July 21, 2026 01:02
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
@live1206

Copy link
Copy Markdown
Contributor Author

Follow-up for the Azure SDK full-regeneration build failure in Azure/azure-sdk-for-net#61111:

  • Built all 26 affected SDK packages: 24 passed; Projects.Agents failed on unresolved OpenAI.InternalToolChoiceParam XML documentation, and Projects failed only transitively.
  • Fixed the issue generically in b7bb20a: :C formatting now renders providers removed by the reference map as <c> text instead of invalid cref links, including removed generic arguments.
  • Regenerated Projects.Agents with the local emitter; the stale reference is now <c>global::OpenAI.InternalToolChoiceParam</c>.
  • Projects.Agents and Projects both build successfully across net8.0, net10.0, and netstandard2.0.
  • Full validation passed: 1,712 core tests, 1,504 ClientModel tests, complete generated-library regeneration, package build/format, Cop, and type-aware oxlint.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
@live1206

Copy link
Copy Markdown
Contributor Author

Restored #11285’s intentional generic InternalHelperProvider non-root retention in 3a9384605. Helpers remain internal and do not become accessibility roots, but they are no longer removed solely because provider-graph reachability finds no call site. Focused App Configuration and Projects.Agents regeneration keeps representative helper files byte-for-byte unchanged while preserving the valid reference-map cleanup.

@live1206

Copy link
Copy Markdown
Contributor Author

latest regen: Azure/azure-sdk-for-net#61150

@JoshLove-msft JoshLove-msft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference-map correctness findings.

--generated by Copilot

live1206 added 3 commits July 22, 2026 04:48
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c4b68173-2f2b-467d-9a94-9be9d14b5e17
@live1206

Copy link
Copy Markdown
Contributor Author

latest regen: Azure/azure-sdk-for-net#61203

@JoshLove-msft
JoshLove-msft enabled auto-merge July 22, 2026 19:06
@JoshLove-msft
JoshLove-msft added this pull request to the merge queue Jul 22, 2026
Merged via the queue into microsoft:main with commit 89f388c Jul 22, 2026
29 checks passed
@live1206
live1206 deleted the fix/csharp-reference-map-sdk-accessibility branch July 23, 2026 01:54
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[http-client-csharp] Avoid retaining unused PipelineRequestHeadersExtensions for Azure request APIs

4 participants