rustango 0.43.0

Django-shaped batteries-included web framework for Rust: ORM + migrations + auto-admin + multi-tenancy + audit log + auth (sessions, JWT, OAuth2/OIDC, HMAC) + APIs (ViewSet, OpenAPI auto-derive, JSON:API) + jobs (in-mem + Postgres) + email + media (S3 / R2 / B2 / MinIO + presigned uploads + collections + tags) + production middleware (CSRF, CSP, rate-limiting, compression, idempotency, etc.).
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
//! DRF-style serializer layer — typed JSON output from model instances.
//!
//! A serializer is a Rust struct that maps a [`Model`] instance to a
//! JSON-ready shape, with per-field control over what is included,
//! renamed, or excluded.
//!
//! ## Quick start
//!
//! ```ignore
//! use rustango::Serializer;
//! use rustango::serializer::ModelSerializer;
//!
//! #[derive(Serializer, serde::Deserialize, Default)]
//! #[serializer(model = Post)]
//! pub struct PostSerializer {
//!     pub id:         i64,
//!     pub title:      String,
//!     #[serializer(read_only)]
//!     pub created_at: chrono::DateTime<chrono::Utc>,
//!     #[serializer(write_only)]
//!     pub secret:     String,
//!     #[serializer(source = "body")]
//!     pub content:    String,
//!     #[serializer(skip)]
//!     pub tag_ids:    Vec<i64>,   // set manually: s.tag_ids = post.tags_m2m().all(&pool).await?
//! }
//!
//! // Serialize:
//! let s = PostSerializer::from_model(&post);
//! let json = s.to_value();
//!
//! // Serialize many:
//! let json_array = PostSerializer::many_to_value(&posts);
//! ```
//!
//! ## Field attributes
//!
//! | Attribute | Effect on `from_model` | Effect on JSON output | Effect on `writable_fields` |
//! |---|---|---|---|
//! | *(none)* | mapped from model | included | yes |
//! | `read_only` | mapped from model | included | no |
//! | `write_only` | `Default::default()` | excluded | yes |
//! | `source = "x"` | mapped from `model.x` | included | yes |
//! | `skip` | `Default::default()` | included | no |
//! | `method = "fn"` | calls `Self::fn(&model)` | included | no |
//! | `nested` | reads `model.<field>.value()` then `Child::from_model(parent)` | included | no |
//! | `nested(strict)` | same, but panics on unloaded FK | included | no |
//! | `many = TagSerializer` | initializes to `Vec::new()`; populate via `set_<field>(&[Tag])` helper | included | no |
//! | `slug = "name"` | clones `model.<source>.value()?.name` (DRF SlugRelatedField) | included | no |
//! | `validate = "fn"` | per-field validator called by `Self::validate(&self)` | n/a | n/a |
//!
//! ## Nested serializers — auto-resolved via `#[serializer(nested)]`
//!
//! When the field type is another serializer and the model's FK is
//! already loaded (via `select_related`), the macro emits a `from_model`
//! initializer that walks the FK automatically:
//!
//! ```ignore
//! #[derive(Serializer, serde::Deserialize, Default)]
//! #[serializer(model = Post)]
//! struct PostWithAuthor {
//!     pub id: i64,
//!     pub title: String,
//!     #[serializer(nested)]
//!     pub author: AuthorSerializer,
//! }
//! ```
//!
//! If the FK was *not* loaded (no `select_related`), the field falls
//! back to `Default::default()` rather than panicking — production
//! degrades gracefully. Use `#[serializer(nested(strict))]` to opt
//! back into the v0.18.1 panic-on-unloaded behaviour for tests.
//!
//! For lists of children (one-to-many / M2M), use
//! `#[serializer(many = ChildSerializer)]`. The macro emits a
//! `set_<field>(&[Child])` setter; the caller fetches the children
//! and calls it after `from_model` (auto-load isn't possible because
//! the M2M accessor is async).
//!
//! ## Computed fields — `#[serializer(method = "fn")]`
//!
//! DRF `SerializerMethodField` analog. The macro emits a `from_model`
//! initializer that calls `Self::fn(&model)`:
//!
//! ```ignore
//! impl PostSerializer {
//!     fn excerpt(model: &Post) -> String {
//!         model.body.chars().take(80).collect::<String>() + "…"
//!     }
//! }
//!
//! #[derive(Serializer, serde::Deserialize, Default)]
//! #[serializer(model = Post)]
//! struct PostSerializer {
//!     pub title: String,
//!     #[serializer(method = "excerpt")]
//!     pub excerpt: String,
//! }
//! ```
//!
//! ## Validation
//!
//! Cross-field validation: implement `validate(&self)` as an inherent
//! method on the serializer struct:
//!
//! ```ignore
//! impl PostSerializer {
//!     pub fn validate(&self) -> Result<(), rustango::forms::FormErrors> {
//!         let mut errors = rustango::forms::FormErrors::default();
//!         if self.title.is_empty() {
//!             errors.add("title", "title cannot be empty");
//!         }
//!         if errors.is_empty() { Ok(()) } else { Err(errors) }
//!     }
//! }
//! ```
//!
//! Per-field validators: declare `#[serializer(validate = "fn_name")]`
//! on the field and write `fn fn_name(value: &T) -> Result<(), String>`
//! as an associated method. The macro-generated `validate(&self)`
//! aggregates per-field results into a `FormErrors`.

