Skip to content

[WIP] feat: add support for model proxies - #12603

Draft
Jorge Rangel (jorgerangel-msft) wants to merge 4 commits into
microsoft:mainfrom
jorgerangel-msft:jorgerangel-msft-csharp-route-nested-model-mrw-proxies
Draft

Jorge Rangel (jorgerangel-msft) wants to merge 4 commits into
microsoft:mainfrom
jorgerangel-msft:jorgerangel-msft-csharp-route-nested-model-mrw-proxies

Conversation

@jorgerangel-msft

Copy link
Copy Markdown
Contributor

No description provided.

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

@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 Nov 17, 2026

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

XML-only parent reads using wire-format options can silently bypass a registered child proxy when the child also supports JSON, causing inconsistent behavior between equivalent wire-format and explicit XML reads.

1 open issue
What changed in this PR

Adds model-proxy support to the C# HTTP client emitter, allowing registered proxies to participate in nested model serialization and deserialization.

Changes:

  • Adds proxy-aware nested JSON/XML reads and JSON writes, with static-deserializer fallbacks.
  • Adds runtime tests, generator baselines, and regenerated sample serializers.
  • Updates scaffolding and test projects to System.ClientModel version 1.17.0-alpha.20261117.1.

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

File Description
TestProjects/​Spector/​http/​versioning/​typeChangedFrom/​v2/​src/​Versioning.TypeChangedFrom.V2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​typeChangedFrom/​v1/​src/​Versioning.TypeChangedFrom.V1.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​returnTypeChangedFrom/​v2/​src/​Versioning.ReturnTypeChangedFrom.V2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​returnTypeChangedFrom/​v1/​src/​Versioning.ReturnTypeChangedFrom.V1.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​renamedFrom/​v2/​src/​Versioning.RenamedFrom.V2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​renamedFrom/​v1/​src/​Versioning.RenamedFrom.V1.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​removed/​v2Preview/​src/​Versioning.Removed.V2Preview.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​removed/​v2/​src/​Versioning.Removed.V2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​removed/​v1/​src/​Versioning.Removed.V1.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​madeOptional/​v2/​src/​Versioning.MadeOptional.V2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​madeOptional/​v1/​src/​Versioning.MadeOptional.V1.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​added/​v2/​src/​Versioning.Added.V2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​versioning/​added/​v1/​src/​Versioning.Added.V1.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​union/​src/​Type.Union.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​union/​discriminated/​extends/​src/​Type.Union.Discriminated.Extends.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​scalar/​src/​Type.Scalar.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​property/​value-types/​src/​Type.Property.ValueTypes.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​property/​optionality/​src/​Type.Property.Optional.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​property/​nullable/​src/​Type.Property.Nullable.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​property/​additional-properties/​src/​Type.Property.AdditionalProperties.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​visibility/​src/​Type.Model.Visibility.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​usage/​src/​Type.Model.Usage.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​inheritance/​single-discriminator/​src/​Type.Model.Inheritance.SingleDiscriminator.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​inheritance/​recursive/​src/​Type.Model.Inheritance.Recursive.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​inheritance/​not-discriminated/​src/​Type.Model.Inheritance.NotDiscriminated.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​inheritance/​nested-discriminator/​src/​Type.Model.Inheritance.NestedDiscriminator.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​inheritance/​enum-discriminator/​src/​Type.Model.Inheritance.EnumDiscriminator.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​model/​empty/​src/​Type.Model.Empty.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​enum/​fixed/​src/​Type.Enum.Fixed.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​enum/​extensible/​src/​Type.Enum.Extensible.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​dictionary/​src/​Type.Dictionary.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​type/​array/​src/​Type.Array.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​streaming/​sse/​src/​Streaming.Sse.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​streaming/​jsonl/​src/​Streaming.Jsonl.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​special-words/​src/​SpecialWords.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​special-headers/​repeatability/​src/​SpecialHeaders.Repeatability.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​special-headers/​conditional-request/​src/​SpecialHeaders.ConditionalRequest.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​service/​multiple-services/​src/​Service.MultipleServices.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​server/​versions/​versioned/​src/​Server.Versions.Versioned.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​server/​versions/​not-versioned/​src/​Server.Versions.NotVersioned.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​server/​path/​single/​src/​Server.Path.Single.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​server/​path/​multiple/​src/​Server.Path.Multiple.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​server/​endpoint/​not-defined/​src/​Server.Endpoint.NotDefined.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​serialization/​encoded-name/​json/​src/​Serialization.EncodedName.Json.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​routes/​src/​Routes.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​response/​status-code-range/​src/​Response.StatusCodeRange.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​response/​body-or-no-content/​src/​Response.BodyOrNoContent.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​resiliency/​srv-driven/​v2/​src/​Resiliency.SrvDriven.V2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​resiliency/​srv-driven/​v1/​src/​Resiliency.SrvDriven.V1.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​payload/​xml/​src/​Payload.Xml.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​payload/​pageable/​src/​Payload.Pageable.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​payload/​multipart/​src/​Payload.MultiPart.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​payload/​media-type/​src/​Payload.MediaType.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​payload/​json-merge-patch/​src/​Payload.JsonMergePatch.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​payload/​head/​src/​Payload.Head.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​payload/​content-negotiation/​src/​Payload.ContentNegotiation.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​parameters/​spread/​src/​Parameters.Spread.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​parameters/​query/​src/​Parameters.Query.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​parameters/​path/​src/​Parameters.Path.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​parameters/​collection-format/​src/​Parameters.CollectionFormat.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​parameters/​body-root/​src/​Parameters.BodyRoot.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​parameters/​body-optionality/​src/​Parameters.BodyOptionality.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​parameters/​basic/​src/​Parameters.Basic.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​encode/​numeric/​src/​Encode.Numeric.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​encode/​duration/​src/​Encode.Duration.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​encode/​datetime/​src/​Encode.Datetime.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​encode/​bytes/​src/​Encode.Bytes.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​encode/​boolean/​src/​Encode.Boolean.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​encode/​array/​src/​Encode.Array.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​documentation/​src/​Documentation.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​client/​structure/​two-operation-group/​src/​Client.Structure.Service.TwoOperationGroup.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​client/​structure/​renamed-operation/​src/​Client.Structure.Service.Renamed.Operation.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​client/​structure/​multi-client/​src/​Client.Structure.Service.Multi.Client.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​client/​structure/​default/​src/​Client.Structure.Service.Default.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​client/​structure/​client-operation-group/​src/​Client.Structure.Service.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​azure/​client-generator-core/​response-replacement/​src/​_Specs_.Azure.ClientGenerator.Core.ResponseReplacement.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​azure/​client-generator-core/​response-as-bool/​src/​_Specs_.Azure.ClientGenerator.Core.ResponseAsBool.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​authentication/​union/​src/​Authentication.Union.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​authentication/​oauth2/​src/​Authentication.OAuth2.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​authentication/​http/​custom/​src/​Authentication.Http.Custom.csproj Updates runtime dependency.
TestProjects/​Spector/​http/​authentication/​api-key/​src/​Authentication.ApiKey.csproj Updates runtime dependency.
TestProjects/​Local/​Sample-TypeSpec/​src/​SampleTypeSpec.csproj Updates sample runtime dependency.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​XmlUnionPet.Serialization.cs Adds proxy-aware union payload reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​XmlAdvancedModel.Serialization.cs Adds proxy-aware XML child and collection reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​Wrapper.Serialization.cs Adds proxy-aware nested model reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​UnionPet.Serialization.cs Adds proxy-aware union variant reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​SerializePetsUnion.Serialization.cs Adds proxy-aware union payload reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​RoundTripModel.Serialization.cs Adds proxy-aware model property reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​RebasedUnionPet.Serialization.cs Adds proxy-aware rebased union reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​RebasedUnionB.Serialization.cs Adds proxy-aware leaf reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​RebasedUnionA.Serialization.cs Adds proxy-aware leaf reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​PageThing.Serialization.cs Adds proxy-aware page item reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​PagePreviewDetails.Serialization.cs Adds proxy-aware page item reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​OptionalNullableProperties.Serialization.cs Adds proxy routing after the null check.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​OptionalNullableContainer.Serialization.cs Adds proxy-aware child reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​NullableDynamicModel.Serialization.cs Adds proxy-aware dynamic child and collection reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​NestedMammal.Serialization.cs Adds proxy-aware union variant reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ListWithStringNextLinkResult.Serialization.cs Adds proxy-aware result item reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ListWithNextLinkResult.Serialization.cs Adds proxy-aware result item reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ListWithContinuationTokenResult.Serialization.cs Adds proxy-aware result item reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​ListWithContinuationTokenHeaderResponseResult.Serialization.cs Adds proxy-aware result item reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​LifecycleModel.Serialization.cs Adds proxy-aware preview model reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​DynamicModel.Serialization.cs Adds proxy-aware dynamic child and collection reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​DirectUnionPetContainer.Serialization.cs Adds proxy-aware union property reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​DirectUnionPet.Serialization.cs Adds proxy-aware union payload reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​DeserializerPet.Serialization.cs Adds proxy-aware union payload reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​DeserializePetsUnion.Serialization.cs Adds proxy-aware union payload reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​DescendantUnionPet.Serialization.cs Adds proxy-aware descendant reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Models/​CollisionPetsUnion.Serialization.cs Adds proxy-aware nested union reads.
TestProjects/​Local/​Sample-TypeSpec/​src/​Generated/​Internal/​ModelSerializationExtensions.cs Adds proxy routing, write resolution, and XML byte conversion.
TestProjects/​Local.Tests/​ModelProxyTests.cs Tests nested proxies, declining proxies, XML reads, and context eligibility.
Packages.Data.props Updates the shared runtime dependency.
Microsoft.TypeSpec.Generator/​src/​Primitives/​NewProjectScaffolding.cs Updates the dependency for newly scaffolded projects.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​TestData/​DiscriminatedUnionSerializationTests/​WritesContextualEnvelopeSerialization(type,data).cs Updates the envelope proxy-routing baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​TestData/​DiscriminatedUnionSerializationTests/​WritesContextualEnvelopeSerialization(kind,value).cs Updates the envelope proxy-routing baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​XmlDeserializationTests.cs Adds dual-format XML routing coverage.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​XmlDeserializationTests/​XmlDeserializationMethodRoutesDualFormatNestedModelOnlyForXmlFormat.cs Adds the explicit-XML routing baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​XmlDeserializationTests/​XmlDeserializationMethodHandlesNestedModel.cs Updates the nested XML routing baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​DynamicModelSerializationTests/​DeserializeModelWithAP.cs Updates nested model routing in the additional-properties baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​DynamicModelSerializationTests/​DeserializeModelPropertyType.cs Updates the model property routing baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​DynamicModelSerializationTests/​DeserializeDictionaryProperties.cs Updates the dictionary value routing baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​DynamicModelSerializationTests/​DeserializeArrayProperties.cs Updates the array item routing baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​TestData/​DeserializationTests/​NestedModelReadsRouteThroughModelProxies.cs Adds property, list, and dictionary routing baselines.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​MrwSerializationTypeDefinitions/​DeserializationTests.cs Tests nested routing and unchanged discriminator dispatch.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​TestData/​ModelSerializationExtensionsDefinitionTests/​ValidateWriteObjectValueXmlIncludesFileBinaryContentCase.cs Updates JSON write resolution in the XML-enabled baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​TestData/​ModelSerializationExtensionsDefinitionTests/​ValidateWriteObjectValueResolvesModelProxies.cs Adds the proxy-aware JSON writer baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​TestData/​ModelSerializationExtensionsDefinitionTests/​ValidateWriteObjectValueIncludesFileBinaryContentCase.cs Updates the writer baseline for proxy resolution.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​TestData/​ModelSerializationExtensionsDefinitionTests/​ValidateShouldReadThroughProxyMethod.cs Adds the proxy eligibility helper baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​TestData/​ModelSerializationExtensionsDefinitionTests/​ValidateGetUtf8BytesMethodsWithXmlModels.cs Adds the XML byte-conversion baseline.
Microsoft.TypeSpec.Generator.ClientModel/​test/​Providers/​Definitions/​ModelSerializationExtensionsDefinitionTests.cs Adds helper and writer generation tests.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Snippets/​XElementSnippets.cs Adds XML byte-conversion invocation support.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Snippets/​ModelSerializationExtensionsSnippets.Xml.cs Adds the XML byte-conversion helper call.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Snippets/​ModelSerializationExtensionsSnippets.cs Adds proxy eligibility helper invocation support.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Snippets/​ModelReaderWriterOptionsSnippets.cs Adds proxy count and resolution expressions.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​MrwSerializationTypeDefinition.Xml.cs Generates conditional proxy-aware XML reads.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​MrwSerializationTypeDefinition.cs Generates proxy-aware nested JSON reads.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​ModelSerializationExtensionsDefinition.Xml.cs Generates XML element-to-bytes conversion.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​ModelSerializationExtensionsDefinition.cs Generates proxy eligibility checks and JSON write resolution.
Microsoft.TypeSpec.Generator.ClientModel/​src/​Providers/​DiscriminatedUnionSerialization.cs Routes union payload reads through proxy-aware generation.

