fdeantoni/prost-wkt

Serde JSON serialization breaks for `Any` types without `#[serde(default)]`

rnbguy opened this issue · 0 comments

Going through the serialize function, I realized, it calls try_unpack()

match self.clone().try_unpack() {
Ok(result) => serde::ser::Serialize::serialize(result.as_ref(), serializer),
Err(_) => {

which may fail because "value" is assigned to {} in this block and the Message type may not derive #[serde(default)].

pub fn try_unpack(self) -> Result<Box<dyn prost_wkt::MessageSerde>, AnyError> {
let type_url = self.type_url.clone();
let empty = json!({
"@type": &type_url,
"value": {}
});
let template: Box<dyn prost_wkt::MessageSerde> = serde_json::from_value(empty)

In such cases, serialization defaults to {"@type": ..., "value": ...}.

state.serialize_field("@type", &self.type_url)?;
state.serialize_field("value", &self.value)?;

This breaks the protobuf serialization to JSON for the Any types without #[serde(default)].