@@ -2,135 +2,6 @@ use std::ops::DerefMut;
22
33use crate :: schema:: { Schema , SchemaObject , SubschemaValidation } ;
44
5- #[ cfg( test) ]
6- #[ test]
7- fn optional_tagged_enum_with_unit_variants ( ) {
8- let original_schema_object_value = serde_json:: json!( {
9- "anyOf" : [
10- {
11- "description" : "A very simple enum with empty variants" ,
12- "oneOf" : [
13- {
14- "type" : "string" ,
15- "enum" : [
16- "C" ,
17- "D"
18- ]
19- } ,
20- {
21- "description" : "First variant doc-comment" ,
22- "type" : "string" ,
23- "enum" : [
24- "A"
25- ]
26- } ,
27- {
28- "description" : "Second variant doc-comment" ,
29- "type" : "string" ,
30- "enum" : [
31- "B"
32- ]
33- }
34- ]
35- } ,
36- {
37- "enum" : [
38- null
39- ] ,
40- "nullable" : true
41- }
42- ]
43- } ) ;
44-
45- let expected_converted_schema_object_value = serde_json:: json!( {
46- "description" : "A very simple enum with empty variants" ,
47- "nullable" : true ,
48- "oneOf" : [
49- {
50- "type" : "string" ,
51- "enum" : [
52- "C" ,
53- "D"
54- ]
55- } ,
56- {
57- "description" : "First variant doc-comment" ,
58- "type" : "string" ,
59- "enum" : [
60- "A"
61- ]
62- } ,
63- {
64- "description" : "Second variant doc-comment" ,
65- "type" : "string" ,
66- "enum" : [
67- "B"
68- ]
69- }
70- ]
71- } ) ;
72-
73-
74- let original_schema_object: SchemaObject =
75- serde_json:: from_value ( original_schema_object_value) . expect ( "valid JSON" ) ;
76- let expected_converted_schema_object: SchemaObject =
77- serde_json:: from_value ( expected_converted_schema_object_value) . expect ( "valid JSON" ) ;
78-
79- let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
80- hoist_any_of_subschema_with_a_nullable_variant ( & mut actual_converted_schema_object) ;
81-
82- assert_json_diff:: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
83- }
84-
85- #[ cfg( test) ]
86- #[ test]
87- fn optional_tagged_enum_with_unit_variants_but_also_an_existing_description ( ) {
88- let original_schema_object_value = serde_json:: json!( {
89- "description" : "This comment will be lost" ,
90- "anyOf" : [
91- {
92- "description" : "A very simple enum with empty variants" ,
93- "type" : "string" ,
94- "enum" : [
95- "C" ,
96- "D" ,
97- "A" ,
98- "B"
99- ]
100- } ,
101- {
102- "enum" : [
103- null
104- ] ,
105- "nullable" : true
106- }
107- ]
108- } ) ;
109-
110- let expected_converted_schema_object_value = serde_json:: json!( {
111- "description" : "A very simple enum with empty variants" ,
112- "nullable" : true ,
113- "type" : "string" ,
114- "enum" : [
115- "C" ,
116- "D" ,
117- "A" ,
118- "B"
119- ]
120- } ) ;
121-
122-
123- let original_schema_object: SchemaObject =
124- serde_json:: from_value ( original_schema_object_value) . expect ( "valid JSON" ) ;
125- let expected_converted_schema_object: SchemaObject =
126- serde_json:: from_value ( expected_converted_schema_object_value) . expect ( "valid JSON" ) ;
127-
128- let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
129- hoist_any_of_subschema_with_a_nullable_variant ( & mut actual_converted_schema_object) ;
130-
131- assert_json_diff:: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
132- }
133-
1345/// Replace the schema with the anyOf subschema and set to nullable when the
1356/// only other subschema is the nullable entry.
1367///
@@ -216,3 +87,135 @@ pub(crate) fn hoist_any_of_subschema_with_a_nullable_variant(kube_schema: &mut S
21687 // Set the schema to nullable (as we know we matched the null variant earlier)
21788 kube_schema. extensions . insert ( "nullable" . to_owned ( ) , true . into ( ) ) ;
21889}
90+
91+ #[ cfg( test) ]
92+ mod tests {
93+ use super :: * ;
94+
95+ #[ test]
96+ fn optional_tagged_enum_with_unit_variants ( ) {
97+ let original_schema_object_value = serde_json:: json!( {
98+ "anyOf" : [
99+ {
100+ "description" : "A very simple enum with empty variants" ,
101+ "oneOf" : [
102+ {
103+ "type" : "string" ,
104+ "enum" : [
105+ "C" ,
106+ "D"
107+ ]
108+ } ,
109+ {
110+ "description" : "First variant doc-comment" ,
111+ "type" : "string" ,
112+ "enum" : [
113+ "A"
114+ ]
115+ } ,
116+ {
117+ "description" : "Second variant doc-comment" ,
118+ "type" : "string" ,
119+ "enum" : [
120+ "B"
121+ ]
122+ }
123+ ]
124+ } ,
125+ {
126+ "enum" : [
127+ null
128+ ] ,
129+ "nullable" : true
130+ }
131+ ]
132+ } ) ;
133+
134+ let expected_converted_schema_object_value = serde_json:: json!( {
135+ "description" : "A very simple enum with empty variants" ,
136+ "nullable" : true ,
137+ "oneOf" : [
138+ {
139+ "type" : "string" ,
140+ "enum" : [
141+ "C" ,
142+ "D"
143+ ]
144+ } ,
145+ {
146+ "description" : "First variant doc-comment" ,
147+ "type" : "string" ,
148+ "enum" : [
149+ "A"
150+ ]
151+ } ,
152+ {
153+ "description" : "Second variant doc-comment" ,
154+ "type" : "string" ,
155+ "enum" : [
156+ "B"
157+ ]
158+ }
159+ ]
160+ } ) ;
161+
162+
163+ let original_schema_object: SchemaObject =
164+ serde_json:: from_value ( original_schema_object_value) . expect ( "valid JSON" ) ;
165+ let expected_converted_schema_object: SchemaObject =
166+ serde_json:: from_value ( expected_converted_schema_object_value) . expect ( "valid JSON" ) ;
167+
168+ let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
169+ hoist_any_of_subschema_with_a_nullable_variant ( & mut actual_converted_schema_object) ;
170+
171+ assert_json_diff:: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
172+ }
173+
174+ #[ test]
175+ fn optional_tagged_enum_with_unit_variants_but_also_an_existing_description ( ) {
176+ let original_schema_object_value = serde_json:: json!( {
177+ "description" : "This comment will be lost" ,
178+ "anyOf" : [
179+ {
180+ "description" : "A very simple enum with empty variants" ,
181+ "type" : "string" ,
182+ "enum" : [
183+ "C" ,
184+ "D" ,
185+ "A" ,
186+ "B"
187+ ]
188+ } ,
189+ {
190+ "enum" : [
191+ null
192+ ] ,
193+ "nullable" : true
194+ }
195+ ]
196+ } ) ;
197+
198+ let expected_converted_schema_object_value = serde_json:: json!( {
199+ "description" : "A very simple enum with empty variants" ,
200+ "nullable" : true ,
201+ "type" : "string" ,
202+ "enum" : [
203+ "C" ,
204+ "D" ,
205+ "A" ,
206+ "B"
207+ ]
208+ } ) ;
209+
210+
211+ let original_schema_object: SchemaObject =
212+ serde_json:: from_value ( original_schema_object_value) . expect ( "valid JSON" ) ;
213+ let expected_converted_schema_object: SchemaObject =
214+ serde_json:: from_value ( expected_converted_schema_object_value) . expect ( "valid JSON" ) ;
215+
216+ let mut actual_converted_schema_object = original_schema_object. clone ( ) ;
217+ hoist_any_of_subschema_with_a_nullable_variant ( & mut actual_converted_schema_object) ;
218+
219+ assert_json_diff:: assert_json_eq!( actual_converted_schema_object, expected_converted_schema_object) ;
220+ }
221+ }
0 commit comments