Review effort: Balanced

if (prop.NameEquals("requiredModel"u8))
{
requiredModel = Thing.DeserializeThing(prop.Value, options);
requiredModel = options.ShouldReadThroughProxy(typeof(Thing), SampleTypeSpecContext.Default) ? ModelReaderWriter.Read<Thing>(prop.Value.GetUtf8Bytes(), options, SampleTypeSpecContext.Default) : Thing.DeserializeThing(prop.Value, options);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what about the implicit and explicit operators used in the generated clients? The current SCM proposal has a ModelProxies property on ClientPipelineOptions, which means the generated clients need to pass those proxies through ModelReaderWriterOptions to the serialization / deserialization of models in operations. Since we can't pass an options param through the existing explicit and implicit operators, can we just add internal ToBinaryContent and FromClientResult methods that call the corresponding mrw factory methods with the client level options ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in this PR:

  • Client options: clients built on System.ClientModel get a _modelReaderWriterOptions field, set in the constructor from options.ModelProxies.ToWireOptions(). With no proxies configured this returns the shared WireOptions, so the no-proxy path is unchanged. Creating the pipeline freezes the client options, so the copy can't go stale. The field starts as WireOptions, so clients created through the mocking constructor still work.
  • Subclients: they receive the parent's options through their internal constructor.
  • Models and convenience methods: root input models get internal BinaryContent ToBinaryContent(ModelReaderWriterOptions), and root output models get internal static T FromClientResult(ClientResult, ModelReaderWriterOptions) (JSON, XML, dual-format, dynamic). Convenience methods use these instead of the operators; the operators stay for compatibility. Dual-format XML bodies pass _modelReaderWriterOptions.ToXmlOptions().
  • Paging: collection results take the options in their constructor and use FromClientResult for next-link/token extraction and items.
  • Other reads: XML list responses, collection/dictionary responses, envelope unions, and SSE/JSON Lines streaming also read with the client's options.
  • Azure: pipelines whose options don't expose model proxies (ClientPipelineApi.GetModelProxies returns null, e.g. Azure.Core until [Core] Add proxy registration to Azure.Core client options Azure/azure-sdk-for-net#64141) keep the current output. ClientWithoutModelProxiesKeepsCastConversions covers this.
  • Runtime tests: Local.Tests register proxies on SampleTypeSpecClientOptions.ModelProxies and cover responses, request bodies, paging, and a subclient XML response.

Not covered yet: multipart bodies and request collections serialized through BinaryContentHelper still use WireOptions; I'll follow up. This overlaps with #11390.

🤖 Generated by Jorge's Copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We should cover those helper methods as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ea8dfab. When the pipeline supports model proxies:

  • BinaryContentHelper: the overloads that write items take an optional ModelReaderWriterOptions options = null, defaulting to WireOptions so existing callers and custom code keep compiling. These are FromEnumerable<T>(IEnumerable<T>), FromEnumerable<T>(ReadOnlySpan<T>), FromDictionary<TValue>, and the XML FromEnumerable<T>(…, rootNameHint, childNameHint). Convenience methods pass _modelReaderWriterOptions; BinaryData collections keep their own overloads, which don't write models.
  • Multipart: ToMultipartFormContent(ModelReaderWriterOptions options) writes the model parts (single, list, and dictionary) with the client's options, and convenience methods pass _modelReaderWriterOptions.

Tests: generator TestData snapshots for the helper signatures, the convenience call sites (model list/dictionary vs BinaryData list), and multipart serialization, plus no-proxy pipeline cases. At runtime, ClientMultipartBodiesUseClientOptionsProxies uploads a Cat and checks that its list and dictionary model parts go through the proxy.

🤖 Generated by Jorge's Copilot

Address PR feedback on model proxy routing:
- Read dual-format nested XML models through their proxies with XML options so XML-only parents read with "W" still apply registered proxies.
- Make ShouldReadThroughProxy generic and cache the per-type context lookup.
- Copy ClientPipelineOptions.ModelProxies into a client-level ModelReaderWriterOptions field and pass it to subclients and paging collection results.
- Add internal ToBinaryContent(ModelReaderWriterOptions) and FromClientResult(ClientResult, ModelReaderWriterOptions) conversions and use them in convenience methods instead of the cast operators.
- Keep the existing output for pipelines whose options don't carry model proxies.

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

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 are reported for the model-proxy support changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

/// <summary>
/// Gets whether generated clients route model reads and writes through the proxies configured on their options.
/// </summary>
public bool SupportsModelProxies => GetModelProxies(Empty) is not null;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why are we passing the Empty statemetn to this ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It was a probe: SupportsModelProxies called GetModelProxies(Empty) only to check whether the override returned an expression. I replaced it with an explicit public virtual bool SupportsModelProxies => false; that pipelines override, so there's no probe anymore. The call sites also no longer use ValueExpression.Empty.ToApi<ClientPipelineApi>(); they read TypeFactory.ClientPipelineApi.ToExpression() instead.

🤖 Generated by Jorge's Copilot

public override ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies)
=> Static<ClientPipeline>().Invoke(nameof(ClientPipeline.Create), [options, New.Array(ScmCodeModelGenerator.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType), perRetryPolicies, New.Array(ScmCodeModelGenerator.Instance.TypeFactory.ClientPipelineApi.PipelinePolicyType)]).As<ClientPipeline>();

