|
| 1 | +use std::ops::DerefMut; |
| 2 | + |
| 3 | +use crate::schema::{Schema, SchemaObject, SubschemaValidation}; |
| 4 | + |
| 5 | +#[cfg(test)] |
| 6 | +#[test] |
| 7 | +fn tagged_enum_with_unit_variants() { |
| 8 | + let original_schema_object_value = serde_json::json!({ |
| 9 | + "description": "A very simple enum with unit variants", |
| 10 | + "oneOf": [ |
| 11 | + { |
| 12 | + "type": "string", |
| 13 | + "enum": [ |
| 14 | + "C", |
| 15 | + "D" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "description": "First variant doc-comment", |
| 20 | + "type": "string", |
| 21 | + "enum": [ |
| 22 | + "A" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "description": "Second variant doc-comment", |
| 27 | + "type": "string", |
| 28 | + "enum": [ |
| 29 | + "B" |
| 30 | + ] |
| 31 | + }, |
| 32 | + ] |
| 33 | + }); |
| 34 | + |
| 35 | + let expected_converted_schema_object_value = serde_json::json!({ |
| 36 | + "description": "A very simple enum with unit variants", |
| 37 | + "type": "string", |
| 38 | + "enum": [ |
| 39 | + "C", |
| 40 | + "D", |
| 41 | + "A", |
| 42 | + "B" |
| 43 | + ] |
| 44 | + }); |
| 45 | + |
| 46 | + |
| 47 | + let original_schema_object: SchemaObject = |
| 48 | + serde_json::from_value(original_schema_object_value).expect("valid JSON"); |
| 49 | + let expected_converted_schema_object: SchemaObject = |
| 50 | + serde_json::from_value(expected_converted_schema_object_value).expect("valid JSON"); |
| 51 | + |
| 52 | + let mut actual_converted_schema_object = original_schema_object.clone(); |
| 53 | + hoist_one_of_enum_with_unit_variants(&mut actual_converted_schema_object); |
| 54 | + |
| 55 | + assert_json_diff::assert_json_eq!(actual_converted_schema_object, expected_converted_schema_object); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +/// Replace a list of typed oneOf subschemas with a typed schema level enum |
| 60 | +/// |
| 61 | +/// Used for correcting the schema for tagged enums with unit variants. |
| 62 | +/// NOTE: Subschema descriptions are lost when they are combined into a single enum of the same type. |
| 63 | +/// |
| 64 | +/// This will return early without modifications unless: |
| 65 | +/// - There are `oneOf` subschemas (not empty) |
| 66 | +/// - Each subschema contains an enum |
| 67 | +/// - Each subschema is typed |
| 68 | +/// - Each subschemas types is the same as the others |
| 69 | +/// |
| 70 | +/// NOTE: This should work regardless of whether other hoisting has been performed or not. |
| 71 | +fn hoist_one_of_enum_with_unit_variants(kube_schema: &mut SchemaObject) { |
| 72 | + // Run some initial checks in case there is nothing to do |
| 73 | + let SchemaObject { |
| 74 | + subschemas: Some(subschemas), |
| 75 | + .. |
| 76 | + } = kube_schema |
| 77 | + else { |
| 78 | + return; |
| 79 | + }; |
| 80 | + |
| 81 | + let SubschemaValidation { |
| 82 | + one_of: Some(one_of), .. |
| 83 | + } = subschemas.deref_mut() |
| 84 | + else { |
| 85 | + return; |
| 86 | + }; |
| 87 | + |
| 88 | + if one_of.is_empty() { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // At this point, we can be reasonably sure we need to hoist the oneOf |
| 93 | + // subschema enums and types up to the schema level, and unset the oneOf field. |
| 94 | + // From here, anything that looks wrong will panic instead of return. |
| 95 | + // TODO (@NickLarsenNZ): Return errors instead of panicking, leave panicking up to the infallible schemars::Transform |
| 96 | + |
| 97 | + // Prepare to ensure each variant schema has a type |
| 98 | + let mut types = one_of.iter().map(|schema| match schema { |
| 99 | + Schema::Object(SchemaObject { |
| 100 | + instance_type: Some(r#type), |
| 101 | + .. |
| 102 | + }) => r#type, |
| 103 | + Schema::Object(untyped) => panic!("oneOf variants need to define a type: {untyped:#?}"), |
| 104 | + Schema::Bool(_) => panic!("oneOf variants can not be of type boolean"), |
| 105 | + }); |
| 106 | + |
| 107 | + // Get the first type |
| 108 | + let variant_type = types.next().expect("at this point, there must be a type"); |
| 109 | + // Ensure all variant types match it |
| 110 | + if types.any(|r#type| r#type != variant_type) { |
| 111 | + panic!("oneOf variants must all have the same type"); |
| 112 | + } |
| 113 | + |
| 114 | + // For each `oneOf` entry, iterate over the `enum` and `const` values. |
| 115 | + // Panic on an entry that doesn't contain an `enum` or `const`. |
| 116 | + let new_enums = one_of.iter().flat_map(|schema| match schema { |
| 117 | + Schema::Object(SchemaObject { |
| 118 | + enum_values: Some(r#enum), |
| 119 | + .. |
| 120 | + }) => r#enum.clone(), |
| 121 | + // Warning: The `const` check below must come after the enum check above. |
| 122 | + // Otherwise it will panic on a valid entry with an `enum`. |
| 123 | + Schema::Object(SchemaObject { other, .. }) => match other.get("const") { |
| 124 | + Some(r#const) => vec![r#const.clone()], |
| 125 | + None => panic!("oneOf variant did not provide \"enum\" or \"const\": {schema:#?}"), |
| 126 | + }, |
| 127 | + Schema::Bool(_) => panic!("oneOf variants can not be of type boolean"), |
| 128 | + }); |
| 129 | + // Merge the enums (extend just to be safe) |
| 130 | + kube_schema.enum_values.get_or_insert_default().extend(new_enums); |
| 131 | + |
| 132 | + // Hoist the type |
| 133 | + kube_schema.instance_type = Some(variant_type.clone()); |
| 134 | + |
| 135 | + // Clear the oneOf subschemas |
| 136 | + subschemas.one_of = None; |
| 137 | +} |
0 commit comments