use serde_json::Value;

/// Core serializer trait. Implemented by `#[derive(Serializer)]` structs.
///
/// # Required implementations
///
/// The derive macro generates:
/// - `from_model` — maps a model instance to the serializer struct
/// - `writable_fields` — field names accepted on create/update (excludes `read_only` and `skip`)
///
/// # Default implementations
///
/// - `to_value` — calls `serde::Serialize` (which the macro also emits, skipping `write_only` fields)
/// - `many` / `many_to_value` — batch `from_model` calls
/// - `validate` — no-op; override to add cross-field validation
pub trait ModelSerializer: serde::Serialize + Sized {
    /// The [`crate::core::Model`] type this serializer maps from.
    type Model;

    /// Construct a serializer from a model instance.
    ///
    /// `read_only` and normal fields are cloned from the model.
    /// `write_only` and `skip` fields are `Default::default()` —
    /// set them manually after calling this if needed.
    fn from_model(model: &Self::Model) -> Self;

    /// Serialize this instance to a JSON value.
    ///
    /// Uses the `serde::Serialize` implementation emitted by the derive
    /// macro, which respects `write_only` (those fields are excluded).
    fn to_value(&self) -> Value {
        serde_json::to_value(self).unwrap_or(Value::Null)
    }

    /// Serialize a slice of model instances into a `Vec` of serializers.
    fn many(models: &[Self::Model]) -> Vec<Self> {
        models.iter().map(Self::from_model).collect()
    }

    /// Serialize a slice of model instances directly to a JSON array.
    fn many_to_value(models: &[Self::Model]) -> Value {
        Value::Array(
            models
                .iter()
                .map(|m| Self::from_model(m).to_value())
                .collect(),
        )
    }

    /// Field names accepted on create/update requests (excludes `read_only`
    /// and `skip` fields). Used by the ViewSet write path to filter the
    /// incoming JSON body.
    fn writable_fields() -> &'static [&'static str];
}

