jsonapi_axum 1.0.0-rc.1

axum adapter for jsonapi_core: JSON:API extractors, responders, and tower layers built on jsonapi_http
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! axum extractors for JSON:API requests.
//!
//! Each extractor is a thin binding: it reads the relevant part of the request
//! and delegates to [`jsonapi_http`], turning failures into a [`JsonApiError`]
//! rejection.

use std::convert::Infallible;
use std::marker::PhantomData;
use std::sync::Arc;

use axum::extract::{FromRef, FromRequest, FromRequestParts, Request};
use bytes::Bytes;
use http::request::Parts;
use serde::de::DeserializeOwned;

use jsonapi_core::{Document, JsonApiMediaType, PrimaryData, Query, ResourceObject, TypeRegistry};
use jsonapi_http::{
    ClientIdPolicy, check_client_id, check_content_type, check_id_matches, deserialize_body,
    parse_query,
};

use crate::error::JsonApiError;

/// The response media type negotiated by [`AcceptLayer`](crate::AcceptLayer),
/// read back out of the request extensions where the layer stored it.
///
/// A responder's `IntoResponse` cannot see the request, so a handler that wants
/// the response `Content-Type` to reflect the negotiated `ext`/`profile`
/// parameters extracts this and passes it to
/// [`JsonApiResponse::media_type`](crate::JsonApiResponse::media_type):
///
/// ```no_run
/// use jsonapi_axum::{NegotiatedMediaType, JsonApiResponse};
/// # use jsonapi_axum::{Document, Resource};
/// async fn handler(NegotiatedMediaType(media): NegotiatedMediaType) -> JsonApiResponse<Resource> {
///     # let document: Document<Resource> = todo!();
///     JsonApiResponse::new(document).media_type(media)
/// }
/// ```
///
/// When no [`AcceptLayer`](crate::AcceptLayer) ran (nothing stored an extension),
/// it falls back to [`JsonApiMediaType::plain`], so it never fails.
#[derive(Debug, Clone)]
pub struct NegotiatedMediaType(pub JsonApiMediaType);

impl<S> FromRequestParts<S> for NegotiatedMediaType
where
    S: Send + Sync,
{
    type Rejection = Infallible;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        let media = parts
            .extensions
            .get::<JsonApiMediaType>()
            .cloned()
            .unwrap_or_else(JsonApiMediaType::plain);
        Ok(NegotiatedMediaType(media))
    }
}

/// Typed JSON:API request-body extractor. Validates the `Content-Type`, buffers
/// the body, and deserializes a [`Document<T>`], rejecting with a JSON:API error
/// document (415 / 400 / 409 / 422) on failure.
///
/// # PATCH
///
/// This is also the PATCH extractor: define a companion resource whose patchable
/// members are [`Field<T>`](jsonapi_core::Field) and use `JsonApi<ArticlePatch>`.
/// Absent members deserialize to `Field::Absent` (leave unchanged), `null` to
/// `Field::Null` (clear), and values to `Field::Set` — and such members are never
/// required, so a partial body does not 422. See `examples/crud_server.rs`.
#[derive(Debug, Clone)]
pub struct JsonApi<T>(pub Document<T>);

impl<T, S> FromRequest<S> for JsonApi<T>
where
    T: ResourceObject + DeserializeOwned + 'static,
    S: Send + Sync,
{
    type Rejection = JsonApiError;

    async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
        check_content_type(req.headers()).map_err(|err| JsonApiError::from_core(&err))?;

        let bytes = Bytes::from_request(req, state).await.map_err(|rejection| {
            // Mirror axum's own status for the rejection rather than hardcoding
            // 400: a `DefaultBodyLimit` length-limit rejection reports 413
            // (Payload Too Large), while other read failures stay 400. Reading
            // `.status()` keeps future rejection variants correctly mapped.
            JsonApiError::from_status(rejection.status(), Some(rejection.body_text()))
        })?;

        let document = deserialize_body::<T>(&bytes).map_err(JsonApiError::from)?;
        Ok(JsonApi(document))
    }
}

