From af7f283967493b106fdf5b1cefe7199f70ab0073 Mon Sep 17 00:00:00 2001 From: jolov Date: Fri, 20 Mar 2026 13:15:20 -0700 Subject: [PATCH] fix: fix dictionary deserialization crash for specs with example values The dictionary TryReadComplexType overload (added in #9983) manually iterated dictionary entries but treated \ metadata as regular dictionary keys. When a dictionary value object contains \ (for reference tracking), the manual iteration read it as a dictionary entry key and tried to parse the reference ID string as an InputExampleValue, corrupting the reader position. Fix: skip \ metadata entries (via TryReadReferenceId) when iterating dictionary keys, consistent with how other converters handle reference tracking metadata. This preserves the manual iteration needed to handle \$-prefixed user keys (like \) while correctly ignoring serialization metadata. Fixes regression from #9983. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../InputTypes/Serialization/Utf8JsonReaderExtensions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/Utf8JsonReaderExtensions.cs b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/Utf8JsonReaderExtensions.cs index 12e488d7c9c..00124c64137 100644 --- a/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/Utf8JsonReaderExtensions.cs +++ b/packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.Input/src/InputTypes/Serialization/Utf8JsonReaderExtensions.cs @@ -164,9 +164,15 @@ public static bool TryReadComplexType(this ref Utf8JsonReader reader, string throw new JsonException(); } reader.Read(); + string? id = null; var result = new Dictionary(); while (reader.TokenType != JsonTokenType.EndObject) { + // Skip $id metadata (reference tracking), just like TryReadReferenceId does + if (reader.TryReadReferenceId(ref id)) + { + continue; + } var key = reader.GetString() ?? throw new JsonException("Dictionary key cannot be null"); reader.Read(); var item = reader.ReadWithConverter(options);