Skip to content

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
merged 1 commit into from
Jul 29, 2025

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented Jul 23, 2025

Fixes #58850

Add an option to remove enum name prefixes from values when serializing and deserializing.

@Copilot Copilot AI review requested due to automatic review settings July 23, 2025 05:58
@JamesNK JamesNK added the area-grpc Includes: GRPC wire-up, templates label Jul 23, 2025
@JamesNK JamesNK requested a review from captainsafia as a code owner July 23, 2025 05:58
Copy link
Contributor

@Copilot Copilot AI left a 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 to GrpcJsonSettings 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

@JamesNK JamesNK added this to the 10.0-preview7 milestone Jul 23, 2025
@JamesNK JamesNK force-pushed the jamesnk/transcoding-stripenumprefix branch from 2a5f3a0 to 02cd7f7 Compare July 23, 2025 06:36
@JamesNK JamesNK changed the title Add gRPC JSON transcoding option for stripping enum prefix Add gRPC JSON transcoding option to remove enum prefix Jul 23, 2025
Comment on lines +152 to +163
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; }
}

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);

Copy link
Member Author

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.

@JamesNK JamesNK force-pushed the jamesnk/transcoding-stripenumprefix branch from 1743ec7 to a7ee15f Compare July 28, 2025 08:15
@JamesNK JamesNK modified the milestones: 10.0-preview7, 10.0-rc1 Jul 28, 2025
@JamesNK
Copy link
Member Author

JamesNK commented Jul 29, 2025

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@JamesNK JamesNK merged commit 02125bd into main Jul 29, 2025
30 checks passed
@JamesNK JamesNK deleted the jamesnk/transcoding-stripenumprefix branch July 29, 2025 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-grpc Includes: GRPC wire-up, templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gRPC Transcoding: options to ser/deserialize enums without prefix
3 participants