Skip to content

Commit 2a5f3a0

Browse files
committed
Update
1 parent 1e74c69 commit 2a5f3a0

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
lines changed

src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/GrpcJsonSettings.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,26 @@ public sealed class GrpcJsonSettings
3434
public bool WriteIndented { get; set; }
3535

3636
/// <summary>
37-
/// Gets or sets a value that indicates whether enum type name prefix on values should be stripped when
38-
/// reading and writing enum values.
39-
/// Default value is <see langword="false"/>.
37+
/// Gets or sets a value indicating whether the enum type name prefix should be removed when reading and writing enum values.
38+
/// The default value is <see langword="false"/>.
4039
/// </summary>
41-
public bool StripEnumPrefix { get; set; }
40+
/// <remarks>
41+
/// <para>
42+
/// In Protocol Buffers, enum value names are globally scoped, so they are often prefixed with the enum type name
43+
/// to avoid name collisions. For example, the <c>Status</c> enum might define values like <c>STATUS_UNKNOWN</c>
44+
/// and <c>STATUS_OK</c>.
45+
/// </para>
46+
/// <code>
47+
/// enum Status {
48+
/// STATUS_UNKNOWN = 0;
49+
/// STATUS_OK = 1;
50+
/// }
51+
/// </code>
52+
/// <para>
53+
/// When <see cref="RemoveEnumPrefix"/> is set to <see langword="true"/>, the enum values above
54+
/// will be read and written as <c>UNKNOWN</c> and <c>OK</c> instead of <c>STATUS_UNKNOWN</c>
55+
/// and <c>STATUS_OK</c>.
56+
/// </para>
57+
/// </remarks>
58+
public bool RemoveEnumPrefix { get; set; }
4259
}

src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumNameHelpers.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ internal static class JsonNamingHelpers
1515
internal static EnumValueDescriptor? GetEnumFieldReadValue(EnumDescriptor enumDescriptor, string value, GrpcJsonSettings settings)
1616
{
1717
string resolvedName;
18-
if (settings.StripEnumPrefix)
18+
if (settings.RemoveEnumPrefix)
1919
{
2020
var nameMapping = GetEnumMapping(enumDescriptor);
21-
if (!nameMapping.StripEnumPrefixMapping.TryGetValue(value, out var n))
21+
if (!nameMapping.RemoveEnumPrefixMapping.TryGetValue(value, out var n))
2222
{
2323
return null;
2424
}
@@ -43,7 +43,7 @@ internal static class JsonNamingHelpers
4343
return null;
4444
}
4545

46-
return settings.StripEnumPrefix ? mapping.StripEnumPrefixName : mapping.OriginalName;
46+
return settings.RemoveEnumPrefix ? mapping.RemoveEnumPrefixName : mapping.OriginalName;
4747
}
4848

4949
private static EnumMapping GetEnumMapping(EnumDescriptor enumDescriptor)
@@ -79,15 +79,15 @@ private static EnumMapping GetEnumMapping(string enumName, Type enumType)
7979
return new NameMapping
8080
{
8181
OriginalName = fieldName,
82-
StripEnumPrefixName = GetEnumValueName(enumName, fieldName)
82+
RemoveEnumPrefixName = GetEnumValueName(enumName, fieldName)
8383
};
8484
});
8585

86-
var stripEnumPrefixMapping = writeMapping.Values.ToDictionary(
87-
m => m.StripEnumPrefixName,
86+
var removeEnumPrefixMapping = writeMapping.Values.ToDictionary(
87+
m => m.RemoveEnumPrefixName,
8888
m => m.OriginalName);
8989

90-
return new EnumMapping { WriteMapping = writeMapping, StripEnumPrefixMapping = stripEnumPrefixMapping };
90+
return new EnumMapping { WriteMapping = writeMapping, RemoveEnumPrefixMapping = removeEnumPrefixMapping };
9191
}
9292

9393
// Remove the prefix from the specified value. Ignore case and underscores in the comparison.
@@ -141,12 +141,12 @@ private static string GetEnumValueName(string enumName, string valueName)
141141
private sealed class EnumMapping
142142
{
143143
public required Dictionary<object, NameMapping> WriteMapping { get; init; }
144-
public required Dictionary<string, string> StripEnumPrefixMapping { get; init; }
144+
public required Dictionary<string, string> RemoveEnumPrefixMapping { get; init; }
145145
}
146146