public override ValueExpression? GetModelProxies(ValueExpression clientOptions)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think we need to account for downstream emitters that may have different client pipeline types. Can we not just add this as an api on clientpipelineapi?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's now on ClientPipelineApi as two virtual members, and ClientPipelineProvider is just the System.ClientModel override:

  • bool SupportsModelProxies (default false)
  • ValueExpression GetModelReaderWriterOptions(ValueExpression clientOptions), which returns the options the client stores in _modelReaderWriterOptions (default WireOptions)

I changed the hook to return the options rather than the proxy list. The Azure.Core bridge (Azure/azure-sdk-for-net#64141) exposes ClientOptions.ModelReaderWriterOptions, not a ModelProxies list, so the Azure generator's HttpPipelineProvider can override both members to build the client's options from that property. That mirrors how ClientDiagnostics is already built from the options. They're virtual rather than abstract so the Azure generator keeps compiling and producing today's output until Azure.Core ships the bridge. ClientPipelineApiTests covers both the default and a custom pipeline override.

🤖 Generated by Jorge's Copilot

Assert.IsNotNull(method, "ToBinaryContent method should exist on Tree");

var binaryContent = (BinaryContent)method!.Invoke(tree, new object[] { "J" })!;
var binaryContent = (BinaryContent)method!.Invoke(tree, new object[] { ModelReaderWriterOptions.Json })!;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

why did this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

With model proxies supported, Tree no longer generates the internal ToBinaryContent(string format) helper. It generates ToBinaryContent(ModelReaderWriterOptions options) instead, so convenience methods can pass the client's options (_modelReaderWriterOptions for JSON, _modelReaderWriterOptions.ToXmlOptions() for XML). These tests call the internal method through reflection, so they now pass ModelReaderWriterOptions.Json/.Xml instead of "J"/"X"; the media-type assertions are unchanged. Pipelines without model proxies still generate the format-string overload, which TestBuildToBinaryContentMethod_DualFormatModel_MethodGenerated covers.

🤖 Generated by Jorge's Copilot

/// <summary> XML-only container whose nested tree also supports JSON. </summary>
/// <param name="tree"> The nested tree. </param>
/// <returns> A new <see cref="SampleTypeSpec.XmlTreeContainer"/> instance for mocking. </returns>
public static XmlTreeContainer XmlTreeContainer(Tree tree = default)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

why is this suddenly added to the model factory ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It came from the XmlTreeContainer fixture I'd added to Sample-TypeSpec; as a new public output model it got a model factory method. I removed the fixture in ea8dfab, so the model factory matches main again.

🤖 Generated by Jorge's Copilot

Comment on lines +1015 to +1028

@doc("Get an XML-only container that nests a tree, which also supports JSON")
@get
@route("/tree/container")
getTreeContainer(): {
@header contentType: "application/xml";
@body body: XmlTreeContainer;
};
}

