1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
use std::ops::{Deref, DerefMut};
use async_trait::async_trait;
use axum_core::{
extract::{FromRequest, Request},
response::{IntoResponse, Response},
};
use bytes::{BufMut, Bytes, BytesMut};
use http::{header, HeaderMap, HeaderValue, StatusCode};
use serde::{de::DeserializeOwned, Serialize};
use crate::rejection::*;
/// YAML Extractor / Response.
///
/// When used as an extractor, it can deserialize request bodies into some type that
/// implements [`serde::Deserialize`]. If the request body cannot be parsed, or it does not contain
/// the `Content-Type: application/yaml` header, it will reject the request and return a
/// `400 Bad Request` response.
///
/// # Extractor example
///
/// ```no_run
/// use axum::{
/// extract,
/// routing::post,
/// Router,
/// };
/// use axum_yaml::Yaml;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct CreateUser {
/// email: String,
/// password: String,
/// }
///
/// async fn create_user(Yaml(payload): Yaml<CreateUser>) {
/// // payload is a `CreateUser`
/// }
///
/// let app = Router::new().route("/users", post(create_user));
/// # async {
/// # axum::serve(
/// # tokio::net::TcpListener::bind("").await.unwrap(),
/// # app.into_make_service(),
/// # )
/// # .await
/// # .unwrap();
/// # };
/// ```
///
/// When used as a response, it can serialize any type that implements [`serde::Serialize`] to
/// `YAML`, and will automatically set `Content-Type: application/yaml` header.
///
/// # Response example
///
/// ```no_run
/// use axum::{
/// extract::Path,
/// routing::get,
/// Router,
/// };
/// use axum_yaml::Yaml;
/// use serde::Serialize;
/// use uuid::Uuid;
///
/// #[derive(Serialize)]
/// struct User {
/// id: Uuid,
/// username: String,
/// }
///
/// async fn get_user(Path(user_id) : Path<Uuid>) -> Yaml<User> {
/// let user = find_user(user_id).await;
/// Yaml(user)
/// }
///
/// async fn find_user(user_id: Uuid) -> User {
/// // ...
/// # unimplemented!()
/// }
///
/// let app = Router::new().route("/users/:id", get(get_user));
/// # async {
/// # axum::serve(
/// # tokio::net::TcpListener::bind("").await.unwrap(),
/// # app.into_make_service(),
/// # )
/// # .await
/// # .unwrap();
/// # };
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Yaml<T>(pub T);
#[async_trait]
impl<T, S> FromRequest<S> for Yaml<T>
where
T: DeserializeOwned,
S: Send + Sync,
{
type Rejection = YamlRejection;
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
if yaml_content_type(req.headers()) {
let bytes = Bytes::from_request(req, state).await?;
Self::from_bytes(&bytes)
} else {
Err(MissingYamlContentType.into())
}
}
}
fn yaml_content_type(headers: &HeaderMap) -> bool {
let Some(content_type) = headers.get(header::CONTENT_TYPE) else {
return false;
};
let Ok(content_type) = content_type.to_str() else {
return false;
};
let Ok(mime) = content_type.parse::<mime::Mime>() else {
return false;
};
let is_yaml_content_type = mime.type_() == "application"
&& (mime.subtype() == "yaml" || mime.suffix().map_or(false, |name| name == "yaml"));
is_yaml_content_type
}
impl<T> Deref for Yaml<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Yaml<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> From<T> for Yaml<T> {
fn from(inner: T) -> Self {
Self(inner)
}
}
impl<T> Yaml<T>
where
T: DeserializeOwned,
{
/// Construct a `Yaml<T>` from a byte slice. Most users should prefer to use the `FromRequest` impl
/// but special cases may require first extracting a `Request` into `Bytes` then optionally
/// constructing a `Yaml<T>`.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, YamlRejection> {
let deserializer = serde_yaml::Deserializer::from_slice(bytes);
match serde_path_to_error::deserialize(deserializer) {
Ok(value) => Ok(Yaml(value)),
Err(err) => Err(YamlError::from_err(err).into()),
}
}
}
impl<T> IntoResponse for Yaml<T>
where
T: Serialize,
{
fn into_response(self) -> Response {
// Use a small initial capacity of 128 bytes like serde_json::to_vec
// https://docs.rs/serde_json/1.0.82/src/serde_json/ser.rs.html#2189
let mut buf = BytesMut::with_capacity(128).writer();
match serde_yaml::to_writer(&mut buf, &self.0) {
Ok(()) => (
[(
header::CONTENT_TYPE,
HeaderValue::from_static("application/yaml"),
)],
buf.into_inner().freeze(),
)
.into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
[(
header::CONTENT_TYPE,
HeaderValue::from_static(mime::TEXT_PLAIN_UTF_8.as_ref()),
)],
err.to_string(),
)
.into_response(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use axum::routing::post;
use axum::Router;
use http::StatusCode;
use serde::Deserialize;
use serde_yaml::Value;
use crate::test_client::TestClient;
#[tokio::test]
async fn deserialize_body() {
#[derive(Debug, Deserialize)]
struct Input {
foo: String,
}
let app = Router::new().route("/", post(|input: Yaml<Input>| async { input.0.foo }));
let client = TestClient::new(app);
let res = client
.post("/")
.body("foo: bar")
.header("content-type", "application/yaml")
.await;
let body = res.text().await;
assert_eq!(body, "bar");
}
#[tokio::test]
async fn consume_body_to_yaml_requres_yaml_content_type() {
#[derive(Debug, Deserialize)]
struct Input {
foo: String,
}
let app = Router::new().route("/", post(|input: Yaml<Input>| async { input.0.foo }));
let client = TestClient::new(app);
let res = client.post("/").body("foo: bar").await;
let status = res.status();
assert_eq!(status, StatusCode::UNSUPPORTED_MEDIA_TYPE);
}
#[tokio::test]
async fn yaml_content_types() {
async fn valid_yaml_content_type(content_type: &str) -> bool {
println!("testing {:?}", content_type);
let app = Router::new().route("/", post(|Yaml(_): Yaml<Value>| async {}));
let res = TestClient::new(app)
.post("/")
.header("content-type", content_type)
.body("foo: ")
.await;
res.status() == StatusCode::OK
}
assert!(valid_yaml_content_type("application/yaml").await);
assert!(valid_yaml_content_type("application/yaml;charset=utf-8").await);
assert!(valid_yaml_content_type("application/yaml; charset=utf-8").await);
assert!(!valid_yaml_content_type("text/yaml").await);
}
#[tokio::test]
async fn invalid_yaml_syntax() {
let app = Router::new().route("/", post(|_: Yaml<Value>| async {}));
let client = TestClient::new(app);
let res = client
.post("/")
.body("- a\nb:")
.header("content-type", "application/yaml")
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
}
#[derive(Deserialize)]
struct Foo {
#[allow(dead_code)]
a: i32,
#[allow(dead_code)]
b: Vec<Bar>,
}
#[derive(Deserialize)]
struct Bar {
#[allow(dead_code)]
x: i32,
#[allow(dead_code)]
y: i32,
}
#[tokio::test]
async fn invalid_yaml_data() {
let app = Router::new().route("/", post(|_: Yaml<Foo>| async {}));
let client = TestClient::new(app);
let res = client
.post("/")
.body("a: 1\nb:\n - x: 2")
.header("content-type", "application/yaml")
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
let body_text = res.text().await;
assert_eq!(
body_text,
"Failed to deserialize the YAML body into the target type: b[0]: b[0]: missing field `y` at line 3 column 7"
);
}
}