@@ -3267,9 +3267,11 @@ pub enum ImportExportPolicy {
3267
3267
Allow ( Vec < oxnet:: IpNet > ) ,
3268
3268
}
3269
3269
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>.
3273
3275
#[ derive( Clone , Debug , Serialize ) ]
3274
3276
pub struct Nullable < T > ( pub Option < T > ) ;
3275
3277
@@ -3313,7 +3315,13 @@ impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for Nullable<T> {
3313
3315
fn deserialize < D : serde:: Deserializer < ' de > > (
3314
3316
deserializer : D ,
3315
3317
) -> 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.
3317
3325
let value = serde_json:: Value :: deserialize ( deserializer) ?;
3318
3326
3319
3327
use serde:: de:: Error ;
0 commit comments