Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Issue reported that extensible string enums in collections generated incorrect serialization code (ToSerialString() / ToEnumName() extension methods) instead of the correct pattern (ToString() / constructor).

Investigation Result: The bug does not reproduce in the current codebase. The serialization logic correctly handles extensible enums:

  • MrwSerializationTypeDefinition.SerializeJsonValueCore() uses ToString() for string-based extensible enums
  • CSharpTypeSnippets.ToEnum() uses constructor for extensible enums (structs)

Changes

  • Added CreateModelWithExtensibleStringEnumCollection() helper to create test fixtures
  • Added TestArrayOfExtensibleStringEnum_UsesCorrectSerialization - validates item.ToString() pattern
  • Added TestArrayOfExtensibleStringEnum_UsesCorrectDeserialization - validates new EnumType(...) pattern

Expected generated code (which is what the current implementation produces):

// Serialization - extensible enum uses ToString()
foreach (var item in EnumCollection)
{
    writer.WriteStringValue(item.ToString());
}

// Deserialization - extensible enum uses constructor
array.Add(new TestEnum(item.GetString()));
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: Missing Extension Methods for Extensible Enums in Collections</issue_title>
<issue_description>### Describe the bug


Bug 1: Missing Extension Methods for Extensible Enums in Collections

Description

The TypeSpec C# generator produces serialization code that calls ToSerialString() and To<EnumName>() extension methods for extensible enum types (readonly structs with string backing), but these extension methods are never generated.

Reproduction

When generating code for CjkBigramTokenFilterScripts (an extensible enum defined as a readonly partial struct), the generated serialization file CjkBigramTokenFilter.Serialization.cs contains:

// Line 46 - serialization
writer.WriteStringValue(item.ToSerialString());

// Line 107 - deserialization  
array.Add(item.GetString().ToCjkBigramTokenFilterScripts());

However, the generated CjkBigramTokenFilterScripts.cs file only contains the struct definition with a ToString() method no ToSerialString() or ToCjkBigramTokenFilterScripts() extension methods are generated.

Expected Behavior

For extensible enums (readonly structs), the generator should either:

  1. Generate the extension methods in a separate *.Serialization.cs file (like it does for fixed enums), OR
  2. Use ToString() and the constructor directly in the serialization code:
    writer.WriteStringValue(item.ToString());
    array.Add(new CjkBigramTokenFilterScripts(item.GetString()));

Workaround

Manually create the missing extension methods:

internal static class CjkBigramTokenFilterScriptsExtensions
{
    public static string ToSerialString(this CjkBigramTokenFilterScripts value) => value.ToString();
    public static CjkBigramTokenFilterScripts ToCjkBigramTokenFilterScripts(this string value) => new(value);
}

Affected Types

This likely affects all extensible enums used in collection properties. Known affected types:

  • CjkBigramTokenFilterScripts
  • EdgeNGramTokenizer

Reproduction

steps: Regenerate SDK on Azure.Search.Documents.

Example PREVIOUS autorest file: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/search/Azure.Search.Documents/src/Generated/Models/CjkBigramTokenFilterScripts.Serialization.cs

Repo: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/search/Azure.Search.Documents/src

Checklist

<agent_instructions>### Root Cause

The bug is in the C# generator (http-client-csharp). For string-based extensible enums, the generator correctly uses ToString() for direct property serialization, but when the enum is inside a collection (IList<T>, arrays), it incorrectly generates calls to ToSerialString() and To<EnumName>() extension methods that are never generated.

Key Files

  1. packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Snippets/CSharpTypeSnippets.cs

    • ToSerial() and ToEnum() methods handle single values correctly
  2. packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/

    • Find where collection/array serialization code is generated
    • Look for code paths that don't use CSharpTypeSnippets.ToSerial() for collection items

Expected Fix

For string-based extensible enums in collections:

  • Serialization: Use item.ToString() instead of item.ToSerialString()
  • Deserialization: Use new EnumName(value) instead of value.To<EnumName>()

Validation

  • Generated code for IList<StringExtensibleEnum> must compile
  • Existing tests must pass
  • Reference: TestData/SerializationCustomizationTests/CanChangeListOfEnumPropToListOfExtensibleEnum.cs shows the correct pattern</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@efrainretana Example repro: https://github.com/Azure/azure-sdk-for-net/blob/3df80e71cc22102f60910e9d188ca864ea18849d/sdk/search/Azure.Search.Documents/src/Generated/Models/CjkBigramTokenFilter.Serialization.cs#L111

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix missing extension methods for extensible enums Add regression tests for extensible string enum collection serialization Jan 27, 2026
Copilot AI requested a review from JonathanCrd January 27, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Missing Extension Methods for Extensible Enums in Collections

2 participants