147147
private sealed class NameMapping
148148
{
149149
public required string OriginalName { get; init; }
150-
public required string StripEnumPrefixName { get; init; }
150+
public required string RemoveEnumPrefixName { get; init; }
151151
}
152152
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#nullable enable
2-
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.StripEnumPrefix.get -> bool
3-
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.StripEnumPrefix.set -> void
2+
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.RemoveEnumPrefix.get -> bool
3+
Microsoft.AspNetCore.Grpc.JsonTranscoding.GrpcJsonSettings.RemoveEnumPrefix.set -> void

src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/ConverterTests/JsonConverterReadTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,14 @@ public void Enum_ReadString(string value, HelloRequest.Types.DataTypes.Types.Nes
247247
[InlineData("UNSPECIFIED", PrefixEnumType.Types.PrefixEnum.Unspecified)]
248248
[InlineData("FOO", PrefixEnumType.Types.PrefixEnum.Foo)]
249249
[InlineData("BAR", PrefixEnumType.Types.PrefixEnum.Bar)]
250-
public void Enum_StripPrefix_ReadString(string value, PrefixEnumType.Types.PrefixEnum expectedValue)
250+
public void Enum_RemovePrefix_ReadString(string value, PrefixEnumType.Types.PrefixEnum expectedValue)
251251
{
252252
var serviceDescriptorRegistry = new DescriptorRegistry();
253253
serviceDescriptorRegistry.RegisterFileDescriptor(JsonTranscodingGreeter.Descriptor.File);
254254

255255
var json = @$"{{ ""singleEnum"": ""{value}"" }}";
256256

257-
var result = AssertReadJson<PrefixEnumType>(json, descriptorRegistry: serviceDescriptorRegistry, serializeOld: false, settings: new GrpcJsonSettings { StripEnumPrefix = true });
257+
var result = AssertReadJson<PrefixEnumType>(json, descriptorRegistry: serviceDescriptorRegistry, serializeOld: false, settings: new GrpcJsonSettings { RemoveEnumPrefix = true });
258258
Assert.Equal(expectedValue, result.SingleEnum);
259259
}
260260

src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/ConverterTests/JsonConverterWriteTests.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,36 +475,23 @@ public void FieldMask_Root()
475475
[InlineData(PrefixEnumType.Types.PrefixEnum.Baz, @"""BAZ""")]
476476
[InlineData(PrefixEnumType.Types.PrefixEnum._123, @"""_123""")]
477477
[InlineData((PrefixEnumType.Types.PrefixEnum)100, "100")]
478-
public void Enum_StripPrefix(PrefixEnumType.Types.PrefixEnum value, string expectedString)
478+
public void Enum_RemovePrefix(PrefixEnumType.Types.PrefixEnum value, string expectedString)
479479
{
480480
var dataTypes = new PrefixEnumType
481481
{
482482
SingleEnum = value
483483
};
484484

485-
var json = AssertWrittenJson(dataTypes, settings: new GrpcJsonSettings { StripEnumPrefix = true }, compareOldNew: false);
485+
var json = AssertWrittenJson(dataTypes, settings: new GrpcJsonSettings { RemoveEnumPrefix = true }, compareOldNew: false);
486486
Assert.Equal(@$"{{""singleEnum"":{expectedString}}}", json);
487487
}
488488

489-
/*
490-
[Fact]
491-
public void Enum_StripPrefix_Collision_Error()
492-
{
493-
var dataTypes = new CollisionPrefixEnumType
494-
{
495-
SingleEnum = CollisionPrefixEnumType.Types.CollisionPrefixEnum.Foo
496-
};
497-
498-
AssertWrittenJson(dataTypes, settings: new GrpcJsonSettings { StripEnumPrefix = true }, compareOldNew: false);
499-
}
500-
*/
501-
502489
[Theory]
503490
[InlineData(PrefixEnumType.Types.PrefixEnum.Unspecified, @"""PREFIX_ENUM_UNSPECIFIED""")]
504491
[InlineData(PrefixEnumType.Types.PrefixEnum.Foo, @"""PREFIX_ENUM_FOO""")]
505492
[InlineData(PrefixEnumType.Types.PrefixEnum.Bar, @"""BAR""")]
506493
[InlineData(PrefixEnumType.Types.PrefixEnum._123, @"""PREFIX_ENUM_123""")]
507-
public void Enum_NoStripPrefix(PrefixEnumType.Types.PrefixEnum value, string expectedString)
494+
public void Enum_NoRemovePrefix(PrefixEnumType.Types.PrefixEnum value, string expectedString)
508495
{
509496
var dataTypes = new PrefixEnumType
510497
{

0 commit comments

Comments
 (0)