Skip to content

Feature Request: Support Flat JSON Structure for OneOf When Creating Protobuf Messages #1320

Description

@clayzx

When working with Protobuf messages that contain oneof fields, the generated JSON types are very straightforward and user-friendly. For example, consider the following message definition:

message Example {
  int32 amount = 1;
  oneof either {
    bytes data = 2;
    string error_message = 3;
  }
}

The generated JSON type is:

/**
 * @generated from message Example
 */
export type ExampleJson = {
  /** @generated from field: int32 amount = 1; */
  amount?: number;

  /** @generated from field: bytes data = 2; */
  data?: string;

  /** @generated from field: string error_message = 3; */
  errorMessage?: string;
};

Using toJson, you can easily convert a Protobuf message to this flat JSON structure, even for oneof cases. However, when creating a Protobuf message from this JSON structure, the library does not currently support a flat layout for oneof fields.

For example, given this data:

const data: ExampleJson = {
  amount: 1,
  data: 'data',
};

Attempting to create a Protobuf message directly:

const example = create(ExampleSchema, { amount: 1, data: 'data' });

results in a message where the data field under either is not set.

Currently, the only supported way is:

const example = create(ExampleSchema, { amount: 1, either: { case: 'data', value: 'data' } });

Describe the solution you'd like

I would like to request support for creating Protobuf messages using a flat JSON structure for oneof fields, such that the following works as expected:

const example = create(ExampleSchema, { amount: 1, data: 'data' });

This would greatly simplify consuming code, make the API more ergonomic, and align with the straightforward JSON types generated.

Describe alternatives you've considered

  • Manually mapping the flat JSON structure to the nested oneof format every time.
  • Writing wrapper functions to perform the conversion.

Both of these approaches add additional boilerplate and cognitive overhead, especially as the number of messages and oneof fields increases.

Additional context

  • Having symmetry between the output format of toJson and the input format for create (or similar) would make working with Protobuf messages much more intuitive.
  • This feature would be especially helpful for projects with many oneof fields or those that generate/type-check JSON payloads.

Thank you for considering this feature request!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions