1- use crate :: schema:: { InstanceType , Metadata , Schema , SchemaObject , SingleOrVec } ;
1+ use crate :: schema:: { InstanceType , Metadata , Schema , SchemaObject , SingleOrVec , NULL_SCHEMA } ;
22
33/// Take oneOf or anyOf subschema properties and move them them into the schema
44/// properties.
@@ -35,28 +35,21 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
3535 return ;
3636 }
3737
38- // Ensure we aren't looking at the one with a null
38+ // Ensure we aren't looking at the one with a null, as that is hoisted by another transformer
3939 if subschemas. len ( ) == 2 {
40- // This is the signature for the null variant, indicating the "other"
41- // variant is the subschema that needs hoisting
42- let null = serde_json:: json!( {
43- "enum" : [ null] ,
44- "nullable" : true
45- } ) ;
46-
47- // Return if one of the two entries are nulls
48- for value in subschemas
40+ // Return if there is a null entry
41+ if subschemas
4942 . iter ( )
5043 . map ( |x| serde_json:: to_value ( x) . expect ( "schema should be able to convert to JSON" ) )
44+ . any ( |x| x == * NULL_SCHEMA )
5145 {
52- if value == null {
53- return ;
54- }
46+ return ;
5547 }
5648 }
5749
5850 // At this point, we can be reasonably sure we need operate on the schema.
59- // TODO (@NickLarsenNZ): Return errors instead of panicking, leave panicking up to the infallible schemars::Transform
51+ // TODO (@NickLarsenNZ): Return errors instead of panicking, leave panicking up to the
52+ // infallible schemars::Transform
6053
6154 let subschemas = subschemas
6255 . iter_mut ( )
@@ -67,7 +60,8 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
6760 . collect :: < Vec < _ > > ( ) ;
6861
6962 for subschema in subschemas {
70- // This will clear out any objects that don't have required/properties fields (so that it appears as: {}).
63+ // This will clear out any objects that don't have required/properties fields (so that it
64+ // appears as: {}).
7165 let metadata = subschema. metadata . take ( ) ;
7266 subschema. instance_type . take ( ) ;
7367
@@ -91,10 +85,12 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
9185 }
9286
9387 // If properties are set, hoist them to the schema properties.
94- // This will panic if duplicate properties are encountered that do not have the same shape.
95- // That can happen when the untagged enum variants each refer to structs which contain the same field name but with different types or doc-comments.
88+ // This will panic if duplicate properties are encountered that do not have the same
89+ // shape. That can happen when the untagged enum variants each refer to structs which
90+ // contain the same field name but with different types or doc-comments.
9691 // The developer needs to make them the same.
97- // TODO (@NickLarsenNZ): Add a case for a structural variant, and a tuple variant containing a structure where the same field name is used.
92+ // TODO (@NickLarsenNZ): Add a case for a structural variant, and a tuple variant
93+ // containing a structure where the same field name is used.
9894 while let Some ( ( property_name, Schema :: Object ( property_schema_object) ) ) =
9995 object. properties . pop_first ( )
10096 {
@@ -105,8 +101,9 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
105101 . properties
106102 . get ( & property_name)
107103 {
108- // TODO (@NickLarsenNZ): Here we could do another check to see if it only differs by description.
109- // If the schema property description is not set, then we could overwrite it and not panic.
104+ // TODO (@NickLarsenNZ): Here we could do another check to see if it only
105+ // differs by description. If the schema property description is not set, then
106+ // we could overwrite it and not panic.
110107 assert_eq ! (
111108 existing_property,
112109 & Schema :: Object ( property_schema_object. clone( ) ) ,
@@ -126,6 +123,8 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
126123
127124#[ cfg( test) ]
128125mod tests {
126+ use assert_json_diff:: assert_json_eq;
127+
129128 use super :: * ;
130129
131130 #[ test]
@@ -314,7 +313,7 @@ mod tests {
314313 let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
315314 hoist_properties_for_any_of_subschemas ( & mut actual_converted_schema_object) ;
316315
317- assert_json_diff :: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
316+ assert_json_eq ! ( actual_converted_schema_object, expected_converted_schema_object) ;
318317 }
319318
320319 #[ test]
@@ -439,7 +438,7 @@ mod tests {
439438 let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
440439 hoist_properties_for_any_of_subschemas ( & mut actual_converted_schema_object) ;
441440
442- assert_json_diff :: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
441+ assert_json_eq ! ( actual_converted_schema_object, expected_converted_schema_object) ;
443442 }
444443
445444 #[ test]
@@ -558,7 +557,7 @@ mod tests {
558557 let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
559558 hoist_properties_for_any_of_subschemas ( & mut actual_converted_schema_object) ;
560559
561- assert_json_diff :: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
560+ assert_json_eq ! ( actual_converted_schema_object, expected_converted_schema_object) ;
562561 }
563562
564563 #[ test]
@@ -647,7 +646,7 @@ mod tests {
647646 let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
648647 hoist_properties_for_any_of_subschemas ( & mut actual_converted_schema_object) ;
649648
650- assert_json_diff :: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
649+ assert_json_eq ! ( actual_converted_schema_object, expected_converted_schema_object) ;
651650 }
652651
653652 #[ test]
0 commit comments