impl<T: ResourceObject> JsonApi<T> {
    /// The primary resource's `id` from a single-resource body, if any.
    fn body_id(&self) -> Option<&str> {
        match &self.0 {
            Document::Data {
                data: PrimaryData::Single(primary),
                ..
            } => primary.resource_id(),
            _ => None,
        }
    }

    /// Assert the body's `data.id` matches `path_id` (the JSON:API `PATCH` rule),
    /// rejecting with a **409 Conflict** JSON:API error (`source.pointer`
    /// `/data/id`) on mismatch. An absent body id is accepted (identity comes
    /// from the URL). Delegates to [`jsonapi_http::check_id_matches`].
    ///
    /// ```no_run
    /// # use jsonapi_axum::{JsonApi, JsonApiError};
    /// # async fn h(id: String, doc: JsonApi<jsonapi_core::Resource>) -> Result<(), JsonApiError> {
    /// doc.require_id(&id)?;
    /// # Ok(()) }
    /// ```
    ///
    /// # Errors
    /// A 409 [`JsonApiError`] when a present body id differs from `path_id`.
    pub fn require_id(&self, path_id: &str) -> Result<(), JsonApiError> {
        check_id_matches(self.body_id(), path_id).map_err(JsonApiError::from)
    }

    /// Apply a [`ClientIdPolicy`] to a create body's client-supplied `id`.
    /// Under [`ClientIdPolicy::Forbid`], a present id is rejected with a
    /// **403 Forbidden** JSON:API error. Delegates to
    /// [`jsonapi_http::check_client_id`].
    ///
    /// # Errors
    /// A 403 [`JsonApiError`] when `policy` is `Forbid` and the body carries an id.
    pub fn check_client_id(&self, policy: ClientIdPolicy) -> Result<(), JsonApiError> {
        check_client_id(policy, self.body_id()).map_err(JsonApiError::from)
    }
}

/// The application's base URL for building `self`/`related` links and the
/// `Location` header, provided from application state via
/// [`FromRef`].
///
/// The base URL is an explicit,
/// app-configured value rather than one derived from the request `Host` /
/// `X-Forwarded-*` headers, which are fragile behind proxies. Store it in your
/// state and implement [`FromRef`] (or make it the state itself); a handler then
/// extracts it directly:
///
/// ```no_run
/// use axum::extract::FromRef;
/// use jsonapi_axum::BaseUrl;
///
/// #[derive(Clone)]
/// struct AppState { base_url: BaseUrl }
/// impl FromRef<AppState> for BaseUrl {
///     fn from_ref(state: &AppState) -> BaseUrl { state.base_url.clone() }
/// }
///
/// async fn handler(BaseUrl(base): BaseUrl) -> String { base }
/// ```
#[derive(Debug, Clone)]
pub struct BaseUrl(pub String);

impl<S> FromRequestParts<S> for BaseUrl
where
    S: Send + Sync,
    Self: FromRef<S>,
{
    type Rejection = Infallible;

    async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
        Ok(Self::from_ref(state))
    }
}

/// JSON:API query-parameter extractor (`sort`/`page`/`filter`/`fields`/
/// `include`). Does **not** validate include paths — use
/// [`JsonApiQueryValidated`] for that. Requires no application state.
#[derive(Debug, Clone)]
pub struct JsonApiQuery(pub Query);

impl<S> FromRequestParts<S> for JsonApiQuery
where
    S: Send + Sync,
{
    type Rejection = JsonApiError;

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        let query = parse_query(&parts.uri).map_err(|err| JsonApiError::from_core(&err))?;
        Ok(JsonApiQuery(query))
    }
}

