1- use std:: collections:: BTreeSet ;
1+ use std:: collections:: { BTreeMap , BTreeSet } ;
22
33use schemars:: schema:: {
4- InstanceType , NumberValidation , RootSchema , Schema , SchemaObject , SingleOrVec ,
5- SubschemaValidation ,
4+ InstanceType , NumberValidation , ObjectValidation , RootSchema , Schema , SchemaObject ,
5+ SingleOrVec , SubschemaValidation ,
66} ;
77use serde_json:: Value ;
88
@@ -77,6 +77,37 @@ impl DiffWalker {
7777 }
7878 }
7979
80+ fn diff_const ( & mut self , json_path : & str , lhs : & mut SchemaObject , rhs : & mut SchemaObject ) {
81+ Self :: normalize_const ( lhs) ;
82+ Self :: normalize_const ( rhs) ;
83+ match ( & lhs. const_value , & rhs. const_value ) {
84+ ( Some ( value) , None ) => self . changes . push ( Change {
85+ path : json_path. to_owned ( ) ,
86+ change : ChangeKind :: ConstRemove {
87+ removed : value. clone ( ) ,
88+ } ,
89+ } ) ,
90+ ( None , Some ( value) ) => self . changes . push ( Change {
91+ path : json_path. to_owned ( ) ,
92+ change : ChangeKind :: ConstAdd {
93+ added : value. clone ( ) ,
94+ } ,
95+ } ) ,
96+ ( Some ( l) , Some ( r) ) if l != r => {
97+ if l. is_object ( ) && r. is_object ( ) { }
98+ self . changes . push ( Change {
99+ path : json_path. to_owned ( ) ,
100+ change : ChangeKind :: ConstRemove { removed : l. clone ( ) } ,
101+ } ) ;
102+ self . changes . push ( Change {
103+ path : json_path. to_owned ( ) ,
104+ change : ChangeKind :: ConstAdd { added : r. clone ( ) } ,
105+ } ) ;
106+ }
107+ _ => ( ) ,
108+ }
109+ }
110+
80111 fn diff_properties (
81112 & mut self ,
82113 json_path : & str ,
@@ -384,6 +415,33 @@ impl DiffWalker {
384415 is_split
385416 }
386417
418+ fn normalize_const ( schema_object : & mut SchemaObject ) {
419+ fn do_normalize ( value : Value ) -> SchemaObject {
420+ match value {
421+ Value :: Object ( obj) => {
422+ let properties = obj
423+ . into_iter ( )
424+ . map ( |( k, v) | ( k, Schema :: Object ( do_normalize ( v) ) ) )
425+ . collect :: < BTreeMap < _ , _ > > ( ) ;
426+ SchemaObject {
427+ object : Some ( Box :: new ( ObjectValidation {
428+ properties,
429+ ..Default :: default ( )
430+ } ) ) ,
431+ ..Default :: default ( )
432+ }
433+ }
434+ _ => SchemaObject {
435+ const_value : Some ( value) ,
436+ ..Default :: default ( )
437+ } ,
438+ }
439+ }
440+ if let Some ( value) = schema_object. const_value . take ( ) {
441+ * schema_object = do_normalize ( value)
442+ }
443+ }
444+
387445 fn do_diff (
388446 & mut self ,
389447 json_path : & str ,
@@ -399,6 +457,7 @@ impl DiffWalker {
399457 if !comparing_any_of {
400458 self . diff_instance_types ( json_path, lhs, rhs) ;
401459 }
460+ self . diff_const ( json_path, lhs, rhs) ;
402461 // If we split the types, we don't want to compare type-specific properties
403462 // because they are already compared in the `Self::diff_any_of`
404463 if !is_lhs_split && !is_rhs_split {
0 commit comments