|
| 1 | +use shopify_function::prelude::*; |
| 2 | +use shopify_function::wasm_api::Deserialize; |
| 3 | + |
| 4 | +#[derive(Deserialize, PartialEq, Debug, Default)] |
| 5 | +#[shopify_function(rename_all = "camelCase")] |
| 6 | +struct TestStructWithDefault { |
| 7 | + // Field with default attribute - will use Default implementation when null |
| 8 | + #[shopify_function(default)] |
| 9 | + field_a: String, |
| 10 | + |
| 11 | + // Field with default attribute - will use Default implementation when null |
| 12 | + #[shopify_function(default)] |
| 13 | + field_b: i32, |
| 14 | + |
| 15 | + // Field without default attribute - will error when null |
| 16 | + field_c: bool, |
| 17 | +} |
| 18 | + |
| 19 | +// Define a struct with more complex default types |
| 20 | +#[derive(Deserialize, PartialEq, Debug, Default)] |
| 21 | +struct TestComplexDefaults { |
| 22 | + // Standard primitive types |
| 23 | + #[shopify_function(default)] |
| 24 | + integer: i32, |
| 25 | + |
| 26 | + #[shopify_function(default)] |
| 27 | + float: f64, |
| 28 | + |
| 29 | + #[shopify_function(default)] |
| 30 | + string: String, |
| 31 | + |
| 32 | + #[shopify_function(default)] |
| 33 | + boolean: bool, |
| 34 | + |
| 35 | + // Collection types |
| 36 | + #[shopify_function(default)] |
| 37 | + vector: Vec<i32>, |
| 38 | + |
| 39 | + #[shopify_function(default)] |
| 40 | + option: Option<String>, |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_derive_deserialize_with_default() { |
| 45 | + // Test with all fields present |
| 46 | + let context = shopify_function::wasm_api::Context::new_with_input(serde_json::json!({ |
| 47 | + "fieldA": "test", |
| 48 | + "fieldB": 1, |
| 49 | + "fieldC": true |
| 50 | + })); |
| 51 | + let root_value = context.input_get().unwrap(); |
| 52 | + |
| 53 | + let input = TestStructWithDefault::deserialize(&root_value).unwrap(); |
| 54 | + assert_eq!( |
| 55 | + input, |
| 56 | + TestStructWithDefault { |
| 57 | + field_a: "test".to_string(), |
| 58 | + field_b: 1, |
| 59 | + field_c: true |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + // Test with default fields set to null |
| 64 | + let context = shopify_function::wasm_api::Context::new_with_input(serde_json::json!({ |
| 65 | + "fieldA": null, |
| 66 | + "fieldB": null, |
| 67 | + "fieldC": true |
| 68 | + })); |
| 69 | + let root_value = context.input_get().unwrap(); |
| 70 | + |
| 71 | + let input = TestStructWithDefault::deserialize(&root_value).unwrap(); |
| 72 | + assert_eq!( |
| 73 | + input, |
| 74 | + TestStructWithDefault { |
| 75 | + field_a: String::default(), // Empty string |
| 76 | + field_b: i32::default(), // 0 |
| 77 | + field_c: true |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + // Test with default fields missing |
| 82 | + let context = shopify_function::wasm_api::Context::new_with_input(serde_json::json!({ |
| 83 | + "fieldC": true |
| 84 | + })); |
| 85 | + let root_value = context.input_get().unwrap(); |
| 86 | + |
| 87 | + // Our implementation is handling missing fields correctly by treating them as null values |
| 88 | + let input = TestStructWithDefault::deserialize(&root_value).unwrap(); |
| 89 | + assert_eq!( |
| 90 | + input, |
| 91 | + TestStructWithDefault { |
| 92 | + field_a: String::default(), // Empty string |
| 93 | + field_b: i32::default(), // 0 |
| 94 | + field_c: true |
| 95 | + } |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn test_derive_deserialize_complex_defaults() { |
| 101 | + // Test with all fields set to null |
| 102 | + let context = shopify_function::wasm_api::Context::new_with_input(serde_json::json!({ |
| 103 | + "integer": null, |
| 104 | + "float": null, |
| 105 | + "string": null, |
| 106 | + "boolean": null, |
| 107 | + "vector": null, |
| 108 | + "option": null |
| 109 | + })); |
| 110 | + let root_value = context.input_get().unwrap(); |
| 111 | + |
| 112 | + let input = TestComplexDefaults::deserialize(&root_value).unwrap(); |
| 113 | + assert_eq!( |
| 114 | + input, |
| 115 | + TestComplexDefaults { |
| 116 | + integer: 0, |
| 117 | + float: 0.0, |
| 118 | + string: String::new(), |
| 119 | + boolean: false, |
| 120 | + vector: Vec::new(), |
| 121 | + option: None, |
| 122 | + } |
| 123 | + ); |
| 124 | + |
| 125 | + // Test with values provided |
| 126 | + let context = shopify_function::wasm_api::Context::new_with_input(serde_json::json!({ |
| 127 | + "integer": 42, |
| 128 | + "float": 3.19, |
| 129 | + "string": "hello", |
| 130 | + "boolean": true, |
| 131 | + "vector": [1, 2, 3], |
| 132 | + "option": "some value" |
| 133 | + })); |
| 134 | + let root_value = context.input_get().unwrap(); |
| 135 | + |
| 136 | + let input = TestComplexDefaults::deserialize(&root_value).unwrap(); |
| 137 | + assert_eq!( |
| 138 | + input, |
| 139 | + TestComplexDefaults { |
| 140 | + integer: 42, |
| 141 | + float: 3.19, |
| 142 | + string: "hello".to_string(), |
| 143 | + boolean: true, |
| 144 | + vector: vec![1, 2, 3], |
| 145 | + option: Some("some value".to_string()), |
| 146 | + } |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +#[test] |
| 151 | +fn test_missing_non_default_field() { |
| 152 | + // Missing a required field (field_c) |
| 153 | + let context = shopify_function::wasm_api::Context::new_with_input(serde_json::json!({ |
| 154 | + "fieldA": "test", |
| 155 | + "fieldB": 1 |
| 156 | + })); |
| 157 | + let root_value = context.input_get().unwrap(); |
| 158 | + |
| 159 | + // Should fail because field_c is required |
| 160 | + TestStructWithDefault::deserialize(&root_value).unwrap_err(); |
| 161 | +} |
0 commit comments