Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to properly honor naming policy when serializing flag enums #36726

Merged
merged 6 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -16,9 +16,11 @@ internal class EnumConverter<T> : JsonConverter<T>
// Odd type codes are conveniently signed types (for enum backing types).
private static readonly string? s_negativeSign = ((int)s_enumTypeCode % 2) == 0 ? null : NumberFormatInfo.CurrentInfo.NegativeSign;
Copy link
Member

@GrabYourPitchforks GrabYourPitchforks Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's incorrect to cache any thread-local state (such as information about the thread-current culture) into a global static. The end result of this is that the very first time this code is executed, the executing thread's negative sign will be read and will be applied to all subsequent operations, regardless of what culture the other threads are running as.

If you wanted this to be properly culture-aware, you need to query the current thread's negative sign on every single call to IsValidIdentifier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be incorrect even if it was a thread-static, of course.


private const string ValueSeparator = ", ";

private readonly EnumConverterOptions _converterOptions;
private readonly JsonNamingPolicy _namingPolicy;
private readonly ConcurrentDictionary<string, string>? _nameCache;
private readonly ConcurrentDictionary<string, JsonEncodedText>? _nameCache;

public override bool CanConvert(Type type)
{
Expand All @@ -35,7 +37,7 @@ public EnumConverter(EnumConverterOptions options, JsonNamingPolicy? namingPolic
_converterOptions = options;
if (namingPolicy != null)
{
_nameCache = new ConcurrentDictionary<string, string>();
_nameCache = new ConcurrentDictionary<string, JsonEncodedText>();
}
else
{
Expand Down Expand Up @@ -158,15 +160,15 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
if (_converterOptions.HasFlag(EnumConverterOptions.AllowStrings))
{
string original = value.ToString();
if (_nameCache != null && _nameCache.TryGetValue(original, out string? transformed))
if (_nameCache != null && _nameCache.TryGetValue(original, out JsonEncodedText transformed))
{
writer.WriteStringValue(transformed);
return;
}

if (IsValidIdentifier(original))
{
transformed = _namingPolicy.ConvertName(original);
transformed = FormatEnumValue(original);
writer.WriteStringValue(transformed);
if (_nameCache != null)
{
Expand Down Expand Up @@ -212,5 +214,35 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
break;
}
}

private JsonEncodedText FormatEnumValue(string value)
{
string converted;

if (!value.Contains(ValueSeparator))
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
converted = _namingPolicy.ConvertName(value);
}
else
{
// todo: optimize implementation here by leveraging https://github.com/dotnet/runtime/issues/934.
string[] enumValues = value.Split(
#if BUILDING_INBOX_LIBRARY
ValueSeparator
#else
new string[] { ValueSeparator }, StringSplitOptions.None
#endif
);

for (int i = 0; i < enumValues.Length; i++)
{
enumValues[i] = _namingPolicy.ConvertName(enumValues[i]);
}

converted = string.Join(ValueSeparator, enumValues);
}

return JsonEncodedText.Encode(converted);
layomia marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ public void ConvertFileAttributes()
options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter(allowIntegerValues: false));
Assert.Throws<JsonException>(() => JsonSerializer.Serialize((FileAttributes)(-1), options));

// Flag values honor naming policy correctly
options = new JsonSerializerOptions();
options.Converters.Add(new JsonStringEnumConverter(new SimpleSnakeCasePolicy()));

json = JsonSerializer.Serialize(
FileAttributes.Directory | FileAttributes.Compressed | FileAttributes.IntegrityStream,
options);
Assert.Equal(@"""directory, compressed, integrity_stream""", json);

json = JsonSerializer.Serialize(FileAttributes.Compressed & FileAttributes.Device, options);
Assert.Equal(@"0", json);

json = JsonSerializer.Serialize(FileAttributes.Directory & FileAttributes.Compressed | FileAttributes.IntegrityStream, options);
Assert.Equal(@"""integrity_stream""", json);
}

public class FileState
Expand Down