Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serde support for prost-types #852

Open
mgoldenberg opened this issue Apr 25, 2023 · 3 comments · May be fixed by #1252
Open

serde support for prost-types #852

mgoldenberg opened this issue Apr 25, 2023 · 3 comments · May be fixed by #1252

Comments

@mgoldenberg
Copy link

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
Copy link
Author

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.

@clotodex
Copy link

Any update on this? I cannot compile it to use Struct.

@caspermeijn
Copy link
Collaborator

Recently, another optional dependency was added, so I feel like an optional dependency for serde makes sense. You are welcome to open an PR.

arbitrary = { version = "1.4", features = ["derive"], optional = true }

allada added a commit to allada/prost that referenced this issue Feb 27, 2025
Adds "serde_deserialize", "serde_serialize" and "serde" flags to
prost-types. This will allow users to enable serde support to the
well known types.

closes tokio-rs#852
@allada allada linked a pull request Feb 27, 2025 that will close this issue
allada added a commit to allada/prost that referenced this issue Feb 27, 2025
Adds "serde_deserialize", "serde_serialize" and "serde" flags to
prost-types. This will allow users to enable serde support to the
well known types.

closes tokio-rs#852
allada added a commit to allada/prost that referenced this issue Feb 27, 2025
Adds "serde_deserialize", "serde_serialize" and "serde" flags to
prost-types. This will allow users to enable serde support to the
well known types.

closes tokio-rs#852
allada added a commit to allada/prost that referenced this issue Feb 27, 2025
Adds "serde_deserialize", "serde_serialize" and "serde" flags to
prost-types. This will allow users to enable serde support to the
well known types.

closes tokio-rs#852
allada added a commit to allada/prost that referenced this issue Feb 27, 2025
Adds "serde_deserialize", "serde_serialize" and "serde" flags to
prost-types. This will allow users to enable serde support to the
well known types.

closes tokio-rs#852
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants