|
1 | 1 | use crate::schema::{InstanceType, Metadata, Schema, SchemaObject, SingleOrVec}; |
2 | 2 |
|
| 3 | +#[cfg(test)] |
| 4 | +#[test] |
| 5 | +fn tagged_enum_with_stuct_and_tuple_variants_before_one_of_hoisting() { |
| 6 | + let original_schema_object_value = serde_json::json!({ |
| 7 | + "description": "A complex tagged enum with unit and struct variants", |
| 8 | + "oneOf": [ |
| 9 | + { |
| 10 | + "additionalProperties": false, |
| 11 | + "description": "Override documentation on the Normal variant", |
| 12 | + "properties": { |
| 13 | + "Normal": { |
| 14 | + "description": "A very simple enum with unit variants", |
| 15 | + "oneOf": [ |
| 16 | + { |
| 17 | + "enum": [ |
| 18 | + "C", |
| 19 | + "D" |
| 20 | + ], |
| 21 | + "type": "string" |
| 22 | + }, |
| 23 | + { |
| 24 | + "description": "First variant", |
| 25 | + "enum": [ |
| 26 | + "A" |
| 27 | + ], |
| 28 | + "type": "string" |
| 29 | + }, |
| 30 | + { |
| 31 | + "description": "Second variant", |
| 32 | + "enum": [ |
| 33 | + "B" |
| 34 | + ], |
| 35 | + "type": "string" |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | + }, |
| 40 | + "required": [ |
| 41 | + "Normal" |
| 42 | + ], |
| 43 | + "type": "object" |
| 44 | + }, |
| 45 | + { |
| 46 | + "additionalProperties": false, |
| 47 | + "description": "Documentation on the Hardcore variant", |
| 48 | + "properties": { |
| 49 | + "Hardcore": { |
| 50 | + "properties": { |
| 51 | + "core": { |
| 52 | + "description": "A very simple enum with unit variants", |
| 53 | + "oneOf": [ |
| 54 | + { |
| 55 | + "enum": [ |
| 56 | + "C", |
| 57 | + "D" |
| 58 | + ], |
| 59 | + "type": "string" |
| 60 | + }, |
| 61 | + { |
| 62 | + "description": "First variant", |
| 63 | + "enum": [ |
| 64 | + "A" |
| 65 | + ], |
| 66 | + "type": "string" |
| 67 | + }, |
| 68 | + { |
| 69 | + "description": "Second variant", |
| 70 | + "enum": [ |
| 71 | + "B" |
| 72 | + ], |
| 73 | + "type": "string" |
| 74 | + } |
| 75 | + ] |
| 76 | + }, |
| 77 | + "hard": { |
| 78 | + "type": "string" |
| 79 | + } |
| 80 | + }, |
| 81 | + "required": [ |
| 82 | + "hard", |
| 83 | + "core" |
| 84 | + ], |
| 85 | + "type": "object" |
| 86 | + } |
| 87 | + }, |
| 88 | + "required": [ |
| 89 | + "Hardcore" |
| 90 | + ], |
| 91 | + "type": "object" |
| 92 | + } |
| 93 | + ] |
| 94 | + }); |
| 95 | + |
| 96 | + let expected_converted_schema_object_value = serde_json::json!( |
| 97 | + { |
| 98 | + "description": "A complex tagged enum with unit and struct variants", |
| 99 | + "oneOf": [ |
| 100 | + { |
| 101 | + "required": [ |
| 102 | + "Normal" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "required": [ |
| 107 | + "Hardcore" |
| 108 | + ] |
| 109 | + } |
| 110 | + ], |
| 111 | + "properties": { |
| 112 | + "Hardcore": { |
| 113 | + "description": "Documentation on the Hardcore variant", |
| 114 | + "properties": { |
| 115 | + "core": { |
| 116 | + "description": "A very simple enum with unit variants", |
| 117 | + "oneOf": [ |
| 118 | + { |
| 119 | + "enum": [ |
| 120 | + "C", |
| 121 | + "D" |
| 122 | + ], |
| 123 | + "type": "string" |
| 124 | + }, |
| 125 | + { |
| 126 | + "description": "First variant", |
| 127 | + "enum": [ |
| 128 | + "A" |
| 129 | + ], |
| 130 | + "type": "string" |
| 131 | + }, |
| 132 | + { |
| 133 | + "description": "Second variant", |
| 134 | + "enum": [ |
| 135 | + "B" |
| 136 | + ], |
| 137 | + "type": "string" |
| 138 | + } |
| 139 | + ] |
| 140 | + }, |
| 141 | + "hard": { |
| 142 | + "type": "string" |
| 143 | + } |
| 144 | + }, |
| 145 | + "required": [ |
| 146 | + "core", |
| 147 | + "hard" |
| 148 | + ], |
| 149 | + "type": "object" |
| 150 | + }, |
| 151 | + "Normal": { |
| 152 | + "description": "Override documentation on the Normal variant", |
| 153 | + "oneOf": [ |
| 154 | + { |
| 155 | + "enum": [ |
| 156 | + "C", |
| 157 | + "D" |
| 158 | + ], |
| 159 | + "type": "string" |
| 160 | + }, |
| 161 | + { |
| 162 | + "description": "First variant", |
| 163 | + "enum": [ |
| 164 | + "A" |
| 165 | + ], |
| 166 | + "type": "string" |
| 167 | + }, |
| 168 | + { |
| 169 | + "description": "Second variant", |
| 170 | + "enum": [ |
| 171 | + "B" |
| 172 | + ], |
| 173 | + "type": "string" |
| 174 | + } |
| 175 | + ] |
| 176 | + } |
| 177 | + }, |
| 178 | + "type": "object" |
| 179 | + } |
| 180 | + ); |
| 181 | + |
| 182 | + let original_schema_object: SchemaObject = |
| 183 | + serde_json::from_value(original_schema_object_value).expect("valid JSON"); |
| 184 | + let expected_converted_schema_object: SchemaObject = |
| 185 | + serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON"); |
| 186 | + |
| 187 | + let mut actual_converted_schema_object = original_schema_object.clone(); |
| 188 | + hoist_properties_for_any_of_subschemas(&mut actual_converted_schema_object); |
| 189 | + |
| 190 | + assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object); |
| 191 | +} |
| 192 | + |
| 193 | +#[cfg(test)] |
| 194 | +#[test] |
| 195 | +fn tagged_enum_with_stuct_and_tuple_variants_after_one_of_hoisting() { |
| 196 | + use serde_json::json; |
| 197 | + |
| 198 | + let original_schema_object_value = serde_json::json!({ |
| 199 | + "description": "A complex tagged enum with unit and struct variants", |
| 200 | + "oneOf": [ |
| 201 | + { |
| 202 | + "additionalProperties": false, |
| 203 | + "description": "Override documentation on the Normal variant", |
| 204 | + "properties": { |
| 205 | + "Normal": { |
| 206 | + "description": "A very simple enum with unit variants", |
| 207 | + "type": "string", |
| 208 | + "enum": [ |
| 209 | + "C", |
| 210 | + "D", |
| 211 | + "A", |
| 212 | + "B" |
| 213 | + ] |
| 214 | + } |
| 215 | + }, |
| 216 | + "required": [ |
| 217 | + "Normal" |
| 218 | + ], |
| 219 | + "type": "object" |
| 220 | + }, |
| 221 | + { |
| 222 | + "additionalProperties": false, |
| 223 | + "description": "Documentation on the Hardcore variant", |
| 224 | + "properties": { |
| 225 | + "Hardcore": { |
| 226 | + "properties": { |
| 227 | + "core": { |
| 228 | + "description": "A very simple enum with unit variants", |
| 229 | + "type": "string", |
| 230 | + "enum": [ |
| 231 | + "C", |
| 232 | + "D", |
| 233 | + "A", |
| 234 | + "B" |
| 235 | + ] |
| 236 | + }, |
| 237 | + "hard": { |
| 238 | + "type": "string" |
| 239 | + } |
| 240 | + }, |
| 241 | + "required": [ |
| 242 | + "hard", |
| 243 | + "core" |
| 244 | + ], |
| 245 | + "type": "object" |
| 246 | + } |
| 247 | + }, |
| 248 | + "required": [ |
| 249 | + "Hardcore" |
| 250 | + ], |
| 251 | + "type": "object" |
| 252 | + } |
| 253 | + ] |
| 254 | + }); |
| 255 | + |
| 256 | + let expected_converted_schema_object_value = serde_json::json!( |
| 257 | + { |
| 258 | + "description": "A complex tagged enum with unit and struct variants", |
| 259 | + "oneOf": [ |
| 260 | + { |
| 261 | + "required": [ |
| 262 | + "Normal" |
| 263 | + ] |
| 264 | + }, |
| 265 | + { |
| 266 | + "required": [ |
| 267 | + "Hardcore" |
| 268 | + ] |
| 269 | + } |
| 270 | + ], |
| 271 | + "properties": { |
| 272 | + "Hardcore": { |
| 273 | + "description": "Documentation on the Hardcore variant", |
| 274 | + "properties": { |
| 275 | + "core": { |
| 276 | + "description": "A very simple enum with unit variants", |
| 277 | + "type": "string", |
| 278 | + "enum": [ |
| 279 | + "C", |
| 280 | + "D", |
| 281 | + "A", |
| 282 | + "B" |
| 283 | + ] |
| 284 | + }, |
| 285 | + "hard": { |
| 286 | + "type": "string" |
| 287 | + } |
| 288 | + }, |
| 289 | + "required": [ |
| 290 | + "core", |
| 291 | + "hard" |
| 292 | + ], |
| 293 | + "type": "object" |
| 294 | + }, |
| 295 | + "Normal": { |
| 296 | + "description": "Override documentation on the Normal variant", |
| 297 | + "type": "string", |
| 298 | + "enum": [ |
| 299 | + "C", |
| 300 | + "D", |
| 301 | + "A", |
| 302 | + "B" |
| 303 | + ] |
| 304 | + } |
| 305 | + }, |
| 306 | + "type": "object" |
| 307 | + } |
| 308 | + ); |
| 309 | + |
| 310 | + let original_schema_object: SchemaObject = |
| 311 | + serde_json::from_value(original_schema_object_value).expect("valid JSON"); |
| 312 | + let expected_converted_schema_object: SchemaObject = |
| 313 | + serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON"); |
| 314 | + |
| 315 | + let mut actual_converted_schema_object = original_schema_object.clone(); |
| 316 | + hoist_properties_for_any_of_subschemas(&mut actual_converted_schema_object); |
| 317 | + |
| 318 | + assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object); |
| 319 | +} |
| 320 | + |
3 | 321 | #[cfg(test)] |
4 | 322 | #[test] |
5 | 323 | fn untagged_enum_with_empty_variant_before_one_of_hoisting() { |
|
0 commit comments