-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add gRPC JSON transcoding option to remove enum prefix #62873
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
Merged
+343
−74
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
...ranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumNameHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Google.Protobuf.Reflection; | ||
|
||
namespace Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal.Json; | ||
|
||
internal static class JsonNamingHelpers | ||
{ | ||
private static readonly ConcurrentDictionary<Type, EnumMapping> _enumMappings = new ConcurrentDictionary<Type, EnumMapping>(); | ||
|
||
internal static EnumValueDescriptor? GetEnumFieldReadValue(EnumDescriptor enumDescriptor, string value, GrpcJsonSettings settings) | ||
{ | ||
string resolvedName; | ||
if (settings.RemoveEnumPrefix) | ||
{ | ||
var nameMapping = GetEnumMapping(enumDescriptor); | ||
if (!nameMapping.RemoveEnumPrefixMapping.TryGetValue(value, out var n)) | ||
{ | ||
return null; | ||
} | ||
|
||
resolvedName = n; | ||
} | ||
else | ||
{ | ||
resolvedName = value; | ||
} | ||
|
||
return enumDescriptor.FindValueByName(resolvedName); | ||
} | ||
|
||
internal static string? GetEnumFieldWriteName(EnumDescriptor enumDescriptor, object value, GrpcJsonSettings settings) | ||
{ | ||
var enumMapping = GetEnumMapping(enumDescriptor); | ||
|
||
// If this returns false, name will be null, which is what we want. | ||
if (!enumMapping.WriteMapping.TryGetValue(value, out var mapping)) | ||
{ | ||
return null; | ||
} | ||
|
||
return settings.RemoveEnumPrefix ? mapping.RemoveEnumPrefixName : mapping.OriginalName; | ||
} | ||
|
||
private static EnumMapping GetEnumMapping(EnumDescriptor enumDescriptor) | ||
{ | ||
return _enumMappings.GetOrAdd( | ||
enumDescriptor.ClrType, | ||
static (t, descriptor) => GetEnumMapping(descriptor.Name, t), | ||
enumDescriptor); | ||
} | ||
|
||
private static EnumMapping GetEnumMapping(string enumName, Type enumType) | ||
{ | ||
var nameMappings = enumType.GetTypeInfo().DeclaredFields | ||
.Where(f => f.IsStatic) | ||
.Where(f => f.GetCustomAttributes<OriginalNameAttribute>().FirstOrDefault()?.PreferredAlias ?? true) | ||
.Select(f => | ||
{ | ||
// If the attribute hasn't been applied, fall back to the name of the field. | ||
var fieldName = f.GetCustomAttributes<OriginalNameAttribute>().FirstOrDefault()?.Name ?? f.Name; | ||
|
||
return new NameMapping | ||
{ | ||
Value = f.GetValue(null)!, | ||
OriginalName = fieldName, | ||
RemoveEnumPrefixName = GetEnumValueName(enumName, fieldName) | ||
}; | ||
}) | ||
.ToList(); | ||
|
||
var writeMapping = nameMappings.ToDictionary(m => m.Value, m => m); | ||
|
||
// Add original names as fallback when mapping enum values with removed prefixes. | ||
// There are added to the dictionary first so they are overridden by the mappings with removed prefixes. | ||
var removeEnumPrefixMapping = nameMappings.ToDictionary(m => m.OriginalName, m => m.OriginalName); | ||
|
||
// Protobuf codegen prevents collision of enum names when the prefix is removed. | ||
// For example, the following enum will fail to build because both fields would resolve to "OK": | ||
// | ||
// enum Status { | ||
// STATUS_OK = 0; | ||
// OK = 1; | ||
// } | ||
// | ||
// Tooling error message: | ||
// ---------------------- | ||
// Enum name OK has the same name as STATUS_OK if you ignore case and strip out the enum name prefix (if any). | ||
// (If you are using allow_alias, please assign the same number to each enum value name.) | ||
// | ||
// Just in case it does happen, map to the first value rather than error. | ||
foreach (var item in nameMappings.GroupBy(m => m.RemoveEnumPrefixName).Select(g => KeyValuePair.Create(g.Key, g.First().OriginalName))) | ||
{ | ||
removeEnumPrefixMapping[item.Key] = item.Value; | ||
} | ||
|
||
return new EnumMapping { WriteMapping = writeMapping, RemoveEnumPrefixMapping = removeEnumPrefixMapping }; | ||
} | ||
|
||
// Remove the prefix from the specified value. Ignore case and underscores in the comparison. | ||
private static string TryRemovePrefix(string prefix, string value) | ||
{ | ||
var normalizedPrefix = prefix.Replace("_", string.Empty, StringComparison.Ordinal).ToLowerInvariant(); | ||
|
||
var prefixIndex = 0; | ||
var valueIndex = 0; | ||
|
||
while (prefixIndex < normalizedPrefix.Length && valueIndex < value.Length) | ||
{ | ||
if (value[valueIndex] == '_') | ||
{ | ||
valueIndex++; | ||
continue; | ||
} | ||
|
||
if (char.ToLowerInvariant(value[valueIndex]) != normalizedPrefix[prefixIndex]) | ||
{ | ||
return value; | ||
} | ||
|
||
prefixIndex++; | ||
valueIndex++; | ||
} | ||
|
||
if (prefixIndex < normalizedPrefix.Length) | ||
{ | ||
return value; | ||
} | ||
|
||
while (valueIndex < value.Length && value[valueIndex] == '_') | ||
{ | ||
valueIndex++; | ||
} | ||
|
||
return valueIndex == value.Length ? value : value.Substring(valueIndex); | ||
} | ||
|
||
private static string GetEnumValueName(string enumName, string valueName) | ||
{ | ||
var result = TryRemovePrefix(enumName, valueName); | ||
JamesNK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Prefix name starting with a digit with an underscore to ensure it is a valid identifier. | ||
return result.Length > 0 && char.IsDigit(result[0]) | ||
? $"_{result}" | ||
: result; | ||
} | ||
|
||
private sealed class EnumMapping | ||
{ | ||
public required Dictionary<object, NameMapping> WriteMapping { get; init; } | ||
public required Dictionary<string, string> RemoveEnumPrefixMapping { get; init; } | ||
} | ||
|
||
private sealed class NameMapping | ||
{ | ||
public required object Value { get; init; } | ||
public required string OriginalName { get; init; } | ||
public required string RemoveEnumPrefixName { get; init; } | ||
} | ||
Comment on lines
+152
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make the private sealed record EnumMapping(
Dictionary<object, NameMapping> WriteMapping,
Dictionary<string, string> RemoveEnumPrefixMapping);
private sealed record NameMapping(
object Value,
string OriginalName,
string RemoveEnumPrefixName); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. But classes work fine here. And records are bloated when doing AOT. |
||
} |
2 changes: 2 additions & 0 deletions
2
...rpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.PropertyNameCaseInsensitive.get -> bool | ||
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.PropertyNameCaseInsensitive.set -> void | ||
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.RemoveEnumPrefix.get -> bool | ||
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.RemoveEnumPrefix.set -> void |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.