22
33import com .fasterxml .jackson .databind .JsonNode ;
44import com .google .common .collect .Iterators ;
5- import io .pravega .schemaregistry .rules .CompatibilityChecker ;
65
76import java .util .Iterator ;
87
98import static io .pravega .schemaregistry .rules .jsoncompatibility .BreakingChangesStore .BreakingChanges ;
109
10+ /**
11+ * this class provides methods for comparing array types in 2 schemas.
12+ * the simple body check is used for instances when an array type node is found. Not when there is an array type.
13+ * being defined
14+ */
1115public class ArrayTypeComparator {
16+
1217 JsonCompatibilityChecker jsonCompatibilityChecker ;
18+
1319 public void setJsonTypeComparator () {
1420 this .jsonCompatibilityChecker = new JsonCompatibilityChecker ();
1521 }
22+
1623 public BreakingChanges compareArrays (JsonNode toCheck , JsonNode toCheckAgainst ) {
1724 if (toCheck .isArray () && toCheckAgainst .isArray ())
1825 return arraySimpleBodyComparision (toCheck , toCheckAgainst );
@@ -26,18 +33,18 @@ public BreakingChanges compareArrays(JsonNode toCheck, JsonNode toCheckAgainst)
2633 if (additionalItemsChanges != null )
2734 return additionalItemsChanges ;
2835 BreakingChanges itemsValidationChanges = itemValidation (toCheck , toCheckAgainst );
29- if (itemsValidationChanges != null )
36+ if (itemsValidationChanges != null )
3037 return itemsValidationChanges ;
31- //TO DO : Add contains and tupleValidation
38+ //TODO : Add contains and tupleValidation
3239 }
3340 return null ;
3441 }
3542
3643 private BreakingChanges arraySimpleBodyComparision (JsonNode toCheck , JsonNode toCheckAgainst ) {
3744 Iterator <String > allNodes = Iterators .concat (toCheck .fieldNames (), toCheckAgainst .fieldNames ());
38- while (allNodes .hasNext ()) {
45+ while (allNodes .hasNext ()) {
3946 String item = allNodes .next ();
40- if (!toCheck .has (item ))
47+ if (!toCheck .has (item ))
4148 return BreakingChanges .ARRAY_SIMPLE_BODY_CHECK_ELEMENT_REMOVED ;
4249 else if (!toCheckAgainst .has (item ))
4350 return BreakingChanges .ARRAY_SIMPLE_BODY_CHECK_ELEMENT_ADDED ;
@@ -46,12 +53,11 @@ else if (!toCheckAgainst.has(item))
4653 }
4754
4855 private BreakingChanges checkUniqueItems (JsonNode toCheck , JsonNode toCheckAgainst ) {
49- if (toCheck .has ("uniqueItems" )) {
56+ if (toCheck .has ("uniqueItems" )) {
5057 if (toCheck .get ("uniqueItems" ).isBoolean () && toCheck .get ("uniqueItems" ).asText () == "true" ) {
51- if (toCheckAgainst .get ("uniqueItems" ) == null )
52- return BreakingChanges .ARRAY_UNIQUE_ITEMS_CONDITION_ENABLED ;
53- else if (toCheckAgainst .get ("uniqueItems" ).isBoolean () && toCheckAgainst .get (
54- "uniqueItems" ).asText ().equals ("false" ))
58+ if (toCheckAgainst .get ("uniqueItems" ) == null || (toCheckAgainst .get (
59+ "uniqueItems" ).isBoolean () && toCheckAgainst .get (
60+ "uniqueItems" ).asText ().equals ("false" )))
5561 return BreakingChanges .ARRAY_UNIQUE_ITEMS_CONDITION_ENABLED ;
5662 }
5763 }
@@ -86,46 +92,46 @@ else if (toCheck.get("additionalItems").isObject())
8692 return BreakingChanges .ARRAY_ADDITIONAL_ITEMS_SCOPE_DECREASED ;
8793 } else if (toCheck .get ("additionalItems" ) != null && toCheckAgainst .get ("additionalItems" ) != null ) {
8894 if (toCheck .get ("additionalItems" ).isBoolean () && toCheck .get ("additionalItems" ).asText () == "false" ) {
89- if (!(toCheckAgainst .get ("additionalItems" ).asText () == "false" ))
95+ if (!(toCheckAgainst .get ("additionalItems" ).asText () == "false" ))
9096 return BreakingChanges .ARRAY_ADDITIONAL_ITEMS_DISABLED ;
91- }
92- else if (toCheck .get ("additionalItems" ).isObject ()) {
93- if ( toCheckAgainst . get ("additionalItems" ). isObject ()) {
94- if ( jsonCompatibilityChecker . checkNodeType ( toCheck . get ( "additionalItems" ), toCheckAgainst .get ("additionalItems" )) != null )
97+ } else if ( toCheck . get ( "additionalItems" ). isObject ()) {
98+ if (toCheckAgainst .get ("additionalItems" ).isObject ()) {
99+ if ( jsonCompatibilityChecker . checkNodeType ( toCheck . get ("additionalItems" ),
100+ toCheckAgainst .get ("additionalItems" )) != null )
95101 return BreakingChanges .ARRAY_ADDITIONAL_ITEMS_SCOPE_INCOMPATIBLE_CHANGE ;
96- }
97- else if ( toCheckAgainst . get ( "additionalItems" ). isBoolean () && toCheckAgainst . get ( "additionalItems" ).asText () == "true" )
102+ } else if ( toCheckAgainst . get ( "additionalItems" ). isBoolean () && toCheckAgainst . get (
103+ "additionalItems" ).asText () == "true" )
98104 return BreakingChanges .ARRAY_ADDITIONAL_ITEMS_SCOPE_DECREASED ;
99105 }
100106 }
101107 return null ;
102108 }
103-
109+
104110 private BreakingChanges itemValidation (JsonNode toCheck , JsonNode toCheckAgainst ) {
105- if (!toCheck .has ("items" ) && !toCheckAgainst .has ("items" ))
111+ if (!toCheck .has ("items" ) && !toCheckAgainst .has ("items" ))
106112 return null ;
107113 return jsonCompatibilityChecker .checkNodeType (toCheck .get ("items" ), toCheckAgainst .get ("items" ));
108114 }
109-
115+
110116 private boolean isDynamicArray (JsonNode node ) {
111- if (node .get ("additionalItems" ) == null )
117+ if (node .get ("additionalItems" ) == null )
112118 return true ;
113- else if (node .get ("additionalItems" ).isBoolean ()) {
114- if (node .get ("additionalItems" ).asText () == "true" )
119+ else if (node .get ("additionalItems" ).isBoolean ()) {
120+ if (node .get ("additionalItems" ).asText () == "true" )
115121 return true ;
116122 }
117123 return false ;
118124 }
119-
120- private boolean isDynamicArrayWithCondition (JsonNode node ) {
121- if (node .get ("additionalItems" ).isObject ())
125+
126+ private boolean isDynamicArrayWithCondition (JsonNode node ) {
127+ if (node .get ("additionalItems" ).isObject ())
122128 return true ;
123129 return false ;
124130 }
125-
131+
126132 private boolean isStaticArray (JsonNode node ) {
127- if (node .get ("additionalItems" ).isBoolean ()) {
128- if (node .get ("additionalItems" ).asText () == "false" )
133+ if (node .get ("additionalItems" ).isBoolean ()) {
134+ if (node .get ("additionalItems" ).asText () == "false" )
129135 return true ;
130136 }
131137 return false ;
0 commit comments