/// Django-shape `UniqueTogetherValidator` — pre-save check that a
/// candidate row doesn't collide with an existing row on any of the
/// model's declared `unique_together` constraints. Issue #437.
///
/// Returns `Ok(())` when no collision is detected. Returns `Err(FormErrors)`
/// with a non-field error per colliding constraint (DRF shape:
/// `"The fields a, b must be unique together"`).
///
/// ## Usage
///
/// ```ignore
/// use std::collections::HashMap;
/// use rustango::core::SqlValue;
/// use rustango::serializer::check_unique_together_pool;
///
/// let mut values: HashMap<&'static str, SqlValue> = HashMap::new();
/// values.insert("org_id", SqlValue::I64(self.org_id));
/// values.insert("user_id", SqlValue::I64(self.user_id));
/// check_unique_together_pool(&pool, Membership::SCHEMA, &values, None).await?;
/// ```
///
/// Pass `exclude_pk = Some(&pk)` on updates so the row being edited
/// doesn't collide with itself. The PK column is read off
/// `ModelSchema::primary_key()` — pass `None` for inserts.
///
/// Partial unique constraints (`unique_when` / `WHERE`-clause partial
/// indexes) are skipped — their conflict semantics depend on the
/// predicate, which this layer doesn't evaluate.
///
/// # Errors
/// - [`crate::sql::ExecError`] forwarded from the underlying query.
/// - The check is non-fatal on errors: a query failure surfaces as
///   `Err` rather than masking as "no collision".
pub async fn check_unique_together_pool(
    pool: &crate::sql::Pool,
    schema: &'static crate::core::ModelSchema,
    values: &std::collections::HashMap<&'static str, crate::core::SqlValue>,
    exclude_pk: Option<&crate::core::SqlValue>,
) -> Result<(), crate::forms::FormErrors> {
    use crate::core::{Filter, Op};

    let mut errors = crate::forms::FormErrors::default();

    for index in schema.indexes {
        // Only multi-column unique indexes — Django's `unique_together`.
        // Single-column UNIQUE is the `unique` field attr (caught by
        // INSERT's RETURNING-on-conflict). Partial unique
        // (`where_clause = Some(_)`) skipped — predicate evaluation
        // would need a stub planner we don't have today.
        if !index.unique || index.columns.len() < 2 || index.where_clause.is_some() {
            continue;
        }

        // Build the AND-of-equality WHERE clause. Missing column values
        // skip the constraint — Django's behavior when only a partial
        // subset of the unique-together fields is bound.
        let mut predicates: Vec<Filter> = Vec::with_capacity(index.columns.len());
        let mut all_bound = true;
        for col in index.columns {
            let Some(val) = values.get(*col) else {
                all_bound = false;
                break;
            };
            predicates.push(Filter {
                column: col,
                op: Op::Eq,
                value: val.clone(),
            });
        }
        if !all_bound {
            continue;
        }

        // Exclude-self on updates: PK != $N.
        if let (Some(pk_field), Some(pk_value)) = (schema.primary_key(), exclude_pk) {
            predicates.push(Filter {
                column: pk_field.column,
                op: Op::Ne,
                value: pk_value.clone(),
            });
        }

        // SELECT 1 FROM table WHERE … LIMIT 1.
        let dialect = pool.dialect();
        let table_q = dialect.quote_ident(schema.table);
        let mut clauses: Vec<String> = Vec::with_capacity(predicates.len());
        let mut params: Vec<crate::core::SqlValue> = Vec::with_capacity(predicates.len());
        for (i, pred) in predicates.iter().enumerate() {
            let col = dialect.quote_ident(pred.column);
            let op_str = match pred.op {
                Op::Eq => "=",
                Op::Ne => "<>",
                _ => unreachable!("only Eq/Ne above"),
            };
            let placeholder = dialect.placeholder(i + 1);
            clauses.push(format!("{col} {op_str} {placeholder}"));
            params.push(pred.value.clone());
        }
        let where_sql = clauses.join(" AND ");
        let sql = format!("SELECT 1 FROM {table_q} WHERE {where_sql} LIMIT 1");

        // Use `raw_query_pool::<(i64,)>` and ignore the actual returned
        // value — presence of any row means a collision.
        let hits: Vec<(i64,)> = crate::sql::raw_query_pool(&sql, params, pool)
            .await
            .map_err(|e| {
                let mut errs = crate::forms::FormErrors::default();
                errs.add_non_field(format!("unique_together check failed: {e}"));
                errs
            })?;
        if !hits.is_empty() {
            errors.add_non_field(format!(
                "The fields {} must be unique together.",
                index.columns.join(", "),
            ));
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

// ============================================================ #434
//
// Django/DRF-shape `HyperlinkedModelSerializer`. Where a regular
// serializer emits PKs (`{"id": 42, "author_id": 7}`), a
// hyperlinked one emits resource URLs (`{"url": "/api/posts/42",
// "author_url": "/api/users/7"}`). rustango's Serializer derive
// is already rich enough that the user can roll their own with
// `#[serializer(method = "url")]` + a manual `fn url(&self) -> String`,
// but that's boilerplate-heavy for the common case. These free
// functions cover the 95% case — substitute `{pk}` placeholders
// in a URL template.

/// Substitute `{pk}` in `template` with the formatted PK value.
///
/// `pk` formats as: integers/floats render their numeric form,
/// strings/UUIDs render their `Display`, everything else falls
/// back to JSON encoding (rare in practice — most PK fields are
/// integer / UUID / string).
///
/// ```ignore
/// use rustango::core::SqlValue;
/// let url = rustango::serializer::hyperlink_url("/api/posts/{pk}", &SqlValue::I64(42));
/// assert_eq!(url, "/api/posts/42");
/// ```
///
/// Every `{pk}` occurrence is substituted — useful for nested
/// resource URLs.
#[must_use]
pub fn hyperlink_url(template: &str, pk: &crate::core::SqlValue) -> String {
    let pk_str = render_pk(pk);
    template.replace("{pk}", &pk_str)
}

/// Wrap a serializer's JSON output with a `url` field (from the
/// model's PK) and optional `<fk>_url` fields (from named FK
/// templates). Issue #434.
///
/// ```ignore
/// use rustango::serializer::{hyperlinked_to_value, Serializer};
/// use std::collections::HashMap;
///
/// let post = Post::objects().get_by_pk_pool(&pool, &42).await?;
/// let ser = PostSerializer::from_model(&post);
/// let base = ser.to_value();
///
/// let mut fk_templates = HashMap::new();
/// fk_templates.insert("author_id", "/api/users/{pk}");
///
/// let hyperlinked = hyperlinked_to_value(
///     base,
///     "/api/posts/{pk}",
///     "id",                  // pk field name on the serializer output
///     &fk_templates,
/// );
/// // {"url": "/api/posts/42", "author_id_url": "/api/users/7", ...}
/// ```
///
/// Behaviour:
///
/// - Adds a `url` field to the top-level object, derived by
///   substituting `{pk}` in `self_template` with `base[pk_field]`.
/// - For each `(fk_field, template)` in `fk_templates`, looks up
///   `base[fk_field]` and emits a sibling `<fk_field>_url` key
///   with the substituted URL. Null / missing FK values produce
///   a null URL (matches DRF's behavior on nullable FKs).
/// - Does NOT remove the original `id` / `<fk>_id` keys. DRF's
///   `HyperlinkedModelSerializer` also keeps them by default;
///   apps that want to redact them can `obj.as_object_mut()
///   .remove("id")` after this call.
///
/// Panics if `base` isn't a JSON object (the standard serializer
/// `to_value` always returns one).
#[must_use]
pub fn hyperlinked_to_value(
    mut base: serde_json::Value,
    self_template: &str,
    pk_field: &str,
    fk_templates: &std::collections::HashMap<&str, &str>,
) -> serde_json::Value {
    let obj = base
        .as_object_mut()
        .expect("hyperlinked_to_value: base must be a JSON object");

    // Self URL — derived from base[pk_field].
    if let Some(pk_val) = obj.get(pk_field) {
        let pk_str = render_pk_json(pk_val);
        let url = self_template.replace("{pk}", &pk_str);
        obj.insert("url".into(), serde_json::Value::String(url));
    }

    // FK URLs — one per (field, template) pair. Output key is
    // `<field>_url`. Missing / null FK values emit a JSON null URL.
    for (fk_field, template) in fk_templates {
        let url_key = format!("{fk_field}_url");
        match obj.get(*fk_field) {
            Some(v) if !v.is_null() => {
                let pk_str = render_pk_json(v);
                let url = template.replace("{pk}", &pk_str);
                obj.insert(url_key, serde_json::Value::String(url));
            }
            _ => {
                obj.insert(url_key, serde_json::Value::Null);
            }
        }
    }

    base
}

fn render_pk(pk: &crate::core::SqlValue) -> String {
    use crate::core::SqlValue;
    match pk {
        SqlValue::I16(v) => v.to_string(),
        SqlValue::I32(v) => v.to_string(),
        SqlValue::I64(v) => v.to_string(),
        SqlValue::F32(v) => v.to_string(),
        SqlValue::F64(v) => v.to_string(),
        SqlValue::String(s) => s.clone(),
        SqlValue::Uuid(u) => u.to_string(),
        // Other variants (Bool / Json / Date / DateTime / Decimal /
        // Binary / Time / Null / List / Array / RangeLiteral) are
        // not realistic PK shapes — fall back to the Debug
        // repr so the URL is at least non-empty, but don't try
        // hard.
        other => format!("{other:?}"),
    }
}

fn render_pk_json(v: &serde_json::Value) -> String {
    match v {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::Bool(b) => b.to_string(),
        other => other.to_string(),
    }
}

#[cfg(test)]
mod hyperlinked_tests {
    use super::*;

    #[test]
    fn hyperlink_url_substitutes_i64_pk() {
        let url = hyperlink_url("/api/posts/{pk}", &crate::core::SqlValue::I64(42));
        assert_eq!(url, "/api/posts/42");
    }

    #[test]
    fn hyperlink_url_substitutes_string_pk() {
        let url = hyperlink_url(
            "/users/{pk}",
            &crate::core::SqlValue::String("alice".into()),
        );
        assert_eq!(url, "/users/alice");
    }

    #[test]
    fn hyperlink_url_substitutes_every_occurrence() {
        let url = hyperlink_url("/posts/{pk}/comments/{pk}", &crate::core::SqlValue::I64(7));
        assert_eq!(url, "/posts/7/comments/7");
    }

    #[test]
    fn hyperlinked_to_value_adds_url_field_for_self() {
        let base = serde_json::json!({"id": 42, "title": "Hi"});
        let out = hyperlinked_to_value(
            base,
            "/api/posts/{pk}",
            "id",
            &std::collections::HashMap::new(),
        );
        assert_eq!(out["url"], "/api/posts/42");
        // Original fields stay.
        assert_eq!(out["id"], 42);
        assert_eq!(out["title"], "Hi");
    }

    #[test]
    fn hyperlinked_to_value_adds_fk_url_keys() {
        let base = serde_json::json!({
            "id": 1,
            "title": "Hi",
            "author_id": 7,
            "section_id": serde_json::Value::Null,
        });
        let mut fks: std::collections::HashMap<&str, &str> = std::collections::HashMap::new();
        fks.insert("author_id", "/users/{pk}");
        fks.insert("section_id", "/sections/{pk}");
        let out = hyperlinked_to_value(base, "/posts/{pk}", "id", &fks);
        assert_eq!(out["author_id_url"], "/users/7");
        // Null FK → null URL.
        assert!(out["section_id_url"].is_null());
    }

    #[test]
    fn hyperlinked_to_value_handles_missing_pk_key_gracefully() {
        // base has no `id` field → no `url` field emitted, but no
        // panic.
        let base = serde_json::json!({"title": "Hi"});
        let out = hyperlinked_to_value(
            base,
            "/api/posts/{pk}",
            "id",
            &std::collections::HashMap::new(),
        );
        assert_eq!(out.get("url"), None);
    }

    #[test]
    fn hyperlinked_to_value_supports_string_pk() {
        let base = serde_json::json!({"slug": "hello", "title": "Hello"});
        let out = hyperlinked_to_value(
            base,
            "/posts/{pk}",
            "slug",
            &std::collections::HashMap::new(),
        );
        assert_eq!(out["url"], "/posts/hello");
    }
}