use std::{collections::HashMap, future::Future, marker::PhantomData, pin::Pin, sync::Arc};
use axum::{
Extension, Json, Router,
extract::{Path, Query},
http::StatusCode,
response::IntoResponse,
routing::get,
};
use che_orm::{
Database, DatabaseQuery, Loaded, Model, ModelField, ModelSerializer, ModelWriteSerializer,
PrefetchRelatedQuery, QueryValue, SelectRelatedQuery, SerializerField, ValidatedWrite, WithOne,
WithOptionalOne, WriteMode,
};
use serde::Serialize;
use serde_json::json;
use super::extensions::MutationHook;
use crate::{AppError, AppResult, AppState, auth::CurrentPrincipal};
#[derive(Clone)]
pub struct MutationHooks<V: ViewSet>(Vec<Arc<dyn MutationHook<V>>>);
impl<V: ViewSet> MutationHooks<V> {
fn after_commit(
&self,
state: &AppState,
principal: Option<&CurrentPrincipal>,
action: ViewAction,
model: &V::Model,
) -> AppResult<()> {
for hook in &self.0 {
hook.after_commit(state, principal, action, model)?;
}
Ok(())
}
fn after_delete(&self, state: &AppState, values: &[serde_json::Value]) {
for (hook, value) in self.0.iter().zip(values) {
if let Err(error) = hook.after_delete(state, value) {
eprintln!(
"post-delete hook {} failed after commit: {error}",
hook.id()
);
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewAction {
List,
Retrieve,
Create,
Update,
Patch,
Delete,
}
pub trait Permission<M: Model>: Clone + Send + Sync + Default + 'static {
fn check(
&self,
_state: &AppState,
_current: Option<&CurrentPrincipal>,
_action: ViewAction,
) -> AppResult<()> {
Ok(())
}
fn check_object(
&self,
_state: &AppState,
_current: Option<&CurrentPrincipal>,
_action: ViewAction,
_model: &M,
) -> AppResult<()> {
Ok(())
}
}
#[derive(Clone, Copy, Default)]
pub struct AllowAny;
impl<M: Model> Permission<M> for AllowAny {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Lookup {
Exact,
Contains,
Gt,
Gte,
Lt,
Lte,
}
#[derive(Debug, thiserror::Error)]
pub enum FilterError {
#[error("unknown filter: {0}")]
UnknownFilter(String),
#[error("unknown ordering field: {0}")]
UnknownOrdering(String),
#[error("invalid value for {field}, expected {expected}")]
InvalidValue {
field: String,
expected: &'static str,
},
}
pub trait FilterValue: QueryValue<Self> + Sized {
const EXPECTED: &'static str;
fn parse(value: &str) -> Result<Self, FilterError>;
}
impl FilterValue for i64 {
const EXPECTED: &'static str = "integer";
fn parse(v: &str) -> Result<Self, FilterError> {
v.parse().map_err(|_| FilterError::InvalidValue {
field: String::new(),
expected: Self::EXPECTED,
})
}
}
impl FilterValue for bool {
const EXPECTED: &'static str = "boolean";
fn parse(v: &str) -> Result<Self, FilterError> {
match v {
"true" | "1" => Ok(true),
"false" | "0" => Ok(false),
_ => Err(FilterError::InvalidValue {
field: String::new(),
expected: Self::EXPECTED,
}),
}
}
}
impl FilterValue for String {
const EXPECTED: &'static str = "string";
fn parse(v: &str) -> Result<Self, FilterError> {
Ok(v.to_owned())
}
}
impl FilterValue for time::OffsetDateTime {
const EXPECTED: &'static str = "RFC3339 datetime";
fn parse(v: &str) -> Result<Self, FilterError> {
time::OffsetDateTime::parse(v, &time::format_description::well_known::Rfc3339).map_err(
|_| FilterError::InvalidValue {
field: String::new(),
expected: Self::EXPECTED,
},
)
}
}
impl<T: FilterValue> FilterValue for Option<T> {
const EXPECTED: &'static str = T::EXPECTED;
fn parse(value: &str) -> Result<Self, FilterError> {
T::parse(value).map(Some)
}
}
pub trait RestQuerySet: Sized + Send {
type Model: Model + Send + Sync + 'static;
type Item: Send + Sync + 'static;
fn item_model(item: &Self::Item) -> &Self::Model;
fn filter(self, expr: che_orm::Expr) -> Self;
fn order_by(self, order: che_orm::OrderBy) -> Self;
fn limit(self, limit: u64) -> Self;
fn offset(self, offset: u64) -> Self;
fn all<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Item>, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a;
fn count<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<usize, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(async move { Ok(<Self as RestQuerySet>::all(self, database).await?.len()) })
}
fn first<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<Option<Self::Item>, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(async move {
Ok(<Self as RestQuerySet>::all(self.limit(1), database)
.await?
.into_iter()
.next())
})
}
}
type Apply = fn(&'static str, &str) -> Result<che_orm::Expr, FilterError>;
type Order = fn(&'static str, bool) -> che_orm::OrderBy;
#[derive(Clone, Copy)]
pub struct Filter<M: Model> {
pub name: &'static str,
source: &'static str,
lookup: Lookup,
apply: Apply,
order: Order,
_marker: PhantomData<fn() -> M>,
}
impl<M: Model> Filter<M> {
pub const fn exact<T: FilterValue>(field: ModelField<M, T>) -> Self {
Self::typed(field, Lookup::Exact, apply_exact::<M, T>)
}
pub const fn exact_enum<T: che_orm::DbEnum>(field: ModelField<M, T>) -> Self {
Self::typed_enum(field, apply_enum_exact::<M, T>)
}
pub const fn contains(field: ModelField<M, String>) -> Self {
Self::typed(field, Lookup::Contains, apply_contains::<M>)
}
pub const fn gt<T: FilterValue>(field: ModelField<M, T>) -> Self {
Self::typed(field, Lookup::Gt, apply_gt::<M, T>)
}
pub const fn gte<T: FilterValue>(field: ModelField<M, T>) -> Self {
Self::typed(field, Lookup::Gte, apply_gte::<M, T>)
}
pub const fn lt<T: FilterValue>(field: ModelField<M, T>) -> Self {
Self::typed(field, Lookup::Lt, apply_lt::<M, T>)
}
pub const fn lte<T: FilterValue>(field: ModelField<M, T>) -> Self {
Self::typed(field, Lookup::Lte, apply_lte::<M, T>)
}
pub const fn exact_as<T: FilterValue>(name: &'static str, field: ModelField<M, T>) -> Self {
let mut result = Self::exact(field);
result.name = name;
result
}
const fn typed<T: FilterValue>(field: ModelField<M, T>, lookup: Lookup, apply: Apply) -> Self {
Self {
name: field.column().name,
source: field.column().name,
lookup,
apply,
order: apply_order::<M, T>,
_marker: PhantomData,
}
}
const fn typed_enum<T: che_orm::DbEnum>(field: ModelField<M, T>, apply: Apply) -> Self {
Self {
name: field.column().name,
source: field.column().name,
lookup: Lookup::Exact,
apply,
order: apply_order::<M, T>,
_marker: PhantomData,
}
}
fn matches(&self, name: &str) -> bool {
match self.lookup {
Lookup::Exact => name == self.name,
Lookup::Contains => name == format!("{}__contains", self.name),
Lookup::Gt => name == format!("{}__gt", self.name),
Lookup::Gte => name == format!("{}__gte", self.name),
Lookup::Lt => name == format!("{}__lt", self.name),
Lookup::Lte => name == format!("{}__lte", self.name),
}
}
pub const fn source(&self) -> &'static str {
self.source
}
pub const fn lookup(&self) -> Lookup {
self.lookup
}
}
pub trait FilterSetSpec: Clone + Send + Sync + 'static {
type Model: Model;
fn filters(&self) -> &'static [Filter<Self::Model>];
fn apply<Q>(&self, query: Q, params: &HashMap<String, String>) -> Result<Q, FilterError>
where
Q: RestQuerySet<Model = Self::Model>,
{
FilterSet::new(self.filters()).apply(query, params)
}
}
pub struct FilterSet<M: Model + 'static> {
filters: &'static [Filter<M>],
_marker: PhantomData<fn() -> M>,
}
impl<M: Model + 'static> Clone for FilterSet<M> {
fn clone(&self) -> Self {
*self
}
}
impl<M: Model + 'static> Copy for FilterSet<M> {}
impl<M: Model + 'static> FilterSet<M> {
pub const fn new(filters: &'static [Filter<M>]) -> Self {
Self {
filters,
_marker: PhantomData,
}
}
pub fn apply<Q>(&self, mut query: Q, params: &HashMap<String, String>) -> Result<Q, FilterError>
where
Q: RestQuerySet<Model = M>,
{
for (name, value) in params {
if matches!(name.as_str(), "limit" | "offset" | "ordering") {
continue;
}
let f = self
.filters
.iter()
.find(|f| f.matches(name))
.ok_or_else(|| FilterError::UnknownFilter(name.clone()))?;
query = query.filter((f.apply)(f.source, value).map_err(|e| match e {
FilterError::InvalidValue { expected, .. } => FilterError::InvalidValue {
field: name.clone(),
expected,
},
other => other,
})?);
}
if let Some(ordering) = params.get("ordering") {
let descending = ordering.starts_with('-');
let name = ordering.strip_prefix('-').unwrap_or(ordering);
let f = self
.filters
.iter()
.find(|f| f.name == name)
.ok_or_else(|| FilterError::UnknownOrdering(name.to_owned()))?;
query = query.order_by((f.order)(f.source, descending));
}
Ok(query)
}
}
impl<M: Model + 'static> Default for FilterSet<M> {
fn default() -> Self {
Self::new(&[])
}
}
impl<M: Model + 'static> FilterSetSpec for FilterSet<M> {
type Model = M;
fn filters(&self) -> &'static [Filter<M>] {
self.filters
}
}
fn apply_exact<M: Model, T: FilterValue>(
field: &'static str,
value: &str,
) -> Result<che_orm::Expr, FilterError> {
Ok(ModelField::<M, T>::new(M::table_name(), field).eq(T::parse(value)?))
}
fn apply_enum_exact<M: Model, T: che_orm::DbEnum>(
field: &'static str,
value: &str,
) -> Result<che_orm::Expr, FilterError> {
let parsed = T::from_str(value).ok_or(FilterError::InvalidValue {
field: String::new(),
expected: "enum value",
})?;
Ok(ModelField::<M, T>::new(M::table_name(), field).eq(parsed))
}
fn apply_contains<M: Model>(
field: &'static str,
value: &str,
) -> Result<che_orm::Expr, FilterError> {
Ok(ModelField::<M, String>::new(M::table_name(), field).contains(value))
}
macro_rules! range {
($fn:ident, $method:ident) => {
fn $fn<M: Model, T: FilterValue>(
field: &'static str,
value: &str,
) -> Result<che_orm::Expr, FilterError> {
Ok(ModelField::<M, T>::new(M::table_name(), field).$method(T::parse(value)?))
}
};
}
range!(apply_gt, gt);
range!(apply_gte, gte);
range!(apply_lt, lt);
range!(apply_lte, lte);
fn apply_order<M: Model, T>(field: &'static str, desc: bool) -> che_orm::OrderBy {
let f = ModelField::<M, T>::new(M::table_name(), field);
if desc { f.desc() } else { f.asc() }
}
impl<M> RestQuerySet for DatabaseQuery<M>
where
M: Model + Send + Sync + 'static,
{
type Model = M;
type Item = M;
fn item_model(item: &Self::Item) -> &Self::Model {
item
}
fn filter(self, expr: che_orm::Expr) -> Self {
DatabaseQuery::filter(self, expr)
}
fn order_by(self, order: che_orm::OrderBy) -> Self {
DatabaseQuery::order_by(self, order)
}
fn limit(self, limit: u64) -> Self {
DatabaseQuery::limit(self, limit)
}
fn offset(self, offset: u64) -> Self {
DatabaseQuery::offset(self, offset)
}
fn all<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Item>, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(DatabaseQuery::all(self, database))
}
fn count<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<usize, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(DatabaseQuery::count(self, database))
}
}
impl<M, R, Relation> RestQuerySet for SelectRelatedQuery<M, R, Relation, i64>
where
M: Model + Send + Sync + 'static,
R: Model<PrimaryKey = i64> + Send + Sync + 'static,
Relation: Send + Sync + 'static,
{
type Model = M;
type Item = WithOne<M, R, Relation>;
fn item_model(item: &Self::Item) -> &Self::Model {
&item.model
}
fn filter(self, expr: che_orm::Expr) -> Self {
SelectRelatedQuery::<M, R, Relation, i64>::filter(self, expr)
}
fn order_by(self, order: che_orm::OrderBy) -> Self {
SelectRelatedQuery::<M, R, Relation, i64>::order_by(self, order)
}
fn limit(self, limit: u64) -> Self {
SelectRelatedQuery::<M, R, Relation, i64>::limit(self, limit)
}
fn offset(self, offset: u64) -> Self {
SelectRelatedQuery::<M, R, Relation, i64>::offset(self, offset)
}
fn all<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Item>, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(SelectRelatedQuery::<M, R, Relation, i64>::all(
self, database,
))
}
fn count<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<usize, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(SelectRelatedQuery::<M, R, Relation, i64>::count(
self, database,
))
}
}
impl<M, R, Relation> RestQuerySet for SelectRelatedQuery<M, R, Relation, Option<i64>>
where
M: Model + Send + Sync + 'static,
R: Model<PrimaryKey = i64> + Send + Sync + 'static,
Relation: Send + Sync + 'static,
{
type Model = M;
type Item = WithOptionalOne<M, R, Relation>;
fn item_model(item: &Self::Item) -> &Self::Model {
&item.model
}
fn filter(self, expr: che_orm::Expr) -> Self {
SelectRelatedQuery::<M, R, Relation, Option<i64>>::filter(self, expr)
}
fn order_by(self, order: che_orm::OrderBy) -> Self {
SelectRelatedQuery::<M, R, Relation, Option<i64>>::order_by(self, order)
}
fn limit(self, limit: u64) -> Self {
SelectRelatedQuery::<M, R, Relation, Option<i64>>::limit(self, limit)
}
fn offset(self, offset: u64) -> Self {
SelectRelatedQuery::<M, R, Relation, Option<i64>>::offset(self, offset)
}
fn all<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Item>, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(SelectRelatedQuery::<M, R, Relation, Option<i64>>::all(
self, database,
))
}
fn count<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<usize, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(SelectRelatedQuery::<M, R, Relation, Option<i64>>::count(
self, database,
))
}
}
impl<M, R, Relation> RestQuerySet for PrefetchRelatedQuery<M, R, Relation, i64>
where
M: Model<PrimaryKey = i64> + Send + Sync + 'static,
R: Model + Send + Sync + 'static,
Relation: Send + Sync + 'static,
{
type Model = M;
type Item = Loaded<M, (che_orm::LoadedMany<R, Relation>,)>;
fn item_model(item: &Self::Item) -> &Self::Model {
&item.model
}
fn filter(self, expr: che_orm::Expr) -> Self {
PrefetchRelatedQuery::filter(self, expr)
}
fn order_by(self, order: che_orm::OrderBy) -> Self {
PrefetchRelatedQuery::order_by(self, order)
}
fn limit(self, limit: u64) -> Self {
PrefetchRelatedQuery::limit(self, limit)
}
fn offset(self, offset: u64) -> Self {
PrefetchRelatedQuery::offset(self, offset)
}
fn all<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<Vec<Self::Item>, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(PrefetchRelatedQuery::all(self, database))
}
fn count<'a>(
self,
database: &'a Database,
) -> Pin<Box<dyn Future<Output = Result<usize, che_orm::OrmError>> + Send + 'a>>
where
Self: 'a,
{
Box::pin(PrefetchRelatedQuery::count(self, database))
}
}
pub trait ViewSet: Clone + Send + Sync + 'static {
type Model: Model<PrimaryKey = i64> + Send + Sync + 'static;
type Serializer: ModelSerializer<Model = Self::Model>
+ ModelWriteSerializer<Model = Self::Model>
+ Serialize
+ Send
+ Sync
+ 'static;
type ListSerializer: ModelSerializer<Model = Self::Model, Input = <Self::Serializer as ModelSerializer>::Input>
+ Serialize
+ Send
+ Sync
+ 'static;
type QuerySet: RestQuerySet<Model = Self::Model, Item = <Self::Serializer as ModelSerializer>::Input>
+ Send
+ Sync;
type FilterSet: FilterSetSpec<Model = Self::Model> + Default;
type Permission: Permission<Self::Model>;
fn path(&self) -> &'static str;
fn filterset(&self) -> Self::FilterSet {
Default::default()
}
fn get_queryset(&self) -> Self::QuerySet;
fn prepare_create(
&self,
_state: &AppState,
_current: Option<&CurrentPrincipal>,
write: ValidatedWrite<Self::Model>,
) -> AppResult<ValidatedWrite<Self::Model>> {
Ok(write)
}
fn prepare_update(
&self,
_state: &AppState,
_principal: Option<&CurrentPrincipal>,
_current: &Self::Model,
write: ValidatedWrite<Self::Model>,
) -> AppResult<ValidatedWrite<Self::Model>> {
Ok(write)
}
fn prepare_patch(
&self,
_state: &AppState,
_principal: Option<&CurrentPrincipal>,
_current: &Self::Model,
write: ValidatedWrite<Self::Model>,
) -> AppResult<ValidatedWrite<Self::Model>> {
Ok(write)
}
fn configure(&self, _config: &mut crate::ViewSetConfig<Self>) -> AppResult<()>
where
Self: Sized,
{
Ok(())
}
fn signal_name(&self, action: ViewAction) -> Option<String> {
let resource = self.path().trim_matches('/').replace('/', ".");
match action {
ViewAction::Create => Some(format!("{resource}.created")),
ViewAction::Update | ViewAction::Patch => Some(format!("{resource}.updated")),
ViewAction::Delete => Some(format!("{resource}.deleted")),
ViewAction::List | ViewAction::Retrieve => None,
}
}
fn signal_access(&self, _action: ViewAction) -> Option<crate::SignalAccess> {
None
}
}
fn normalize_datetime_fields(data: &mut serde_json::Value, fields: &[SerializerField]) {
let Some(object) = data.as_object_mut() else {
return;
};
for field in fields {
if !field.rust_type.contains("OffsetDateTime") {
continue;
}
let Some(value) = object.get_mut(field.name) else {
continue;
};
let Some(input) = value.as_str() else {
continue;
};
if input.len() != "YYYY-MM-DD".len() {
continue;
}
let Ok(date) =
time::Date::parse(input, &time::format_description::well_known::Iso8601::DATE)
else {
continue;
};
let timestamp = date
.with_hms(0, 0, 0)
.expect("midnight is always a valid time")
.assume_utc()
.format(&time::format_description::well_known::Rfc3339)
.expect("UTC timestamps can always be formatted as RFC 3339");
*value = serde_json::Value::String(timestamp);
}
}
pub struct CrudViewSet<M, S> {
path: &'static str,
_marker: PhantomData<fn() -> (M, S)>,
}
impl<M, S> Clone for CrudViewSet<M, S> {
fn clone(&self) -> Self {
Self {
path: self.path,
_marker: PhantomData,
}
}
}
impl<M, S> CrudViewSet<M, S> {
pub const fn new(path: &'static str) -> Self {
Self {
path,
_marker: PhantomData,
}
}
}
impl<M, S> ViewSet for CrudViewSet<M, S>
where
M: Model<PrimaryKey = i64> + Send + Sync + 'static,
S: ModelSerializer<Model = M, Input = M>
+ ModelWriteSerializer<Model = M>
+ Serialize
+ Send
+ Sync
+ 'static,
{
type Model = M;
type Serializer = S;
type ListSerializer = S;
type QuerySet = DatabaseQuery<M>;
type FilterSet = FilterSet<M>;
type Permission = AllowAny;
fn get_queryset(&self) -> Self::QuerySet {
DatabaseQuery::new(M::query())
}
fn path(&self) -> &'static str {
self.path
}
}
pub fn router<V: ViewSet>(state: AppState, viewset: V, extensions: Router) -> Router
where
V::Serializer: Serialize,
{
router_with_hooks(state, viewset, extensions, Vec::new())
}
pub fn router_with_hooks<V: ViewSet>(
state: AppState,
viewset: V,
extensions: Router,
hooks: Vec<Arc<dyn MutationHook<V>>>,
) -> Router
where
V::Serializer: Serialize,
{
let path = viewset.path().trim_end_matches('/');
Router::new()
.route(&format!("{path}/"), get(list::<V>).post(create::<V>))
.route(
&format!("{path}/{{id}}/"),
get(retrieve::<V>)
.put(update::<V>)
.patch(patch::<V>)
.delete(destroy::<V>),
)
.merge(extensions)
.layer(Extension(state))
.layer(Extension(viewset))
.layer(Extension(MutationHooks::<V>(hooks)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_signal_names_normalize_nested_paths() {
let viewset =
CrudViewSet::<crate::auth::User, crate::auth::AdminUserSerializer>::new("/auth/users");
assert_eq!(
viewset.signal_name(ViewAction::Create).as_deref(),
Some("auth.users.created")
);
assert_eq!(
viewset.signal_name(ViewAction::Update).as_deref(),
Some("auth.users.updated")
);
assert_eq!(
viewset.signal_name(ViewAction::Delete).as_deref(),
Some("auth.users.deleted")
);
}
#[test]
fn count_params_exclude_ordering() {
let params = HashMap::from([
("ordering".to_owned(), "-created_at".to_owned()),
("name__contains".to_owned(), "task".to_owned()),
("limit".to_owned(), "50".to_owned()),
]);
let count_params = count_params(¶ms);
assert!(!count_params.contains_key("ordering"));
assert_eq!(count_params.get("name__contains"), Some(&"task".to_owned()));
assert_eq!(count_params.get("limit"), Some(&"50".to_owned()));
}
#[test]
fn datetime_fields_accept_iso_dates_as_utc_midnight() {
let fields = [SerializerField {
name: "work_date",
source: "work_date",
read_only: false,
write_only: false,
rust_type: "time::OffsetDateTime",
related_model: None,
many: false,
}];
let mut data = json!({"work_date": "2026-08-28", "title": "Entry"});
normalize_datetime_fields(&mut data, &fields);
assert_eq!(data["work_date"], "2026-08-28T00:00:00Z");
assert_eq!(data["title"], "Entry");
}
#[test]
fn datetime_fields_leave_rfc3339_and_other_fields_unchanged() {
let fields = [
SerializerField {
name: "due_at",
source: "due_at",
read_only: false,
write_only: false,
rust_type: "Option<time::OffsetDateTime>",
related_model: None,
many: false,
},
SerializerField {
name: "title",
source: "title",
read_only: false,
write_only: false,
rust_type: "String",
related_model: None,
many: false,
},
];
let mut data = json!({
"due_at": "2026-08-28T13:45:00+03:00",
"title": "2026-08-28"
});
normalize_datetime_fields(&mut data, &fields);
assert_eq!(data["due_at"], "2026-08-28T13:45:00+03:00");
assert_eq!(data["title"], "2026-08-28");
}
}
pub fn openapi_json_for<M, S>(path: &str) -> serde_json::Value
where
M: Model,
S: ModelSerializer<Model = M> + ModelWriteSerializer<Model = M> + Serialize,
{
openapi_json_for_with_list::<M, S>(path, S::fields())
}
pub fn openapi_json_for_with_list<M, S>(
path: &str,
list_fields: &[SerializerField],
) -> serde_json::Value
where
M: Model,
S: ModelSerializer<Model = M> + ModelWriteSerializer<Model = M> + Serialize,
{
let serializer_name = std::any::type_name::<S>()
.rsplit("::")
.next()
.unwrap_or("Serializer");
let response = format!("{serializer_name}Response");
let create = format!("{serializer_name}Create");
let update = format!("{serializer_name}Update");
let patch = format!("{serializer_name}Patch");
let list = format!("{serializer_name}List");
let list_item = format!("{serializer_name}ListItem");
let schema = M::schema();
let mut response_properties = serde_json::Map::new();
let mut create_properties = serde_json::Map::new();
let mut update_properties = serde_json::Map::new();
let mut patch_properties = serde_json::Map::new();
let mut response_required = Vec::new();
let mut create_required = Vec::new();
let mut update_required = Vec::new();
for field in S::fields() {
let column = schema
.columns
.iter()
.find(|column| column.name == field.source || column.field_name == field.source);
let property = column
.map(openapi_column_schema)
.unwrap_or_else(|| json!({"type": "object"}));
let property = if field.related_model.is_some() {
json!({"type": "object"})
} else {
property
};
if !field.write_only {
response_properties.insert(field.name.to_owned(), property.clone());
if column.is_some_and(|column| !column.nullable) {
response_required.push(field.name.to_owned());
}
}
if !field.read_only && field.related_model.is_none() {
create_properties.insert(field.name.to_owned(), property.clone());
update_properties.insert(field.name.to_owned(), property.clone());
patch_properties.insert(field.name.to_owned(), property);
if column.is_some_and(|column| {
!column.nullable
&& column.default.is_none()
&& !column.auto_now
&& !column.auto_now_add
}) {
create_required.push(field.name.to_owned());
update_required.push(field.name.to_owned());
}
}
}
let response_schema = object_schema(response_properties, response_required);
let mut list_properties = serde_json::Map::new();
let mut list_required = Vec::new();
for field in list_fields.iter().filter(|field| !field.write_only) {
let column = schema
.columns
.iter()
.find(|column| column.name == field.source || column.field_name == field.source);
let property = if field.related_model.is_some() {
json!({"type": "object"})
} else {
column
.map(openapi_column_schema)
.unwrap_or_else(|| json!({"type": "object"}))
};
list_properties.insert(field.name.to_owned(), property);
if column.is_some_and(|column| !column.nullable) {
list_required.push(field.name.to_owned());
}
}
let list_item_schema = object_schema(list_properties, list_required);
let create_schema = object_schema(create_properties, create_required);
let update_schema = object_schema(update_properties, update_required);
let patch_schema = object_schema(patch_properties, Vec::new());
let error = schema_ref("Error");
let collection = format!("{}/", path.trim_end_matches('/'));
let detail = format!("{collection}{{id}}/");
json!({
"openapi": "3.0.3",
"info": {"title": "che-rest API", "version": "0.1.0"},
"paths": {
collection: {
"get": {"responses": {"200": {"description": "OK", "content": {"application/json": {"schema": schema_ref(&list)}}}}},
"post": {"security": [{"TokenAuth": []}, {"SessionCookie": [], "CsrfToken": []}], "requestBody": {"required": true, "content": {"application/json": {"schema": schema_ref(&create)}}}, "responses": {"201": response_with_ref(&response), "400": response_with_ref_value(&error), "401": response_with_ref_value(&error), "403": response_with_ref_value(&error)}}
},
detail: {
"get": {"responses": {"200": response_with_ref(&response), "401": response_with_ref_value(&error), "403": response_with_ref_value(&error), "404": response_with_ref_value(&error)}},
"put": {"security": [{"TokenAuth": []}, {"SessionCookie": [], "CsrfToken": []}], "requestBody": {"required": true, "content": {"application/json": {"schema": schema_ref(&update)}}}, "responses": {"200": response_with_ref(&response), "400": response_with_ref_value(&error), "401": response_with_ref_value(&error), "403": response_with_ref_value(&error), "404": response_with_ref_value(&error)}},
"patch": {"security": [{"TokenAuth": []}, {"SessionCookie": [], "CsrfToken": []}], "requestBody": {"required": true, "content": {"application/json": {"schema": schema_ref(&patch)}}}, "responses": {"200": response_with_ref(&response), "400": response_with_ref_value(&error), "401": response_with_ref_value(&error), "403": response_with_ref_value(&error), "404": response_with_ref_value(&error)}},
"delete": {"security": [{"TokenAuth": []}, {"SessionCookie": [], "CsrfToken": []}], "responses": {"204": {"description": "No Content"}, "401": response_with_ref_value(&error), "403": response_with_ref_value(&error), "404": response_with_ref_value(&error)}}
}
},
"components": {"schemas": {
response.clone(): response_schema,
list_item.clone(): list_item_schema,
create: create_schema,
update: update_schema,
patch: patch_schema,
list: {"type": "object", "required": ["count", "results"], "properties": {"count": {"type": "integer"}, "results": {"type": "array", "items": schema_ref(&list_item)}}},
"Error": {"type": "object", "required": ["detail"], "properties": {"detail": {"type": "string"}}}
}}
})
}
fn response_with_ref(name: &str) -> serde_json::Value {
json!({"description": "OK", "content": {"application/json": {"schema": schema_ref(name)}}})
}
fn response_with_ref_value(reference: &serde_json::Value) -> serde_json::Value {
json!({"description": "Error", "content": {"application/json": {"schema": reference}}})
}
fn object_schema(
properties: serde_json::Map<String, serde_json::Value>,
required: Vec<String>,
) -> serde_json::Value {
let mut schema = json!({"type": "object", "properties": properties});
if !required.is_empty() {
schema["required"] = json!(required);
}
schema
}
fn schema_ref(name: &str) -> serde_json::Value {
json!({"$ref": format!("#/components/schemas/{name}")})
}
pub fn openapi_column_schema(column: &che_orm::ColumnSchema) -> serde_json::Value {
let mut schema = match column.column_type {
che_orm::ColumnType::Integer => json!({"type": "integer", "format": "int64"}),
che_orm::ColumnType::Real => json!({"type": "number", "format": "double"}),
che_orm::ColumnType::Uuid => json!({"type": "string", "format": "uuid"}),
che_orm::ColumnType::Text => json!({"type": "string"}),
che_orm::ColumnType::Binary => json!({"type": "string", "format": "byte"}),
che_orm::ColumnType::Boolean => json!({"type": "boolean"}),
che_orm::ColumnType::DateTime => json!({
"type": "string",
"format": "date-time",
"description": "RFC 3339 datetime. ISO date (YYYY-MM-DD) is accepted and interpreted as midnight UTC."
}),
};
if let Some(choices) = &column.choices {
schema["enum"] = json!(choices);
}
if column.nullable {
schema["nullable"] = json!(true);
}
schema
}
fn current(ext: &Option<Extension<CurrentPrincipal>>) -> Option<&CurrentPrincipal> {
ext.as_ref().map(|e| &e.0)
}
fn error_filter(e: FilterError) -> AppError {
AppError::BadRequest(e.to_string())
}
fn error_validation(e: che_orm::ValidationErrors) -> AppError {
AppError::BadRequest(e.detail)
}
fn error_write(e: che_orm::OrmError) -> AppError {
match e {
che_orm::OrmError::QueryBuild(error) => AppError::BadRequest(format!("{error:?}")),
other => AppError::Orm(other),
}
}
fn page(params: &HashMap<String, String>, name: &str) -> AppResult<u64> {
params
.get(name)
.map(|v| {
v.parse()
.map_err(|_| AppError::BadRequest(format!("{name} must be an integer")))
})
.transpose()
.map(|v| v.unwrap_or(0))
}
fn count_params(params: &HashMap<String, String>) -> HashMap<String, String> {
let mut params = params.clone();
params.remove("ordering");
params
}
async fn list<V: ViewSet>(
Extension(state): Extension<AppState>,
Extension(viewset): Extension<V>,
who: Option<Extension<CurrentPrincipal>>,
Query(params): Query<HashMap<String, String>>,
) -> AppResult<impl IntoResponse>
where
V::Serializer: Serialize,
{
let principal = current(&who);
V::Permission::default().check(&state, principal, ViewAction::List)?;
let filter = viewset.filterset();
let count_params = count_params(¶ms);
let count = filter
.apply(viewset.get_queryset(), &count_params)
.map_err(error_filter)?
.count(state.database())
.await?;
let offset = page(¶ms, "offset")?;
let limit =
page(¶ms, "limit").map(|v| if params.contains_key("limit") { v } else { 20 })?;
if limit > 100 {
return Err(AppError::BadRequest("limit must not exceed 100".into()));
}
let rows = filter
.apply(viewset.get_queryset(), ¶ms)
.map_err(error_filter)?
.limit(limit)
.offset(offset)
.all(state.database())
.await?;
let results = rows
.into_iter()
.map(V::ListSerializer::to_json)
.collect::<Result<Vec<_>, _>>()?;
Ok(Json(json!({"count": count, "results": results})))
}
async fn retrieve<V: ViewSet>(
Extension(state): Extension<AppState>,
Extension(viewset): Extension<V>,
who: Option<Extension<CurrentPrincipal>>,
Path(id): Path<i64>,
) -> AppResult<impl IntoResponse>
where
V::Serializer: Serialize,
{
let principal = current(&who);
V::Permission::default().check(&state, principal, ViewAction::Retrieve)?;
let item = viewset
.get_queryset()
.filter(V::Model::primary_key().eq(id))
.first(state.database())
.await?
.ok_or(AppError::NotFound)?;
V::Permission::default().check_object(
&state,
principal,
ViewAction::Retrieve,
V::QuerySet::item_model(&item),
)?;
Ok(Json(V::Serializer::to_json(item)?))
}
async fn destroy<V: ViewSet>(
Extension(state): Extension<AppState>,
Extension(viewset): Extension<V>,
Extension(hooks): Extension<MutationHooks<V>>,
who: Option<Extension<CurrentPrincipal>>,
Path(id): Path<i64>,
) -> AppResult<impl IntoResponse> {
let principal = current(&who);
V::Permission::default().check(&state, principal, ViewAction::Delete)?;
{
let item = viewset
.get_queryset()
.filter(V::Model::primary_key().eq(id))
.first(state.database())
.await?
.ok_or(AppError::NotFound)?;
V::Permission::default().check_object(
&state,
principal,
ViewAction::Delete,
V::QuerySet::item_model(&item),
)?;
}
let model = state
.database()
.get::<V::Model>(id)
.await?
.ok_or(AppError::NotFound)?;
let mut values = Vec::with_capacity(hooks.0.len());
for hook in &hooks.0 {
values.push(hook.prepare_delete_async(&state, &model).await?);
}
state.database().delete::<V::Model>(id).await?;
hooks.after_delete(&state, &values);
if let Some(signal) = viewset.signal_name(ViewAction::Delete)
&& viewset.signal_access(ViewAction::Delete).is_some()
{
state.signals().publish(signal, json!({"id": id}));
}
Ok(StatusCode::NO_CONTENT)
}
async fn create<V: ViewSet>(
Extension(state): Extension<AppState>,
Extension(viewset): Extension<V>,
Extension(hooks): Extension<MutationHooks<V>>,
who: Option<Extension<CurrentPrincipal>>,
Json(data): Json<serde_json::Value>,
) -> AppResult<impl IntoResponse>
where
V::Serializer: Serialize,
{
let principal = current(&who);
V::Permission::default().check(&state, principal, ViewAction::Create)?;
let mut data = data;
normalize_datetime_fields(&mut data, V::Serializer::fields());
let write = V::Serializer::is_valid(data, WriteMode::Create).map_err(error_validation)?;
let model = viewset
.prepare_create(&state, principal, write)?
.save(state.database())
.await
.map_err(error_write)?
.ok_or(AppError::BadRequest("create did not return a model".into()))?;
hooks.after_commit(&state, principal, ViewAction::Create, &model)?;
let item = viewset
.get_queryset()
.filter(V::Model::primary_key().eq(model.primary_key_value()))
.first(state.database())
.await?
.ok_or(AppError::NotFound)?;
let payload = V::Serializer::to_json(item)?;
if let Some(signal) = viewset.signal_name(ViewAction::Create)
&& viewset.signal_access(ViewAction::Create).is_some()
{
state
.signals()
.publish(signal, json!({"id": model.primary_key_value()}));
}
Ok((StatusCode::CREATED, Json(payload)))
}
async fn patch<V: ViewSet>(
Extension(state): Extension<AppState>,
Extension(viewset): Extension<V>,
Extension(hooks): Extension<MutationHooks<V>>,
who: Option<Extension<CurrentPrincipal>>,
Path(id): Path<i64>,
Json(data): Json<serde_json::Value>,
) -> AppResult<impl IntoResponse>
where
V::Serializer: Serialize,
{
let principal = current(&who);
V::Permission::default().check(&state, principal, ViewAction::Patch)?;
let write = {
let current = viewset
.get_queryset()
.filter(V::Model::primary_key().eq(id))
.first(state.database())
.await?
.ok_or(AppError::NotFound)?;
V::Permission::default().check_object(
&state,
principal,
ViewAction::Patch,
V::QuerySet::item_model(¤t),
)?;
let mut data = data;
normalize_datetime_fields(&mut data, V::Serializer::fields());
let write =
V::Serializer::is_valid(data, WriteMode::Patch { id }).map_err(error_validation)?;
viewset.prepare_patch(&state, principal, V::QuerySet::item_model(¤t), write)?
};
let model = write
.save(state.database())
.await
.map_err(error_write)?
.ok_or(AppError::NotFound)?;
hooks.after_commit(&state, principal, ViewAction::Patch, &model)?;
let item = viewset
.get_queryset()
.filter(V::Model::primary_key().eq(model.primary_key_value()))
.first(state.database())
.await?
.ok_or(AppError::NotFound)?;
let payload = V::Serializer::to_json(item)?;
if let Some(signal) = viewset.signal_name(ViewAction::Patch)
&& viewset.signal_access(ViewAction::Patch).is_some()
{
state
.signals()
.publish(signal, json!({"id": model.primary_key_value()}));
}
Ok(Json(payload))
}
async fn update<V: ViewSet>(
Extension(state): Extension<AppState>,
Extension(viewset): Extension<V>,
Extension(hooks): Extension<MutationHooks<V>>,
who: Option<Extension<CurrentPrincipal>>,
Path(id): Path<i64>,
Json(data): Json<serde_json::Value>,
) -> AppResult<impl IntoResponse>
where
V::Serializer: Serialize,
{
let principal = current(&who);
V::Permission::default().check(&state, principal, ViewAction::Update)?;
let write = {
let current = viewset
.get_queryset()
.filter(V::Model::primary_key().eq(id))
.first(state.database())
.await?
.ok_or(AppError::NotFound)?;
V::Permission::default().check_object(
&state,
principal,
ViewAction::Update,
V::QuerySet::item_model(¤t),
)?;
let mut data = data;
normalize_datetime_fields(&mut data, V::Serializer::fields());
let write =
V::Serializer::is_valid(data, WriteMode::Update { id }).map_err(error_validation)?;
viewset.prepare_update(&state, principal, V::QuerySet::item_model(¤t), write)?
};
let model = write
.save(state.database())
.await
.map_err(error_write)?
.ok_or(AppError::NotFound)?;
hooks.after_commit(&state, principal, ViewAction::Update, &model)?;
let item = viewset
.get_queryset()
.filter(V::Model::primary_key().eq(model.primary_key_value()))
.first(state.database())
.await?
.ok_or(AppError::NotFound)?;
let payload = V::Serializer::to_json(item)?;
if let Some(signal) = viewset.signal_name(ViewAction::Update)
&& viewset.signal_access(ViewAction::Update).is_some()
{
state
.signals()
.publish(signal, json!({"id": model.primary_key_value()}));
}
Ok(Json(payload))
}