@doc("XML-only container whose nested tree also supports JSON")
model XmlTreeContainer {
@doc("The nested tree")
tree: Tree;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think we need another XML case. Simply add a new unit test that uses TestData for validation if we are trying to validate a new scenario

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I removed the getTreeContainer operation and the XmlTreeContainer model from Sample-TypeSpec, along with the Local.Tests runtime test that used them; Sample-TypeSpec.tsp now matches main. The generator unit test XmlDeserializationMethodReadsDualFormatNestedModelWithXmlOptions (TestData baseline) still covers the scenario: it builds an XML-only model with a dual-format nested model and checks that the proxy read uses options.ToXmlOptions().

🤖 Generated by Jorge's Copilot

{
Pipeline = ClientPipeline.Create(options, Array.Empty<PipelinePolicy>(), new PipelinePolicy[] { new UserAgentPolicy(typeof(SampleTypeSpecClient).Assembly) }, Array.Empty<PipelinePolicy>());
}
_modelReaderWriterOptions = options.ModelProxies.ToWireOptions();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

where does ToWireOptions come from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's a generated internal extension method in ModelSerializationExtensions (src/Generated/Internal/ModelSerializationExtensions.cs): ToWireOptions(this IList<ModelProxy> modelProxies). It returns the shared WireOptions when no proxies are registered; otherwise it returns a new "W" options instance with the proxies copied. It's only generated when the pipeline supports model proxies. As of ea8dfab, the constructor line comes from ClientPipelineApi.GetModelReaderWriterOptions(options), which the System.ClientModel pipeline implements with this helper.

🤖 Generated by Jorge's Copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we should pull the latest from main and address the merge conflicts

…rp-route-nested-model-mrw-proxies

Pass the client's model reader writer options to the SSE event-subtype parser added on main.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- Replace the ClientPipelineApi.GetModelProxies probe with virtual SupportsModelProxies and GetModelReaderWriterOptions(clientOptions) hooks so downstream pipelines, such as Azure.Core's, can build the client's options from their own client options.
- Pass the client's options to the BinaryContentHelper overloads that write models (as an optional parameter) and to ToMultipartFormContent.
- Remove the XmlTreeContainer Sample-TypeSpec fixture; the scenario stays covered by generator TestData tests.

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

This branch has not been deployed

No deployments
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.

2 participants