33use std:: { collections:: BTreeMap , str:: FromStr } ;
44
55use derive_more:: From ;
6- #[ cfg( feature = "schema" ) ] use schemars:: schema :: Schema ;
6+ #[ cfg( feature = "schema" ) ] use schemars:: Schema ;
77use serde:: { Deserialize , Serialize } ;
88use serde_json:: Value ;
99
@@ -168,10 +168,10 @@ impl FromStr for Reason {
168168/// on the top level under the "x-kubernetes-validations".
169169///
170170/// ```rust
171- /// use schemars::schema:: Schema;
171+ /// use schemars::Schema;
172172/// use kube::core::{Rule, Reason, Message, validate};
173173///
174- /// let mut schema = Schema::Object(Default:: default() );
174+ /// let mut schema = Schema::default();
175175/// let rule = Rule{
176176/// rule: "self.spec.host == self.url.host".into(),
177177/// message: Some("must be a URL with the host matching spec.host".into()),
@@ -189,21 +189,15 @@ impl FromStr for Reason {
189189#[ cfg_attr( docsrs, doc( cfg( feature = "schema" ) ) ) ]
190190pub fn validate ( s : & mut Schema , rule : impl Into < Rule > ) -> Result < ( ) , serde_json:: Error > {
191191 let rule: Rule = rule. into ( ) ;
192- match s {
193- Schema :: Bool ( _) => ( ) ,
194- Schema :: Object ( schema_object) => {
195- let rule = serde_json:: to_value ( rule) ?;
196- schema_object
197- . extensions
198- . entry ( "x-kubernetes-validations" . into ( ) )
199- . and_modify ( |rules| {
200- if let Value :: Array ( rules) = rules {
201- rules. push ( rule. clone ( ) ) ;
202- }
203- } )
204- . or_insert ( serde_json:: to_value ( & [ rule] ) ?) ;
205- }
206- } ;
192+ let rule = serde_json:: to_value ( rule) ?;
193+ s. ensure_object ( )
194+ . entry ( "x-kubernetes-validations" )
195+ . and_modify ( |rules| {
196+ if let Value :: Array ( rules) = rules {
197+ rules. push ( rule. clone ( ) ) ;
198+ }
199+ } )
200+ . or_insert ( serde_json:: to_value ( & [ rule] ) ?) ;
207201 Ok ( ( ) )
208202}
209203
@@ -219,13 +213,13 @@ pub fn validate(s: &mut Schema, rule: impl Into<Rule>) -> Result<(), serde_json:
219213/// field: Option<String>,
220214/// }
221215///
222- /// let gen = &mut schemars::gen ::SchemaSettings::openapi3().into_generator();
223- /// let mut schema = MyStruct::json_schema(gen );
216+ /// let generate = &mut schemars::generate ::SchemaSettings::openapi3().into_generator();
217+ /// let mut schema = MyStruct::json_schema(generate );
224218/// let rule = Rule::new("self != oldSelf");
225219/// validate_property(&mut schema, 0, rule)?;
226220/// assert_eq!(
227221/// serde_json::to_string(&schema).unwrap(),
228- /// r#"{"type":"object","properties":{"field":{"type":"string","nullable":true ,"x-kubernetes-validations":[{"rule":"self != oldSelf"}]}}}"#
222+ /// r#"{"type":"object","properties":{"field":{"type":[ "string","null"] ,"x-kubernetes-validations":[{"rule":"self != oldSelf"}]}}}"#
229223/// );
230224/// # Ok::<(), serde_json::Error>(())
231225///```
@@ -234,20 +228,22 @@ pub fn validate(s: &mut Schema, rule: impl Into<Rule>) -> Result<(), serde_json:
234228pub fn validate_property (
235229 s : & mut Schema ,
236230 property_index : usize ,
237- rule : impl Into < Rule > ,
231+ rule : impl Into < Rule > + Clone ,
238232) -> Result < ( ) , serde_json:: Error > {
239- match s {
240- Schema :: Bool ( _) => ( ) ,
241- Schema :: Object ( schema_object) => {
242- let obj = schema_object. object ( ) ;
243- for ( n, ( _, schema) ) in obj. properties . iter_mut ( ) . enumerate ( ) {
244- if n == property_index {
245- return validate ( schema, rule) ;
246- }
233+ let obj = s. ensure_object ( ) ;
234+ if let Some ( properties) = obj
235+ . entry ( "properties" )
236+ . or_insert ( serde_json:: Value :: Object ( Default :: default ( ) ) )
237+ . as_object_mut ( )
238+ {
239+ for ( n, ( _, schema) ) in properties. iter_mut ( ) . enumerate ( ) {
240+ if n == property_index {
241+ let mut prop = Schema :: try_from ( schema. clone ( ) ) ?;
242+ validate ( & mut prop, rule. clone ( ) ) ?;
243+ * schema = prop. to_value ( ) ;
247244 }
248245 }
249- } ;
250-
246+ }
251247 Ok ( ( ) )
252248}
253249
@@ -267,25 +263,33 @@ pub fn validate_property(
267263/// a: bool,
268264/// b: Option<bool>,
269265/// }
270- /// let gen = &mut schemars::gen ::SchemaSettings::openapi3().into_generator();
271- /// let mut first = MyStruct::json_schema(gen );
272- /// let mut second = MySecondStruct::json_schema(gen );
266+ /// let generate = &mut schemars::generate ::SchemaSettings::openapi3().into_generator();
267+ /// let mut first = MyStruct::json_schema(generate );
268+ /// let mut second = MySecondStruct::json_schema(generate );
273269/// merge_properties(&mut first, &mut second);
274270///
275271/// assert_eq!(
276272/// serde_json::to_string(&first).unwrap(),
277- /// r#"{"type":"object","properties":{"a":{"type":"boolean"},"b":{"type":"boolean","nullable":true }}}"#
273+ /// r#"{"type":"object","properties":{"a":{"type":"boolean"},"b":{"type":[ "boolean","null"] }}}"#
278274/// );
279275/// # Ok::<(), serde_json::Error>(())
280276#[ cfg( feature = "schema" ) ]
281277#[ cfg_attr( docsrs, doc( cfg( feature = "schema" ) ) ) ]
282278pub fn merge_properties ( s : & mut Schema , merge : & mut Schema ) {
283- match s {
284- schemars:: schema:: Schema :: Bool ( _) => ( ) ,
285- schemars:: schema:: Schema :: Object ( schema_object) => {
286- let obj = schema_object. object ( ) ;
287- for ( k, v) in & merge. clone ( ) . into_object ( ) . object ( ) . properties {
288- obj. properties . insert ( k. clone ( ) , v. clone ( ) ) ;
279+ if let Some ( properties) = s
280+ . ensure_object ( )
281+ . entry ( "properties" )
282+ . or_insert ( serde_json:: Value :: Object ( Default :: default ( ) ) )
283+ . as_object_mut ( )
284+ {
285+ if let Some ( merge_properties) = merge
286+ . ensure_object ( )
287+ . entry ( "properties" )
288+ . or_insert ( serde_json:: Value :: Object ( Default :: default ( ) ) )
289+ . as_object_mut ( )
290+ {
291+ for ( k, v) in merge_properties {
292+ properties. insert ( k. clone ( ) , v. clone ( ) ) ;
289293 }
290294 }
291295 }
@@ -366,12 +370,12 @@ impl MergeStrategy {
366370/// field: Option<String>,
367371/// }
368372///
369- /// let gen = &mut schemars::gen ::SchemaSettings::openapi3().into_generator();
370- /// let mut schema = MyStruct::json_schema(gen );
373+ /// let generate = &mut schemars::generate ::SchemaSettings::openapi3().into_generator();
374+ /// let mut schema = MyStruct::json_schema(generate );
371375/// merge_strategy_property(&mut schema, 0, MapMerge::Atomic)?;
372376/// assert_eq!(
373377/// serde_json::to_string(&schema).unwrap(),
374- /// r#"{"type":"object","properties":{"field":{"type":"string","nullable":true ,"x-kubernetes-map-type":"atomic"}}}"#
378+ /// r#"{"type":"object","properties":{"field":{"type":[ "string","null"] ,"x-kubernetes-map-type":"atomic"}}}"#
375379/// );
376380///
377381/// # Ok::<(), serde_json::Error>(())
@@ -383,17 +387,18 @@ pub fn merge_strategy_property(
383387 property_index : usize ,
384388 strategy : impl Into < MergeStrategy > ,
385389) -> Result < ( ) , serde_json:: Error > {
386- match s {
387- Schema :: Bool ( _) => ( ) ,
388- Schema :: Object ( schema_object) => {
389- let obj = schema_object. object ( ) ;
390- for ( n, ( _, schema) ) in obj. properties . iter_mut ( ) . enumerate ( ) {
391- if n == property_index {
392- return merge_strategy ( schema, strategy. into ( ) ) ;
393- }
390+ if let Some ( properties) = s
391+ . ensure_object ( )
392+ . entry ( "properties" )
393+ . or_insert ( serde_json:: Value :: Object ( Default :: default ( ) ) )
394+ . as_object_mut ( )
395+ {
396+ for ( n, ( _, schema) ) in properties. iter_mut ( ) . enumerate ( ) {
397+ if n == property_index {
398+ return merge_strategy ( schema, strategy. into ( ) ) ;
394399 }
395400 }
396- } ;
401+ }
397402
398403 Ok ( ( ) )
399404}
@@ -402,10 +407,9 @@ pub fn merge_strategy_property(
402407/// such as "x-kubernetes-list-type" and "x-kubernetes-list-map-keys".
403408///
404409/// ```rust
405- /// use schemars::schema::Schema;
406410/// use kube::core::{ListMerge, Reason, Message, merge_strategy};
407411///
408- /// let mut schema = Schema ::Object(Default::default());
412+ /// let mut schema = serde_json::Value ::Object(Default::default());
409413/// merge_strategy(&mut schema, ListMerge::Map(vec!["key".into(),"another".into()]).into())?;
410414/// assert_eq!(
411415/// serde_json::to_string(&schema).unwrap(),
@@ -416,15 +420,11 @@ pub fn merge_strategy_property(
416420///```
417421#[ cfg( feature = "schema" ) ]
418422#[ cfg_attr( docsrs, doc( cfg( feature = "schema" ) ) ) ]
419- pub fn merge_strategy ( s : & mut Schema , strategy : MergeStrategy ) -> Result < ( ) , serde_json:: Error > {
420- match s {
421- Schema :: Bool ( _) => ( ) ,
422- Schema :: Object ( schema_object) => {
423- for ( key, value) in strategy. keys ( ) ? {
424- schema_object. extensions . insert ( key, value) ;
425- }
423+ pub fn merge_strategy ( s : & mut Value , strategy : MergeStrategy ) -> Result < ( ) , serde_json:: Error > {
424+ for ( key, value) in strategy. keys ( ) ? {
425+ if let Some ( s) = s. as_object_mut ( ) {
426+ s. insert ( key, value) ;
426427 }
427- } ;
428-
428+ }
429429 Ok ( ( ) )
430430}
0 commit comments