Skip to main content

mockforge_openapi/
response_rewriter.rs

1//! [`ResponseRewriter`] trait for post-generation response mutation.
2//!
3//! The OpenAPI router runs two optional mutation passes on each generated
4//! response body:
5//!
6//! 1. **Template token expansion** — substitutes placeholders like
7//!    `{{uuid}}` or `{{now}}` with rendered values.
8//! 2. **Override application** — applies operation-targeted patches from
9//!    user-supplied rules.
10//!
11//! Both of those historically lived in `mockforge-core`
12//! (`templating::expand_tokens` and `overrides::Overrides::apply`). To
13//! let `mockforge-openapi` own the router without pulling in core's
14//! templating + conditions + encryption graph, the router dispatches
15//! through a `ResponseRewriter` trait object. Core supplies the concrete
16//! implementation (see `mockforge_core::openapi_rewriter::CoreResponseRewriter`)
17//! that chains its own templating + overrides engines.
18
19use serde_json::Value;
20
21/// Hook for post-generation response body mutation used by the OpenAPI
22/// router. Implementations are called conditionally — `expand_tokens` only
23/// when template expansion is enabled for the current context, and
24/// `apply_overrides` only when operation overrides are enabled.
25pub trait ResponseRewriter: Send + Sync {
26    /// Expand template tokens (e.g. `{{uuid}}`, `{{now}}`) in-place inside
27    /// the response body.
28    fn expand_tokens(&self, body: &mut Value);
29
30    /// Apply user-supplied override rules to the response body in-place,
31    /// keyed by the operation id, tags, and request path.
32    fn apply_overrides(&self, operation_id: &str, tags: &[String], path: &str, body: &mut Value);
33}