prost icon indicating copy to clipboard operation
prost copied to clipboard

`serde` support for `prost-types`

Open mgoldenberg opened this issue 1 year ago • 1 comments

Hi!

I'm trying to support serde-based serialization on some Protobuf types I have generated using prost. I do this conditionally by putting the following in the build script for my crate.

fn main() {
    let protos = 
    prost_build::Config::new()
        .type_attribute(".", "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]");
        .compile_protos(&[/* list of files */], &[/* list of includes */])
        .unwrap();
}

This works great until I use any of the well-known types. The problem, of course, is that the well-known types have already been generated and exist in prost-types. The workaround is to use compile_well_known_types(), like so.

syntax = "proto3";

import "google/protobuf/timestamp.proto";

message LogWithTime {
    string log = 1;
    google.protobuf.Timestamp timestamp = 2;
}
fn main() {
    let protos = 
    prost_build::Config::new()
        .type_attribute(".", "#[cfg_attr(feature = \"serde\", derive(serde::Serialize, serde::Deserialize))]");
        .compile_well_known_types() // <---- added this line!
        .compile_protos(&[/* list of files */], &[/* list of includes */])
        .unwrap();
}

The problem now, however, is that the well-known types I generated are no longer compatible with any other crate's well-known types. So, it becomes difficult to do transformations between the types generated across crates.

My workaround is to generate the well-known types with the appropriate serde attributes in a new shared crate that can be used by all the other crates in my workspace. But, I was wondering: would it be possible to add a feature flag to prost-types that would allow one to turn on support for serde? Or perhaps the feature flag is better situated in prost-build? I'm not sure exactly where the best place to put it is, but generally having support for serde would be nice.

mgoldenberg avatar Apr 25 '23 18:04 mgoldenberg

By the way, I did see there was some desire for this at some point, but figured it might be worth bringing up in a separate issue in case things have changed.

I'm very keen to add support for the protobuf JSON encoding to prost by having an option to output the right serde annotations, however I haven't found time to actually work on this. Once that's done, they could be enabled for prost-types through a cargo feature. I can't give a timeline on when this might land, though.

mgoldenberg avatar Apr 25 '23 18:04 mgoldenberg