-
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
StripEnumPrefix
property toGrpcJsonSettings
for configuration - Implemented enum prefix stripping logic in new
JsonNamingHelpers
class - 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
...onTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumNameHelpers.cs
Show resolved
Hide resolved
...onTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumNameHelpers.cs
Show resolved
Hide resolved
2a5f3a0
to
02cd7f7
Compare
...JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumConverter.cs
Outdated
Show resolved
Hide resolved
...onTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumNameHelpers.cs
Outdated
Show resolved
Hide resolved
...onTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/Json/EnumNameHelpers.cs
Outdated
Show resolved
Hide resolved
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The 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.
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.