Skip to content

Commit 26d25ab

Browse files
committed
better comments on Nullable
1 parent de2c4f9 commit 26d25ab

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

common/src/api/external/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,9 +3267,11 @@ pub enum ImportExportPolicy {
32673267
Allow(Vec<oxnet::IpNet>),
32683268
}
32693269

3270-
/// Use instead of Option in API request body structs to get a field that can be
3271-
/// null (parsed as `None`) but is not optional. Will fail to parse if the key
3272-
/// is not present.
3270+
/// Use instead of Option in API request body structs to get a field that can
3271+
/// be null (parsed as `None`) but is not optional. Unlike Option, Nullable
3272+
/// will fail to parse if the key is not present. The JSON Schema in the
3273+
/// OpenAPI definition will also reflect that the field is required. See
3274+
/// <https://github.com/serde-rs/serde/issues/2753>.
32733275
#[derive(Clone, Debug, Serialize)]
32743276
pub struct Nullable<T>(pub Option<T>);
32753277

@@ -3313,7 +3315,13 @@ impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for Nullable<T> {
33133315
fn deserialize<D: serde::Deserializer<'de>>(
33143316
deserializer: D,
33153317
) -> Result<Self, D::Error> {
3316-
// this is what errors if the key isn't present
3318+
// This line is required to get a parse error on missing fields.
3319+
// It seems that when the field is missing in the JSON, struct
3320+
// deserialization produces an error before this function is even hit,
3321+
// and that error is passed in here inside `deserializer`. If we don't
3322+
// do this Value::deserialize to cause that error to be returned as a
3323+
// missing field error, Option's deserialize will eat it by turning it
3324+
// into a successful parse as None.
33173325
let value = serde_json::Value::deserialize(deserializer)?;
33183326

33193327
use serde::de::Error;

0 commit comments

Comments
 (0)