Skip to content

[http-client-csharp] Honor lifecycle visibility in shared models - #12444

Merged
JoshLove-msft merged 7 commits into
microsoft:mainfrom
JoshLove-msft:joshlove-msft-csharp-lifecycle-visibility
Oct 30, 2026
Merged

JoshLove-msft merged 7 commits into
microsoft:mainfrom
JoshLove-msft:joshlove-msft-csharp-lifecycle-visibility

Conversation

@JoshLove-msft

@JoshLove-msft JoshLove-msft commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #12197.

Preserves shared C# models while honoring each operation's lifecycle visibility:

  • Relaxes constructor requiredness for properties excluded from an applicable request lifecycle, without changing update-only required properties or unrelated polymorphic siblings.
  • Passes operation visibility through request serialization using a generated internal [Flags] enum so populated, disallowed properties are omitted, including nested models and collections, spread inputs, XML, JSON Lines, and multipart bodies.
  • Retains lifecycle context for read-only XML properties, recognizes text/vendor XML media types, and forwards options through enumerable/span-specific serializers while preserving media-type selection order.
  • Preserves read deserialization, ordinary persistence serialization, optional-body omission, and raw PATCH omission semantics. No typed PATCH convenience API or disallowed-property validation is added.
  • Implements all Visibility Spector scenarios and adds emitter, input-converter, generator, and generated-client regressions.

All checked-in libraries were regenerated. Validation passed: 406 emitter tests, 318 input tests, 3,013 generator tests, 2,690 ClientModel tests, 839 local runtime tests, and all 10 Visibility Spector tests; emitter type checking/API extraction, repository emitter lint rules, and Cop checks also passed. Disabling lifecycle filtering caused all nine original payload-filtering regressions to fail consistently in two runs; restoring it returned the runtime suite to green. The XML and collection review regressions also reproduced the original failures before their fixes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 03:29
@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 Oct 30, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 95a4daf

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

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.

🟡 Changes recommended

Critical serialization defects can cause XML and collection request bodies to fail at runtime and allow read-only XML properties to be sent in Create requests.

3 open issues
What changed in this PR

Addresses #12197 by preserving shared C# models while making constructor requiredness and request serialization respect operation lifecycle visibility.

Changes:

  • Carries property and operation visibility from the emitter into the C# generator.
  • Applies lifecycle filtering across JSON, XML, collections, streaming, and multipart requests.
  • Adds regression coverage and regenerates fixtures for shared models, persistence, optional bodies, and raw PATCH behavior.

Paths below are relative to packages/http-client-csharp/.

File Description
generator/​TestProjects/​Spector/​http/​type/​model/​visibility/​tspCodeModel.json Records lifecycle metadata and relaxed requiredness.
generator/​TestProjects/​Spector/​http/​type/​model/​visibility/​src/​Generated/​TypeModelVisibilityModelFactory.cs Updates nullable factory parameters.
generator/​TestProjects/​Spector/​http/​type/​model/​visibility/​src/​Generated/​Models/​VisibilityModel.Serialization.cs Removes the redundant internal constructor.
generator/​TestProjects/​Spector/​http/​type/​model/​visibility/​src/​Generated/​Models/​VisibilityModel.cs Updates shared-model constructor and properties.
generator/​TestProjects/​Spector.Tests/​Http/​_Type/​Model/​Visibility/​VisibilityTests.cs Adds lifecycle operation scenarios.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​VisibilitySamples.RestClient.cs Adds regression fixture request builders.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​VisibilitySamples.cs Passes operation visibility into serialization.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​SampleTypeSpecModelFactory.cs Adds fixture model factories.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​SampleTypeSpecClient.cs Exposes the visibility sub-client.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​SampleTypeSpecContext.cs Registers new serializable models.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​LifecycleMultipartTopic.Serialization.cs Adds nested multipart model serialization.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​LifecycleMultipartTopic.cs Defines the nested multipart fixture model.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​LifecycleMultipart.Serialization.Multipart.cs Filters parts and forwards serialization options.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​LifecycleMultipart.cs Defines the multipart fixture model.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ChatTopicState.Serialization.cs Adds state enum wire conversion.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ChatTopicState.cs Defines fixture state values.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ChatTopicEnvelope.Serialization.cs Serializes nested models and collections.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ChatTopicEnvelope.cs Defines the nested collection fixture.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ChatTopic.Serialization.cs Applies JSON and XML visibility guards.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ChatTopic.cs Makes lifecycle-specific state optional to construct.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Internal/​LifecycleModelReaderWriterOptions.cs Adds generated lifecycle options and checks.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Internal/​JsonLinesBinaryContent.cs Forwards options during streaming serialization.
generator/​TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Internal/​BinaryContentHelper.cs Forwards options through collection helpers.
generator/​TestProjects/​Local/​Sample-TypeSpec/​Sample-TypeSpec.tsp Defines visibility regression models and operations.
generator/​TestProjects/​Local.Tests/​LifecycleVisibilityTests.cs Tests payload filtering and preserved behavior.
generator/​Microsoft.TypeSpec.Generator/​src/​Primitives/​PropertyWireInformation.cs Carries property visibility into serialization metadata.
generator/​Microsoft.TypeSpec.Generator.Input/​test/​TypeSpecInputConverterTests.cs Tests visibility metadata deserialization.
generator/​Microsoft.TypeSpec.Generator.Input/​test/​InputModelPropertyTests.cs Tests property visibility defaults and updates.
generator/​Microsoft.TypeSpec.Generator.Input/​src/​InputTypes/​Serialization/​InputOperationConverter.cs Reads operation request visibility.
generator/​Microsoft.TypeSpec.Generator.Input/​src/​InputTypes/​Serialization/​InputModelPropertyConverter.cs Reads property lifecycle visibility.
generator/​Microsoft.TypeSpec.Generator.Input/​src/​InputTypes/​InputVisibility.cs Defines lifecycle flags.
generator/​Microsoft.TypeSpec.Generator.Input/​src/​InputTypes/​InputOperation.cs Stores and updates request visibility.
generator/​Microsoft.TypeSpec.Generator.Input/​src/​InputTypes/​InputModelProperty.cs Stores and updates property visibility.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​TestData/​ScmMethodProviderCollectionTests/​JsonLinesRequestGeneratesAsyncStreamingConvenienceMethod(WriteToAsyncStj).cs Updates the streaming options baseline.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​XmlSerializationTests.cs Selects read-only XML baselines.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​XmlSerializationTests/​XmlSerializationSkipsNullCollections(True,ReadOnly).cs Covers unwrapped read-only collection guards.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​XmlSerializationTests/​XmlSerializationSkipsNullCollections(False,ReadOnly).cs Covers wrapped read-only collection guards.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​TestData/​BinaryContentHelperDefinitionTests/​FromEnumerableXmlMethodIsCorrectlyDefined.cs Updates XML options-forwarding baseline.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​BinaryContentHelperDefinitionTests.cs Verifies the helper’s options parameter.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Snippets/​MultiPartFormContentSnippets.cs Supports multipart serialization options.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Snippets/​BinaryContentHelperSnippets.cs Supports collection serialization options.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​ScmOutputLibrary.cs Registers the lifecycle helper provider.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​ScmMethodProviderCollection.cs Generates lifecycle-aware request conversions.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​MultipartFormDataSerializationDefinition.cs Filters multipart properties and forwards options.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​MrwSerializationTypeDefinition.Xml.cs Adds XML property visibility guards.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​MrwSerializationTypeDefinition.cs Adds shared property visibility wrapping.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​LifecycleModelReaderWriterOptionsDefinition.cs Generates lifecycle options and visibility checks.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​JsonLinesBinaryContentDefinition.cs Generates streaming options propagation.
generator/​Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​BinaryContentHelperDefinition.cs Generates options-aware collection helpers.
emitter/​test/​Unit/​model-type.test.ts Tests shared-model requiredness and inheritance.
emitter/​src/​type/​input-type.ts Adds property lifecycle metadata.
emitter/​src/​type/​input-operation.ts Adds operation visibility metadata.
emitter/​src/​lib/​type-converter.ts Converts property visibility to flags.
emitter/​src/​lib/​operation-converter.ts Resolves operation request visibility.
emitter/​src/​lib/​client-model-builder.ts Relaxes requiredness across applicable input lifecycles.

