pub struct ResponseModelConfig {
pub include: Option<HashSet<String>>,
pub exclude: Option<HashSet<String>>,
pub by_alias: bool,
pub exclude_unset: bool,
pub exclude_defaults: bool,
pub exclude_none: bool,
/* private fields */
}Expand description
Configuration for response model serialization.
This provides FastAPI-compatible options for controlling how response data is serialized and validated before sending to clients.
§Examples
use fastapi_core::ResponseModelConfig;
use std::collections::HashSet;
// Only include specific fields
let config = ResponseModelConfig::new()
.include(["id", "name", "email"].into_iter().map(String::from).collect());
// Exclude sensitive fields
let config = ResponseModelConfig::new()
.exclude(["password", "internal_notes"].into_iter().map(String::from).collect());
// Use field aliases in output
let config = ResponseModelConfig::new()
.by_alias(true);Fields§
§include: Option<HashSet<String>>Only include these fields in the response. If None, all fields are included (subject to exclude).
exclude: Option<HashSet<String>>Exclude these fields from the response.
by_alias: boolUse serde aliases in output field names.
exclude_unset: boolExclude fields that were not explicitly set. Requires the type to track which fields were set.
exclude_defaults: boolExclude fields that have their default values.
exclude_none: boolExclude fields with None values.
Implementations§
Source§impl ResponseModelConfig
impl ResponseModelConfig
Sourcepub fn by_alias(self, value: bool) -> Self
pub fn by_alias(self, value: bool) -> Self
Use serde aliases in output.
To make this meaningful, supply alias metadata using
with_aliases(...) or with_aliases_from::<T>().
Sourcepub fn exclude_unset(self, value: bool) -> Self
pub fn exclude_unset(self, value: bool) -> Self
Exclude unset fields.
To make this meaningful, supply the per-response set field list using
with_set_fields(...).
Sourcepub fn exclude_defaults(self, value: bool) -> Self
pub fn exclude_defaults(self, value: bool) -> Self
Exclude fields with default values.
To make this meaningful, supply default JSON via
with_defaults_from::<T>() where T: Default + Serialize, or
with_defaults_json_provider(...).
Sourcepub fn exclude_none(self, value: bool) -> Self
pub fn exclude_none(self, value: bool) -> Self
Exclude fields with None values.
Sourcepub fn with_aliases(
self,
aliases: &'static [(&'static str, &'static str)],
) -> Self
pub fn with_aliases( self, aliases: &'static [(&'static str, &'static str)], ) -> Self
Provide alias metadata explicitly.
Sourcepub fn with_aliases_from<T: ResponseModelAliases>(self) -> Self
pub fn with_aliases_from<T: ResponseModelAliases>(self) -> Self
Provide alias metadata from a type-level provider.
Sourcepub fn with_defaults_json_provider(
self,
provider: fn() -> Result<Value, String>,
) -> Self
pub fn with_defaults_json_provider( self, provider: fn() -> Result<Value, String>, ) -> Self
Provide a default JSON provider explicitly.
Sourcepub fn with_defaults_from<T: Default + Serialize>(self) -> Self
pub fn with_defaults_from<T: Default + Serialize>(self) -> Self
Provide default JSON for the model via T::default().
Sourcepub fn with_set_fields(self, fields: HashSet<String>) -> Self
pub fn with_set_fields(self, fields: HashSet<String>) -> Self
Provide the set of canonical field names that were explicitly set for this response.
Sourcepub fn has_filtering(&self) -> bool
pub fn has_filtering(&self) -> bool
Check if any filtering is configured.
Sourcepub fn filter_json(
&self,
value: Value,
) -> Result<Value, ResponseValidationError>
pub fn filter_json( &self, value: Value, ) -> Result<Value, ResponseValidationError>
Apply filtering to a JSON value.
This filters the JSON according to the configuration:
- Applies include whitelist
- Applies exclude blacklist
- Removes None values if exclude_none is set
Trait Implementations§
Source§impl Clone for ResponseModelConfig
impl Clone for ResponseModelConfig
Source§fn clone(&self) -> ResponseModelConfig
fn clone(&self) -> ResponseModelConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more