Allow non-Serialize default values.
Default values that don't implement Serialize are now ignored, rather than causing a compile error.
This is done by simulating specialization using a technique copied from Rocket:
5ebefa97c9/core/lib/src/sentinel.rs (L391-L445)
Fixes #115
			
			
This commit is contained in:
		
							parent
							
								
									6f39a13724
								
							
						
					
					
						commit
						690fe44343
					
				
					 4 changed files with 46 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,5 @@
 | 
			
		|||
mod util;
 | 
			
		||||
use schemars::JsonSchema;
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use util::*;
 | 
			
		||||
 | 
			
		||||
fn is_default<T: Default + PartialEq>(value: &T) -> bool {
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +24,7 @@ where
 | 
			
		|||
    ser.collect_str(&format_args!("i:{} b:{}", value.my_int, value.my_bool))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug)]
 | 
			
		||||
#[derive(Default, JsonSchema, Debug)]
 | 
			
		||||
#[serde(default)]
 | 
			
		||||
pub struct MyStruct {
 | 
			
		||||
    pub my_int: i32,
 | 
			
		||||
| 
						 | 
				
			
			@ -37,9 +36,10 @@ pub struct MyStruct {
 | 
			
		|||
        skip_serializing_if = "is_default"
 | 
			
		||||
    )]
 | 
			
		||||
    pub my_struct2_default_skipped: MyStruct2,
 | 
			
		||||
    pub not_serialize: NotSerialize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default, Deserialize, Serialize, JsonSchema, Debug, PartialEq)]
 | 
			
		||||
#[derive(Default, JsonSchema, Debug, PartialEq)]
 | 
			
		||||
#[serde(default = "ten_and_true")]
 | 
			
		||||
pub struct MyStruct2 {
 | 
			
		||||
    #[serde(default = "six")]
 | 
			
		||||
| 
						 | 
				
			
			@ -47,6 +47,9 @@ pub struct MyStruct2 {
 | 
			
		|||
    pub my_bool: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Default, JsonSchema, Debug)]
 | 
			
		||||
pub struct NotSerialize;
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn schema_default_values() -> TestResult {
 | 
			
		||||
    test_default_generated_schema::<MyStruct>("default")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -22,6 +22,9 @@
 | 
			
		|||
    },
 | 
			
		||||
    "my_struct2_default_skipped": {
 | 
			
		||||
      "$ref": "#/definitions/MyStruct2"
 | 
			
		||||
    },
 | 
			
		||||
    "not_serialize": {
 | 
			
		||||
      "$ref": "#/definitions/NotSerialize"
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  "definitions": {
 | 
			
		||||
| 
						 | 
				
			
			@ -38,6 +41,9 @@
 | 
			
		|||
          "type": "boolean"
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    },
 | 
			
		||||
    "NotSerialize": {
 | 
			
		||||
      "type": "null"
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue