-
Notifications
You must be signed in to change notification settings - Fork 334
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:
- Generate the extension methods in a separate
*.Serialization.csfile (like it does for fixed enums), OR - 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:
CjkBigramTokenFilterScriptsEdgeNGramTokenizer
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
- Follow our Code of Conduct
- Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion.
- The provided reproduction is a minimal reproducible example of the bug.