microsoft / microsoft/typespec

Expose reusable collection deserialization helper for generator extensions

Open
#10,887 1 comment 1 reaction 0 assignees View on GitHub
emitter:client:csharp feature
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

## Context

The Azure SDK for .NET management-plane generator needs to deserialize top-level response bodies and LRO IOperationSource results whose C# type is a framework collection, for example:

- TypeSpec Record -> C# IDictionary
- LRO result Foo[] -> C# IList / IReadOnlyList

These collection interface types cannot be deserialized by calling ModelReaderWriter.Read(...) on the collection type itself because they do not implement IPersistableModel.

## Existing behavior

MTG already emits correct collection deserialization loops for generated model properties. For example, generated model serialization uses explicit Dictionary<,> / List<> construction with oreach over JsonElement.EnumerateObject() or EnumerateArray(), then delegates leaf values to type-specific deserialization.

However, that logic appears to live in internal/private model serialization implementation details (for example in MrwSerializationTypeDefinition) and is not exposed as a reusable statement-producing helper.

The public TypeFactory.DeserializeJsonValue(...) API handles a single leaf value expression, but it returns a ValueExpression; it cannot emit the multi-statement collection block needed for top-level collection values (declare, null guard, oreach, assign/return).

## Request

Please expose a reusable collection deserialization helper/API that generator extensions can call for arbitrary JSON elements / response bodies, ideally something statement-producing, such as:

`csharp
IReadOnlyList BuildDeserializeJsonValueStatements(
CSharpType type,
ValueExpression jsonElement,
string valueName,
out ValueExpression value);
`

or an equivalent API that supports:

- Dictionary and list/interface collection types.
- Nested collections.
- Delegating leaf values through TypeFactory.DeserializeJsonValue(...) or equivalent existing leaf logic.
- Both top-level response values and generated model property values.

## Why

Azure SDK for .NET currently needs a local bridge helper in the management generator to cover this gap for top-level response/LRO collection deserialization. Having this exposed in MTG would avoid duplicating collection deserialization logic in downstream generators and keep behavior aligned with generated model serialization.

Related downstream PR: https://github.com/Azure/azure-sdk-for-net/pull/59628

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.