/// JSON:API query extractor that additionally validates `include` paths against
/// an [`Arc<TypeRegistry>`] pulled from application state, rooted at `T`'s
/// resource type. Opt-in: apps without a registry use [`JsonApiQuery`] instead.
#[derive(Debug, Clone)]
pub struct JsonApiQueryValidated<T> {
    /// The parsed and include-validated query.
    pub query: Query,
    _marker: PhantomData<T>,
}

impl<T, S> FromRequestParts<S> for JsonApiQueryValidated<T>
where
    T: ResourceObject + 'static,
    S: Send + Sync,
    Arc<TypeRegistry>: FromRef<S>,
{
    type Rejection = JsonApiError;

    async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
        let query = parse_query(&parts.uri).map_err(|err| JsonApiError::from_core(&err))?;

        let registry = Arc::<TypeRegistry>::from_ref(state);
        let root = T::type_info().type_name;
        let includes: Vec<&str> = query.include.iter().map(String::as_str).collect();
        registry
            .validate_include_paths(root, &includes)
            .map_err(|err| JsonApiError::from_core(&err))?;

        Ok(JsonApiQueryValidated {
            query,
            _marker: PhantomData,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::Router;
    use axum::body::Body;
    use axum::routing::{get, post};
    use http::{Request, StatusCode, header};
    use jsonapi_core::Relationship;
    use serde_json::Value;
    use tower::ServiceExt;

    #[derive(Debug, Clone, jsonapi_core::JsonApi)]
    #[jsonapi(type = "people")]
    struct Person {
        #[jsonapi(id)]
        id: String,
        #[allow(dead_code)]
        name: String,
    }

    #[derive(Debug, Clone, jsonapi_core::JsonApi)]
    #[jsonapi(type = "articles")]
    struct Article {
        #[jsonapi(id)]
        id: String,
        #[allow(dead_code)]
        title: String,
        #[jsonapi(relationship, type = "people")]
        #[allow(dead_code)]
        author: Relationship<Person>,
    }

    const VALID_ARTICLE: &str = r#"{"data":{"type":"articles","id":"1",
        "attributes":{"title":"Hi"},
        "relationships":{"author":{"data":{"type":"people","id":"9"}}}}}"#;

    fn registry() -> Arc<TypeRegistry> {
        let mut registry = TypeRegistry::new();
        registry.register::<Article>().register::<Person>();
        Arc::new(registry)
    }

    async fn status_of(response: axum::response::Response) -> (StatusCode, Value) {
        let status = response.status();
        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        let value = if bytes.is_empty() {
            Value::Null
        } else {
            serde_json::from_slice(&bytes).unwrap_or(Value::Null)
        };
        (status, value)
    }

    // --- JsonApi<T> body extractor ---

    fn body_router() -> Router {
        async fn create(JsonApi(_doc): JsonApi<Article>) -> StatusCode {
            StatusCode::CREATED
        }
        Router::new().route("/articles", post(create))
    }

    fn post_request(content_type: &str, body: &str) -> Request<Body> {
        Request::builder()
            .method("POST")
            .uri("/articles")
            .header(header::CONTENT_TYPE, content_type)
            .body(Body::from(body.to_owned()))
            .unwrap()
    }

    #[test]
    fn body_extractor_accepts_valid_document() {
        pollster::block_on(async {
            let response = body_router()
                .oneshot(post_request("application/vnd.api+json", VALID_ARTICLE))
                .await
                .unwrap();
            assert_eq!(response.status(), StatusCode::CREATED);
        });
    }

    #[test]
    fn body_extractor_rejects_wrong_content_type_with_415() {
        pollster::block_on(async {
            let response = body_router()
                .oneshot(post_request("application/json", VALID_ARTICLE))
                .await
                .unwrap();
            let (status, _) = status_of(response).await;
            assert_eq!(status, StatusCode::UNSUPPORTED_MEDIA_TYPE);
        });
    }

    #[test]
    fn body_extractor_maps_body_limit_to_413() {
        pollster::block_on(async {
            // A tiny DefaultBodyLimit layer makes axum's Bytes extractor reject an
            // oversized body with a length-limit rejection; that must surface as a
            // JSON:API 413, not a generic 400.
            use axum::extract::DefaultBodyLimit;
            let router = body_router().layer(DefaultBodyLimit::max(8));
            let big_body = format!(
                r#"{{"data":{{"type":"articles","id":"1","attributes":{{"title":"{}"}}}}}}"#,
                "x".repeat(256)
            );
            let response = router
                .oneshot(post_request("application/vnd.api+json", &big_body))
                .await
                .unwrap();
            let (status, json) = status_of(response).await;
            assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
            assert_eq!(json["errors"][0]["status"], "413");
        });
    }

    #[test]
    fn body_extractor_maps_missing_attribute_to_422_with_pointer() {
        pollster::block_on(async {
            let body = r#"{"data":{"type":"articles","id":"1","attributes":{},
                "relationships":{"author":{"data":{"type":"people","id":"9"}}}}}"#;
            let response = body_router()
                .oneshot(post_request("application/vnd.api+json", body))
                .await
                .unwrap();
            let (status, json) = status_of(response).await;
            assert_eq!(status, StatusCode::UNPROCESSABLE_ENTITY);
            assert_eq!(
                json["errors"][0]["source"]["pointer"],
                "/data/attributes/title"
            );
        });
    }

    #[test]
    fn body_extractor_maps_type_mismatch_to_409_with_pointer() {
        pollster::block_on(async {
            // Wire `type` is "people" but the route deserializes into `Article`.
            let body = r#"{"data":{"type":"people","id":"1","attributes":{"title":"x"},
                "relationships":{"author":{"data":{"type":"people","id":"9"}}}}}"#;
            let response = body_router()
                .oneshot(post_request("application/vnd.api+json", body))
                .await
                .unwrap();
            let (status, json) = status_of(response).await;
            assert_eq!(status, StatusCode::CONFLICT);
            assert_eq!(json["errors"][0]["source"]["pointer"], "/data/type");
        });
    }

    // --- JsonApiQuery (no state) ---

    #[test]
    fn query_extractor_parses_values_through_to_the_handler() {
        pollster::block_on(async {
            // Reflect the parsed query into the body so we assert the values
            // actually reached the handler, not merely that extraction returned OK.
            async fn list(JsonApiQuery(q): JsonApiQuery) -> String {
                let field = &q.sort[0];
                let page_size = q.page.get("size").map(String::as_str).unwrap_or("none");
                format!(
                    "sort={}:{} page_size={page_size}",
                    field.field, field.descending
                )
            }
            let router = Router::new().route("/articles", get(list));
            let request = Request::builder()
                .uri("/articles?sort=-created&page[size]=2")
                .body(Body::empty())
                .unwrap();
            let (status, body) = {
                let response = router.oneshot(request).await.unwrap();
                let status = response.status();
                let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
                    .await
                    .unwrap();
                (status, String::from_utf8(bytes.to_vec()).unwrap())
            };
            assert_eq!(status, StatusCode::OK);
            assert_eq!(body, "sort=created:true page_size=2");
        });
    }

    #[test]
    fn query_extractor_rejects_malformed_param_with_400() {
        pollster::block_on(async {
            async fn list(JsonApiQuery(_q): JsonApiQuery) -> StatusCode {
                StatusCode::OK
            }
            let router = Router::new().route("/articles", get(list));
            // `fields[` has no closing bracket -> QueryParse error.
            let request = Request::builder()
                .uri("/articles?fields[=title")
                .body(Body::empty())
                .unwrap();
            let response = router.oneshot(request).await.unwrap();
            assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        });
    }

    // --- JsonApiQueryValidated<T> (registry from state) ---

    fn validated_router() -> Router {
        async fn list(_q: JsonApiQueryValidated<Article>) -> StatusCode {
            StatusCode::OK
        }
        Router::new()
            .route("/articles", get(list))
            .with_state(registry())
    }

    #[test]
    fn validated_query_accepts_known_include() {
        pollster::block_on(async {
            let request = Request::builder()
                .uri("/articles?include=author")
                .body(Body::empty())
                .unwrap();
            let response = validated_router().oneshot(request).await.unwrap();
            assert_eq!(response.status(), StatusCode::OK);
        });
    }

    #[test]
    fn validated_query_rejects_unknown_include_with_400() {
        pollster::block_on(async {
            let request = Request::builder()
                .uri("/articles?include=bogus")
                .body(Body::empty())
                .unwrap();
            let response = validated_router().oneshot(request).await.unwrap();
            let (status, json) = status_of(response).await;
            assert_eq!(status, StatusCode::BAD_REQUEST);
            assert_eq!(json["errors"][0]["source"]["parameter"], Value::Null);
            assert!(
                json["errors"][0]["detail"]
                    .as_str()
                    .unwrap()
                    .contains("bogus")
            );
        });
    }

    // --- NegotiatedMediaType (G10) ---

    const TEST_PROFILE: &str = "https://example.com/p";

    async fn body_text(response: axum::response::Response) -> String {
        let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
            .await
            .unwrap();
        String::from_utf8(bytes.to_vec()).unwrap()
    }

    #[test]
    fn negotiated_media_type_reflects_accept_layer_profile() {
        pollster::block_on(async {
            // Reflect the negotiated profile into the body so we prove the layer's
            // stored media type reached the extractor.
            async fn handler(NegotiatedMediaType(media): NegotiatedMediaType) -> String {
                media.profile.join(",")
            }
            let router = Router::new()
                .route("/x", get(handler))
                .layer(crate::AcceptLayer::new().profile([TEST_PROFILE]));
            let request = Request::builder()
                .uri("/x")
                .header(
                    header::ACCEPT,
                    format!("application/vnd.api+json; profile=\"{TEST_PROFILE}\""),
                )
                .body(Body::empty())
                .unwrap();
            let response = router.oneshot(request).await.unwrap();
            assert_eq!(response.status(), StatusCode::OK);
            assert_eq!(body_text(response).await, TEST_PROFILE);
        });
    }

    #[test]
    fn negotiated_media_type_without_layer_falls_back_to_plain() {
        pollster::block_on(async {
            async fn handler(NegotiatedMediaType(media): NegotiatedMediaType) -> String {
                (media == JsonApiMediaType::plain()).to_string()
            }
            // No AcceptLayer: nothing stored in extensions.
            let router = Router::new().route("/x", get(handler));
            let request = Request::builder().uri("/x").body(Body::empty()).unwrap();
            let response = router.oneshot(request).await.unwrap();
            assert_eq!(body_text(response).await, "true");
        });
    }

    #[test]
    fn negotiated_media_type_drives_response_content_type_end_to_end() {
        pollster::block_on(async {
            use crate::JsonApiResponse;
            async fn handler(
                NegotiatedMediaType(media): NegotiatedMediaType,
            ) -> JsonApiResponse<jsonapi_core::Resource> {
                let document: Document<jsonapi_core::Resource> =
                    serde_json::from_str(r#"{"data":{"type":"articles","id":"1"}}"#).unwrap();
                JsonApiResponse::new(document).media_type(media)
            }
            let router = Router::new()
                .route("/x", get(handler))
                .layer(crate::AcceptLayer::new().profile([TEST_PROFILE]));
            let request = Request::builder()
                .uri("/x")
                .header(
                    header::ACCEPT,
                    format!("application/vnd.api+json; profile=\"{TEST_PROFILE}\""),
                )
                .body(Body::empty())
                .unwrap();
            let response = router.oneshot(request).await.unwrap();
            let content_type = response
                .headers()
                .get(header::CONTENT_TYPE)
                .and_then(|v| v.to_str().ok())
                .unwrap()
                .to_string();
            assert_eq!(
                content_type,
                format!("application/vnd.api+json; profile=\"{TEST_PROFILE}\"")
            );
        });
    }
}