Skip to content

Commit e42dcad

Browse files
Bufix: Name::full_name() is correct for empty packages (#1386)
Changes the implementation of Name::full_name() so that it produces valid urls via Name::type_url() if the package is empty. Co-authored-by: Aaron Eline <aeline@amazon.com>
1 parent 107153f commit e42dcad

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

prost-types/src/any.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl Name for Any {
4949
#[cfg(test)]
5050
mod tests {
5151
use super::*;
52+
use prost::{bytes, encoding, Message};
5253

5354
#[test]
5455
fn check_any_serialization() {
@@ -65,4 +66,56 @@ mod tests {
6566
// Wrong type URL
6667
assert!(any.to_msg::<Duration>().is_err());
6768
}
69+
#[derive(Clone, PartialEq, Debug, Default)]
70+
struct Test {
71+
value: i32,
72+
}
73+
74+
impl Message for Test {
75+
fn encode_raw(&self, buf: &mut impl bytes::BufMut) {
76+
encoding::int32::encode(1, &self.value, buf);
77+
}
78+
79+
fn merge_field(
80+
&mut self,
81+
tag: u32,
82+
wire_type: encoding::WireType,
83+
buf: &mut impl bytes::Buf,
84+
ctx: encoding::DecodeContext,
85+
) -> Result<(), crate::DecodeError> {
86+
if tag == 1 {
87+
encoding::int32::merge(wire_type, &mut self.value, buf, ctx)
88+
} else {
89+
encoding::skip_field(wire_type, tag, buf, ctx)
90+
}
91+
}
92+
93+
fn encoded_len(&self) -> usize {
94+
encoding::int32::encoded_len(1, &self.value)
95+
}
96+
97+
fn clear(&mut self) {
98+
self.value = 0;
99+
}
100+
}
101+
102+
impl crate::Name for Test {
103+
const PACKAGE: &'static str = ""; // Empty package
104+
const NAME: &'static str = "Test";
105+
}
106+
107+
#[test]
108+
fn dynamic_cast_round_trip() {
109+
let msg = Test::default();
110+
let any = Any::from_msg(&msg).unwrap();
111+
let result: Result<Test, _> = any.to_msg();
112+
result.expect("This should parse!");
113+
}
114+
115+
#[test]
116+
fn default_type_url_should_parse() {
117+
let type_url = Test::type_url(); //any.type_url;
118+
TypeUrl::new(&type_url)
119+
.expect("The URL created by the default implementation should parse");
120+
}
68121
}

prost/src/name.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ pub trait Name: Message {
2121
/// By default, this is the package name followed by the message name.
2222
/// Fully-qualified names must be unique within a domain of Type URLs.
2323
fn full_name() -> String {
24-
format!("{}.{}", Self::PACKAGE, Self::NAME)
24+
if Self::PACKAGE.is_empty() {
25+
Self::NAME.into()
26+
} else {
27+
format!("{}.{}", Self::PACKAGE, Self::NAME)
28+
}
2529
}
2630

2731
/// Type URL for this [`Message`], which by default is the full name with a

0 commit comments

Comments
 (0)