Review effort: Balanced

Use named internal lifecycle flags and preserve XML and collection serialization paths.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 04:15

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.

🟡 Changes recommended

Passing options for lifecycle-aware union arrays and dictionaries selects generic helpers that base64-encode BinaryData instead of embedding its JSON, producing incorrect request bodies.

1 open issue
3 resolved since last review

Review effort: Balanced

…ions

Accept serialization options on specialized BinaryData collection helpers to preserve overload selection and raw JSON values.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 05:04

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.

🟡 Changes recommended

The new using scope disposes paging request content before deferred enumeration, causing ObjectDisposedException on the first request, while scalar union BinaryData bodies incur unnecessary full-payload processing.

2 open issues
1 resolved since last review

Review effort: Balanced

Keep request content alive when returning paging collections and skip unused serialization for raw BinaryData bodies. Cover sync/async paging and raw union request paths.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 05:57

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.

🟡 Changes recommended

A critical issue remains: populated read-only multipart parts can still be sent on Create requests, requiring explicit lifecycle-aware handling and regression coverage.

1 open issue
2 resolved since last review

Review effort: Balanced

Retain lifecycle context for read-only multipart parts and guard serialization while preserving ordinary options behavior.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 06:49

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.

🟡 Changes recommended

Lifecycle filtering can produce an empty nested XML element that the reader does not handle, causing request serialization to loop indefinitely; the helper needs empty-element/EOF handling and a regression test.

1 open issue
1 resolved since last review

Review effort: Balanced

Skip child copying for self-closing XML roots and stop copying at EOF. Share the copying path for named and unnamed elements while preserving attributes. Cover fully filtered request models and collection items, both helper branches, and malformed XML.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 07:41

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.

🟢 Approval recommended

No active review issues block approval of the lifecycle visibility changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Preserve lifecycle context for base-typed requests accepting non-discriminated descendants, without traversing unrelated siblings. Clarify lifecycle preparation, defer media-type detection, document generated visibility flags, and expand metadata and runtime regressions.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 20:58

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.

🟢 Approval recommended

No active review issues were identified for the C# client lifecycle-visibility update.

No open issues 🎉

Review effort: Balanced

@JoshLove-msft
JoshLove-msft added this pull request to the merge queue Oct 30, 2026
Merged via the queue into microsoft:main with commit 05b9bad Oct 30, 2026
30 checks passed
@JoshLove-msft
JoshLove-msft deleted the joshlove-msft-csharp-lifecycle-visibility branch October 30, 2026 22:43
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.

[Bug]: C# emitter should honor lifecycle visibility in model requiredness and request serialization

4 participants