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!
When working with Protobuf messages that contain
oneoffields, the generated JSON types are very straightforward and user-friendly. For example, consider the following message definition:The generated JSON type is:
Using
toJson, you can easily convert a Protobuf message to this flat JSON structure, even foroneofcases. However, when creating a Protobuf message from this JSON structure, the library does not currently support a flat layout foroneoffields.For example, given this data:
Attempting to create a Protobuf message directly:
results in a message where the
datafield undereitheris not set.Currently, the only supported way is:
Describe the solution you'd like
I would like to request support for creating Protobuf messages using a flat JSON structure for
oneoffields, such that the following works as expected:This would greatly simplify consuming code, make the API more ergonomic, and align with the straightforward JSON types generated.
Describe alternatives you've considered
oneofformat every time.Both of these approaches add additional boilerplate and cognitive overhead, especially as the number of messages and
oneoffields increases.Additional context
toJsonand the input format forcreate(or similar) would make working with Protobuf messages much more intuitive.oneoffields or those that generate/type-check JSON payloads.Thank you for considering this feature request!