Skip to content

API Proposal for gRPC JSON transcoding option for case insensitive field names #62892

@JamesNK

Description

@JamesNK

Edit: Updated with feedback.

Background and Motivation

Many JSON APIs have case insensitive property names. People looking to replace their existing JSON API with gRPC JSON transcoding want to keep that behavior. Today they don't have that option. Some are using reflection to modify JSON settings to enable it.

Addresses #50401

Proposed API

namespace Microsoft.AspNetCore.Grpc.JsonTranscoding;

public sealed class GrpcJsonSettings
{
+    /// <summary>
+    /// Gets or sets a value that indicates whether property names are compared using case-insensitive matching during deserialization.
+    /// The default value is <see langword="false"/>.
+    /// </summary>
+    /// <remarks>
+    /// <para>
+    /// The Protobuf JSON specification requires JSON property names to match field names exactly, including case.
+    /// Enabling this option may reduce interoperability, as case-insensitive matching might not be supported
+    /// by other JSON transcoding implementations.
+    /// </para>
+    /// <para>
+    /// For more information, see <see href="https://protobuf.dev/programming-guides/json/"/>.
+    /// </para>
+    /// </remarks>
+    public bool PropertyNameCaseInsensitive { get; set; }
}

Usage Examples

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc()
builder.Services.AddJsonTranscoding(o =>
{
    o.JsonSettings.FieldNamesCaseInsensitive = true;
});

Alternative Designs

This setting internally sets JsonSerializerOptions.PropertyNameCaseInsensitive. I considered calling it PropertyNameCaseInsensitive but in Protobuf members are always called fields, not properties.

Risks

Technically case insensitive property names aren't part of the Protobuf JSON spec - https://protobuf.dev/programming-guides/json/ - but we'll call this out in docs so people can make an informed decision.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-grpcIncludes: GRPC wire-up, templates

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions