Skip to content

Add gRPC JSON transcoding option to remove enum prefix - #62873

Merged
JamesNK merged 1 commit into
mainfrom
jamesnk/transcoding-stripenumprefix
Jul 29, 2025
Merged

JamesNK merged 1 commit into
mainfrom
jamesnk/transcoding-stripenumprefix

Conversation

@JamesNK

@JamesNK JamesNK commented Jul 23, 2025

Copy link
Copy Markdown
Member

Fixes #58850

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

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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
Copy Markdown
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

JamesNK commented Jul 29, 2025

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@JamesNK
JamesNK merged commit 02125bd into main Jul 29, 2025
@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

4 participants