Add gRPC JSON transcoding option to remove enum prefix - #62873
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new option to gRPC JSON transcoding to strip enum prefixes when serializing and deserializing enum values. The feature allows for cleaner JSON representations by removing redundant enum type prefixes from enum value names (e.g., PREFIX_ENUM_FOO becomes FOO).
Key changes:
- Added
StripEnumPrefixproperty toGrpcJsonSettingsfor configuration - Implemented enum prefix stripping logic in new
JsonNamingHelpersclass - Updated enum converter to use the new naming helpers instead of legacy code
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
transcoding.proto |
Added test enum types for prefix stripping validation |
JsonConverterWriteTests.cs |
Added comprehensive tests for enum serialization with and without prefix stripping |
JsonConverterReadTests.cs |
Added tests for enum deserialization with prefix stripping |
Legacy.cs |
Removed deprecated OriginalEnumValueHelper class |
EnumNameHelpers.cs |
New class implementing enum name mapping and prefix stripping logic |
EnumConverter.cs |
Updated to use new naming helpers and descriptor registry |
GrpcJsonSettings.cs |
Added new StripEnumPrefix configuration property |
Comments suppressed due to low confidence (1)
src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/ConverterTests/JsonConverterWriteTests.cs:583
- The new compareOldNew parameter allows skipping comparison with legacy behavior, but there should be specific tests that verify the new StripEnumPrefix functionality produces different output than the default behavior to ensure the feature is working correctly.
private string AssertWrittenJson<TValue>(TValue value, GrpcJsonSettings? settings = null, bool? compareRawStrings = null, bool? compareOldNew = null) where TValue : IMessage
2a5f3a0 to
02cd7f7
Compare
| 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; } | ||
| } |
There was a problem hiding this comment.
Can we make the EnumMapping and NameMapping as record?
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.
Yes. But classes work fine here. And records are bloated when doing AOT.
1743ec7 to
a7ee15f
Compare
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Fixes #58850
Add an option to remove enum name prefixes from values when serializing and deserializing.