Skip to content
Myr

axum: deserialize error path on failure (#3924)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Deserialize without path tracking first, and only if that fails do it again through serde_path_to_error to build the rejection. Error messages and rejection types stay the same, since the second pass sees the same input. This way, the first deserialization doesn't need to allocate path names for potential error reporting, making happy-path faster and deserialization errors twice as slow.

authored by Falk Woldmann Lu · committed by GitHub · parent 90f299c · Browse files · 4 files+31 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+31 added−13 removed

  • other:+31 added−13 removed

4 files+31 added−13 removed

modified:axum/src/extract/query.rs+2 added-2 removed
76 unmodified lines
77 /// ```78 pub fn try_from_uri(value: &Uri) -> Result<Self, QueryRejection> {79 let query = value.query().unwrap_or_default();80 let deserializer =81 serde_html_form::Deserializer::new(form_urlencoded::parse(query.as_bytes()));82 let params = serde_path_to_error::deserialize(deserializer)81 || serde_html_form::Deserializer::new(form_urlencoded::parse(query.as_bytes()));82 let params = crate::util::deserialize_with_error_path(deserializer)83 .map_err(FailedToDeserializeQueryString::from_err)?;84 Ok(Self(params))85 }86}
109 unmodified lines
modified:axum/src/form.rs+2 added-2 removed
82 unmodified lines
8384 match req.extract().await {85 Ok(RawForm(bytes)) => {86 let deserializer =87 serde_html_form::Deserializer::new(form_urlencoded::parse(&bytes));88 let value = serde_path_to_error::deserialize(deserializer).map_err(87 || serde_html_form::Deserializer::new(form_urlencoded::parse(&bytes));88 let value = crate::util::deserialize_with_error_path(deserializer).map_err(89 |err| -> FormRejection {90 if is_get_or_head {91 FailedToDeserializeForm::from_err(err).into()92 } else {
190 unmodified lines
modified:axum/src/json.rs+12 added-9 removed
179 unmodified lines
180 }181 }182 }183184 // like `deserialize_with_error_path`, which can't be used here because of `end`185 serde_json::from_slice(bytes).map(Self).or_else(|_| {184 let mut deserializer = serde_json::Deserializer::from_slice(bytes);186 let mut deserializer = serde_json::Deserializer::from_slice(bytes);187186 serde_path_to_error::deserialize(&mut deserializer)187 .map_err(make_rejection)188 .and_then(|value| {189 deserializer190 .end()191 .map(|()| Self(value))192 .map_err(|err| JsonSyntaxError::from_err(err).into())193 })188 serde_path_to_error::deserialize(&mut deserializer)189 .map_err(make_rejection)190 .and_then(|value| {191 deserializer192 .end()193 .map(|()| Self(value))194 .map_err(|err| JsonSyntaxError::from_err(err).into())195 })196 })197 }198}199200impl<T> IntoResponse for Json<T>
172 unmodified lines
modified:axum/src/util.rs+15 added
7 unmodified lines
8 task::{ready, Context, Poll},9};10use tower::Service;1112/// Deserializes a `T`, and only if that fails does it again through `serde_path_to_error` to report13/// where the error is. Tracking the path allocates for every field, so it's skipped when14/// deserialization succeeds.15#[cfg(any(feature = "form", feature = "query"))]16pub(crate) fn deserialize_with_error_path<'de, D, T>(17 mut make_deserializer: impl FnMut() -> D,18) -> Result<T, serde_path_to_error::Error<D::Error>>19where20 D: serde_core::Deserializer<'de>,21 T: serde_core::Deserialize<'de>,22{23 T::deserialize(make_deserializer())24 .or_else(|_| serde_path_to_error::deserialize(make_deserializer()))25}2627#[derive(Clone, Debug, PartialEq, Eq, Hash)]28pub(crate) struct PercentDecodedStr(Arc<str>);2930impl PercentDecodedStr {
101 unmodified lines