Skip to content

Commit 7d925f1

Browse files
committed
Assume PROTOBUF_WRAPPED_IN_ANY if format is unspecified
UMessage::deserialize_protobuf_bytes has been adapted to interpret UPayloadFormat::UPAYLOAD_FORMAT_UNSPECIFIED as UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY as mandated by the uProtocol specification.
1 parent f4109d1 commit 7d925f1

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/umessage.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,22 @@ impl UMessage {
190190

191191
/// Deserializes a protobuf message from a byte array.
192192
///
193-
/// Will only succeed if payload format is one of
194-
/// - `UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF`
195-
/// - `UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY`
193+
/// # Arguments
194+
///
195+
/// * `payload` - The payload data.
196+
/// * `payload_format` - The format/encoding of the data. Must be one of
197+
/// - `UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF`
198+
/// - `UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY`
199+
/// - `UPayloadFormat::UPAYLOAD_FORMAT_UNSPECIFIED`
200+
///
201+
/// `UPayloadFormat::UPAYLOAD_FORMAT_UNSPECIFIED` is interpreted as
202+
/// `UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY` according to the uProtocol
203+
/// specification.
204+
///
205+
/// # Errors
206+
///
207+
/// Returns an error if the payload format is unsupported or if the data is can not be deserialized
208+
/// based on the given format.
196209
pub(crate) fn deserialize_protobuf_bytes<T: MessageFull + Default>(
197210
payload: &Bytes,
198211
payload_format: &UPayloadFormat,
@@ -201,17 +214,16 @@ pub(crate) fn deserialize_protobuf_bytes<T: MessageFull + Default>(
201214
UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF => {
202215
T::parse_from_tokio_bytes(payload).map_err(UMessageError::DataSerializationError)
203216
}
204-
UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY => {
205-
Any::parse_from_tokio_bytes(payload)
206-
.map_err(UMessageError::DataSerializationError)
207-
.and_then(|any| match any.unpack() {
208-
Ok(Some(v)) => Ok(v),
209-
Ok(None) => Err(UMessageError::PayloadError(
210-
"cannot deserialize payload, message type mismatch".to_string(),
211-
)),
212-
Err(e) => Err(UMessageError::DataSerializationError(e)),
213-
})
214-
}
217+
UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY
218+
| UPayloadFormat::UPAYLOAD_FORMAT_UNSPECIFIED => Any::parse_from_tokio_bytes(payload)
219+
.map_err(UMessageError::DataSerializationError)
220+
.and_then(|any| match any.unpack() {
221+
Ok(Some(v)) => Ok(v),
222+
Ok(None) => Err(UMessageError::PayloadError(
223+
"cannot deserialize payload, message type mismatch".to_string(),
224+
)),
225+
Err(e) => Err(UMessageError::DataSerializationError(e)),
226+
}),
215227
_ => Err(UMessageError::from(
216228
"Unknown/invalid/unsupported payload format",
217229
)),
@@ -223,6 +235,7 @@ mod test {
223235
use std::io;
224236

225237
use protobuf::well_known_types::{any::Any, duration::Duration, wrappers::StringValue};
238+
use test_case::test_case;
226239

227240
use crate::{UAttributes, UStatus};
228241

@@ -235,6 +248,12 @@ mod test {
235248
let any = Any::pack(&data.clone()).unwrap();
236249
let buf: Bytes = any.write_to_bytes().unwrap().into();
237250

251+
let result = deserialize_protobuf_bytes::<StringValue>(
252+
&buf,
253+
&UPayloadFormat::UPAYLOAD_FORMAT_UNSPECIFIED,
254+
);
255+
assert!(result.is_ok_and(|v| v.value == *"hello world"));
256+
238257
let result = deserialize_protobuf_bytes::<StringValue>(
239258
&buf,
240259
&UPayloadFormat::UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY,
@@ -261,12 +280,14 @@ mod test {
261280
assert!(result.is_err_and(|e| matches!(e, UMessageError::PayloadError(_))));
262281
}
263282

264-
#[test]
265-
fn test_deserialize_protobuf_bytes_fails_for_unsupported_format() {
266-
let result = deserialize_protobuf_bytes::<UStatus>(
267-
&"hello".into(),
268-
&UPayloadFormat::UPAYLOAD_FORMAT_TEXT,
269-
);
283+
#[test_case(UPayloadFormat::UPAYLOAD_FORMAT_JSON; "JSON format")]
284+
#[test_case(UPayloadFormat::UPAYLOAD_FORMAT_RAW; "RAW format")]
285+
#[test_case(UPayloadFormat::UPAYLOAD_FORMAT_SHM; "SHM format")]
286+
#[test_case(UPayloadFormat::UPAYLOAD_FORMAT_SOMEIP; "SOMEIP format")]
287+
#[test_case(UPayloadFormat::UPAYLOAD_FORMAT_SOMEIP_TLV; "SOMEIP TLV format")]
288+
#[test_case(UPayloadFormat::UPAYLOAD_FORMAT_TEXT; "TEXT format")]
289+
fn test_deserialize_protobuf_bytes_fails_for_(format: UPayloadFormat) {
290+
let result = deserialize_protobuf_bytes::<UStatus>(&"hello".into(), &format);
270291
assert!(result.is_err_and(|e| matches!(e, UMessageError::PayloadError(_))));
271292
}
272293

0 commit comments

Comments
 (0)