Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,17 @@ private protected string[] GetGeneratedValueNames(
continue;
}

var normalizedName = name.NormalizeCSharpAcronyms();
var normalizedName = NormalizeValueName(inputValues[i].Name, name);
normalizedNames[i] = lastContractNames.Contains(name, StringComparer.Ordinal) && !IsCustomizedValueName(normalizedName)
? name
: normalizedName;
}
return normalizedNames;
}

private protected virtual string NormalizeValueName(string inputName, string generatedName) =>
generatedName.NormalizeCSharpAcronyms();

private protected virtual bool IsCustomizedValueName(string name) =>
GetMemberSuppressionAttributes().Any(a => a.ConstructorArguments.Length > 0 &&
a.ConstructorArguments[0].Value is string suppressedName && suppressedName == name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.TypeSpec.Generator.EmitterRpc;
using Microsoft.TypeSpec.Generator.Expressions;
using Microsoft.TypeSpec.Generator.Input;
Expand All @@ -19,7 +20,7 @@

namespace Microsoft.TypeSpec.Generator.Providers
{
public class ExtensibleEnumProvider : EnumProvider
public partial class ExtensibleEnumProvider : EnumProvider
{
private readonly IReadOnlyList<InputEnumTypeValue> _allowedValues;
private readonly TypeSignatureModifiers _modifiers;
Expand All @@ -43,6 +44,19 @@ internal ExtensibleEnumProvider(InputEnumType input, TypeProvider? declaringType

private readonly FieldProvider _valueField;

private protected override string NormalizeValueName(string inputName, string generatedName)
{
// Use the input name to find words before ToIdentifierName removes separators.
var name = UppercaseWords().Replace(inputName, static match => match.Value[..1] + match.Value[1..].ToLowerInvariant());
return base.NormalizeValueName(inputName, name == inputName ? generatedName : name.ToIdentifierName().NormalizeCSharpUrlSuffix());
}

[GeneratedRegex("""
(?<!\p{Lu})\p{Lu}{3,}(?=\p{Lu}\p{Ll}|[^\p{L}\p{M}\p{N}]|$) # Long uppercase runs, e.g. HTTP in HTTPStatus
| (?<![\p{L}\p{M}\p{N}])\p{Lu}{2}(?![\p{L}\p{M}\p{N}]) # Standalone two-letter words, e.g. OR
""", RegexOptions.IgnorePatternWhitespace)]
private static partial Regex UppercaseWords();

private protected override bool IsCustomizedValueName(string name)
{
var fieldName = name + "Value";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1315,11 +1315,14 @@ public void BuildEnumType_FixedIntEnum_IsExactNameValuePreserved()
Assert.AreEqual("snake_case_value", fields[1].Name);
}

// Validates that an IsExactName-marked value on an extensible string-based enum preserves
// its exact-case name (skipping .ToIdentifierName()) on both the generated field (with
// the `Value` suffix appended) and the generated public property.
[TestCase]
public void BuildEnumType_ExtensibleStringEnum_IsExactNameValuePreserved()
[TestCase("AND", false, "And")]
[TestCase("OR", false, "Or")]
[TestCase("NOT_FOUND", false, "NotFound")]
[TestCase("HTTPStatus", false, "HttpStatus")]
[TestCase("CallbackURL", false, "CallbackUri")]
[TestCase("AND", true, "AND")]
[TestCase("snake_case_value", true, "snake_case_value")]
public void BuildEnumType_ExtensibleStringEnum_MemberCasing(string name, bool isExactName, string expectedName)
{
MockHelpers.LoadMockGenerator(createCSharpTypeCore: (inputType) => typeof(string));

Expand All @@ -1330,7 +1333,7 @@ public void BuildEnumType_ExtensibleStringEnum_IsExactNameValuePreserved()
enumValues,
isExtensible: true);
enumValues.Add(InputFactory.EnumMember.String("One", "1", enumType));
enumValues.Add(InputFactory.EnumMember.String("snake_case_value", "2", enumType, isExactName: true));
enumValues.Add(InputFactory.EnumMember.String(name, name, enumType, isExactName: isExactName));

var enumProvider = EnumProvider.Create(enumType);
var fields = enumProvider.Fields;
Expand All @@ -1340,12 +1343,12 @@ public void BuildEnumType_ExtensibleStringEnum_IsExactNameValuePreserved()
Assert.AreEqual(3, fields.Count);
Assert.AreEqual("_value", fields[0].Name);
Assert.AreEqual("OneValue", fields[1].Name);
// exact-name value: name preserved verbatim, with the `Value` suffix appended
Assert.AreEqual("snake_case_valueValue", fields[2].Name);
Assert.AreEqual(expectedName + "Value", fields[2].Name);
Assert.AreEqual(name, ((LiteralExpression)fields[2].InitializationValue!).Literal);

Assert.AreEqual(2, properties.Count);
Assert.AreEqual("One", properties[0].Name);
Assert.AreEqual("snake_case_value", properties[1].Name);
Assert.AreEqual(expectedName, properties[1].Name);
}

[TestCase(false, "CallbackUrl", false, "CallbackUri")]
Expand Down