-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use std::borrow::Cow; | ||
|
||
use pyo3::{ | ||
intern, | ||
types::{PyAnyMethods, PyDict, PyDictMethods, PyType}, | ||
Bound, Py, PyAny, PyObject, PyResult, Python, | ||
}; | ||
|
||
use crate::{ | ||
definitions::DefinitionsBuilder, | ||
serializers::{ | ||
shared::{BuildSerializer, TypeSerializer}, | ||
CombinedSerializer, Extra, | ||
}, | ||
SchemaSerializer, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NestedModelSerializer { | ||
model: Py<PyType>, | ||
name: String, | ||
} | ||
|
||
impl_py_gc_traverse!(NestedModelSerializer { model }); | ||
|
||
impl BuildSerializer for NestedModelSerializer { | ||
const EXPECTED_TYPE: &'static str = "nested-model"; | ||
|
||
fn build( | ||
schema: &Bound<'_, PyDict>, | ||
_config: Option<&Bound<'_, PyDict>>, | ||
_definitions: &mut DefinitionsBuilder<CombinedSerializer>, | ||
) -> PyResult<CombinedSerializer> { | ||
let py = schema.py(); | ||
let model = schema | ||
.get_item(intern!(py, "model"))? | ||
.expect("Invalid core schema for `nested-model` type") | ||
.downcast::<PyType>() | ||
.expect("Invalid core schema for `nested-model` type") | ||
.clone(); | ||
|
||
let name = model.getattr(intern!(py, "__name__"))?.extract()?; | ||
|
||
Ok(CombinedSerializer::NestedModel(NestedModelSerializer { | ||
model: model.clone().unbind(), | ||
name, | ||
})) | ||
} | ||
} | ||
|
||
impl NestedModelSerializer { | ||
fn nested_serializer<'py>(&self, py: Python<'py>) -> Bound<'py, SchemaSerializer> { | ||
self.model | ||
.bind(py) | ||
.call_method(intern!(py, "model_rebuild"), (), None) | ||
.unwrap(); | ||
|
||
self.model | ||
.getattr(py, intern!(py, "__pydantic_serializer__")) | ||
.unwrap() | ||
.downcast_bound::<SchemaSerializer>(py) | ||
.unwrap() | ||
.clone() | ||
|
||
// crate::schema_cache::retrieve_schema(py, self.model.as_any().clone()) | ||
// .downcast_bound::<SchemaSerializer>(py) | ||
// // FIXME: This actually will always trigger as we cache a `CoreSchema` lol | ||
// .expect("Cached validator was not a `SchemaSerializer`") | ||
// .clone() | ||
} | ||
} | ||
|
||
impl TypeSerializer for NestedModelSerializer { | ||
fn to_python( | ||
&self, | ||
value: &Bound<'_, PyAny>, | ||
include: Option<&Bound<'_, PyAny>>, | ||
exclude: Option<&Bound<'_, PyAny>>, | ||
extra: &Extra, | ||
) -> PyResult<PyObject> { | ||
self.nested_serializer(value.py()) | ||
.get() | ||
.serializer | ||
.to_python(value, include, exclude, extra) | ||
} | ||
|
||
fn json_key<'a>(&self, key: &'a Bound<'_, PyAny>, extra: &Extra) -> PyResult<Cow<'a, str>> { | ||
self.nested_serializer(key.py()).get().serializer.json_key(key, extra) | ||
} | ||
|
||
fn serde_serialize<S: serde::ser::Serializer>( | ||
&self, | ||
value: &Bound<'_, PyAny>, | ||
serializer: S, | ||
include: Option<&Bound<'_, PyAny>>, | ||
exclude: Option<&Bound<'_, PyAny>>, | ||
extra: &Extra, | ||
) -> Result<S::Ok, S::Error> { | ||
self.nested_serializer(value.py()) | ||
.get() | ||
.serializer | ||
.serde_serialize(value, serializer, include, exclude, extra) | ||
} | ||
|
||
fn get_name(&self) -> &str { | ||
&self.name | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use pyo3::{ | ||
intern, | ||
types::{PyAnyMethods, PyDict, PyDictMethods, PyType}, | ||
Bound, Py, PyObject, PyResult, Python, | ||
}; | ||
|
||
use crate::{definitions::DefinitionsBuilder, errors::ValResult, input::Input}; | ||
|
||
use super::{BuildValidator, CombinedValidator, SchemaValidator, ValidationState, Validator}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct NestedModelValidator { | ||
model: Py<PyType>, | ||
name: String, | ||
} | ||
|
||
impl_py_gc_traverse!(NestedModelValidator { model }); | ||
|
||
impl BuildValidator for NestedModelValidator { | ||
const EXPECTED_TYPE: &'static str = "nested-model"; | ||
|
||
fn build( | ||
schema: &Bound<'_, PyDict>, | ||
_config: Option<&Bound<'_, PyDict>>, | ||
_definitions: &mut DefinitionsBuilder<super::CombinedValidator>, | ||
) -> PyResult<super::CombinedValidator> { | ||
let py = schema.py(); | ||
let model = schema | ||
.get_item(intern!(py, "model"))? | ||
.expect("Invalid core schema for `nested-model` type") | ||
.downcast::<PyType>() | ||
.expect("Invalid core schema for `nested-model` type") | ||
.clone(); | ||
|
||
let name = model.getattr(intern!(py, "__name__"))?.extract()?; | ||
|
||
Ok(CombinedValidator::NestedModel(NestedModelValidator { | ||
model: model.clone().unbind(), | ||
name, | ||
})) | ||
} | ||
} | ||
|
||
impl Validator for NestedModelValidator { | ||
fn validate<'py>( | ||
&self, | ||
py: Python<'py>, | ||
input: &(impl Input<'py> + ?Sized), | ||
state: &mut ValidationState<'_, 'py>, | ||
) -> ValResult<PyObject> { | ||
self.model | ||
.bind(py) | ||
.call_method(intern!(py, "model_rebuild"), (), None) | ||
.unwrap(); | ||
|
||
let validator = self | ||
.model | ||
.getattr(py, intern!(py, "__pydantic_validator__")) | ||
.unwrap() | ||
.downcast_bound::<SchemaValidator>(py) | ||
.unwrap() | ||
.clone(); | ||
|
||
// let validator = crate::schema_cache::retrieve_schema(py, self.model.as_any().clone()) | ||
// .downcast_bound::<SchemaValidator>(py) | ||
// // FIXME: This actually will always trigger as we cache a `CoreSchema` lol | ||
// .expect("Cached validator was not a `SchemaValidator`") | ||
// .clone(); | ||
|
||
validator.get().validator.validate(py, input, state) | ||
} | ||
|
||
fn get_name(&self) -> &str { | ||
&self.name | ||
} | ||
} |