1
1
package com .qdesrame .openapi .diff .compare ;
2
2
3
+ import static java .util .Optional .ofNullable ;
4
+
3
5
import com .qdesrame .openapi .diff .compare .schemadiffresult .ArraySchemaDiffResult ;
4
6
import com .qdesrame .openapi .diff .compare .schemadiffresult .ComposedSchemaDiffResult ;
5
7
import com .qdesrame .openapi .diff .compare .schemadiffresult .SchemaDiffResult ;
15
17
16
18
public class SchemaDiff extends ReferenceDiffCache <Schema , ChangedSchema > {
17
19
18
- private Components leftComponents ;
19
- private Components rightComponents ;
20
- private OpenApiDiff openApiDiff ;
21
20
private static RefPointer <Schema > refPointer = new RefPointer <>(RefType .SCHEMAS );
22
-
23
21
private static Map <Class <? extends Schema >, Class <? extends SchemaDiffResult >>
24
22
schemaDiffResultClassMap = new LinkedHashMap <>();
25
23
@@ -29,6 +27,22 @@ public class SchemaDiff extends ReferenceDiffCache<Schema, ChangedSchema> {
29
27
schemaDiffResultClassMap .put (ComposedSchema .class , ComposedSchemaDiffResult .class );
30
28
}
31
29
30
+ private Components leftComponents ;
31
+ private Components rightComponents ;
32
+ private OpenApiDiff openApiDiff ;
33
+
34
+ public SchemaDiff (OpenApiDiff openApiDiff ) {
35
+ this .openApiDiff = openApiDiff ;
36
+ this .leftComponents =
37
+ openApiDiff .getOldSpecOpenApi () != null
38
+ ? openApiDiff .getOldSpecOpenApi ().getComponents ()
39
+ : null ;
40
+ this .rightComponents =
41
+ openApiDiff .getNewSpecOpenApi () != null
42
+ ? openApiDiff .getNewSpecOpenApi ().getComponents ()
43
+ : null ;
44
+ }
45
+
32
46
public static SchemaDiffResult getSchemaDiffResult (OpenApiDiff openApiDiff ) {
33
47
return getSchemaDiffResult (null , openApiDiff );
34
48
}
@@ -54,55 +68,6 @@ public static SchemaDiffResult getSchemaDiffResult(
54
68
}
55
69
}
56
70
57
- public SchemaDiff (OpenApiDiff openApiDiff ) {
58
- this .openApiDiff = openApiDiff ;
59
- this .leftComponents =
60
- openApiDiff .getOldSpecOpenApi () != null
61
- ? openApiDiff .getOldSpecOpenApi ().getComponents ()
62
- : null ;
63
- this .rightComponents =
64
- openApiDiff .getNewSpecOpenApi () != null
65
- ? openApiDiff .getNewSpecOpenApi ().getComponents ()
66
- : null ;
67
- }
68
-
69
- public Optional <ChangedSchema > diff (
70
- HashSet <String > refSet , Schema left , Schema right , DiffContext context ) {
71
- return cachedDiff (refSet , left , right , left .get$ref (), right .get$ref (), context );
72
- }
73
-
74
- public Optional <ChangedSchema > getTypeChangedSchema (
75
- Schema left , Schema right , DiffContext context ) {
76
- ChangedSchema changedSchema = SchemaDiff .getSchemaDiffResult (openApiDiff ).getChangedSchema ();
77
- changedSchema .setOldSchema (left );
78
- changedSchema .setNewSchema (right );
79
- changedSchema .setChangedType (true );
80
- changedSchema .setContext (context );
81
- return Optional .of (changedSchema );
82
- }
83
-
84
- @ Override
85
- protected Optional <ChangedSchema > computeDiff (
86
- HashSet <String > refSet , Schema left , Schema right , DiffContext context ) {
87
- left = refPointer .resolveRef (this .leftComponents , left , left .get$ref ());
88
- right = refPointer .resolveRef (this .rightComponents , right , right .get$ref ());
89
-
90
- left = resolveComposedSchema (leftComponents , left );
91
- right = resolveComposedSchema (rightComponents , right );
92
-
93
- // If type of schemas are different, just set old & new schema, set changedType to true in
94
- // SchemaDiffResult and
95
- // return the object
96
- if (!Objects .equals (left .getType (), right .getType ())
97
- || !Objects .equals (left .getFormat (), right .getFormat ())) {
98
- return getTypeChangedSchema (left , right , context );
99
- }
100
-
101
- // If schema type is same then get specific SchemaDiffResult and compare the properties
102
- SchemaDiffResult result = SchemaDiff .getSchemaDiffResult (right .getClass (), openApiDiff );
103
- return result .diff (refSet , leftComponents , rightComponents , left , right , context );
104
- }
105
-
106
71
protected static Schema resolveComposedSchema (Components components , Schema schema ) {
107
72
if (schema instanceof ComposedSchema ) {
108
73
ComposedSchema composedSchema = (ComposedSchema ) schema ;
@@ -138,4 +103,49 @@ protected static Schema addSchema(Schema schema, Schema fromSchema) {
138
103
// TODO copy other things from fromSchema
139
104
return schema ;
140
105
}
106
+
107
+ private static String getSchemaRef (Schema schema ) {
108
+ return ofNullable (schema ).map (Schema ::get$ref ).orElse (null );
109
+ }
110
+
111
+ public Optional <ChangedSchema > diff (
112
+ HashSet <String > refSet , Schema left , Schema right , DiffContext context ) {
113
+ if (left == null && right == null ) {
114
+ return Optional .empty ();
115
+ }
116
+ return cachedDiff (refSet , left , right , getSchemaRef (left ), getSchemaRef (right ), context );
117
+ }
118
+
119
+ public Optional <ChangedSchema > getTypeChangedSchema (
120
+ Schema left , Schema right , DiffContext context ) {
121
+ ChangedSchema changedSchema = SchemaDiff .getSchemaDiffResult (openApiDiff ).getChangedSchema ();
122
+ changedSchema .setOldSchema (left );
123
+ changedSchema .setNewSchema (right );
124
+ changedSchema .setChangedType (true );
125
+ changedSchema .setContext (context );
126
+ return Optional .of (changedSchema );
127
+ }
128
+
129
+ @ Override
130
+ protected Optional <ChangedSchema > computeDiff (
131
+ HashSet <String > refSet , Schema left , Schema right , DiffContext context ) {
132
+ left = refPointer .resolveRef (this .leftComponents , left , getSchemaRef (left ));
133
+ right = refPointer .resolveRef (this .rightComponents , right , getSchemaRef (right ));
134
+
135
+ left = resolveComposedSchema (leftComponents , left );
136
+ right = resolveComposedSchema (rightComponents , right );
137
+
138
+ // If type of schemas are different, just set old & new schema, set changedType to true in
139
+ // SchemaDiffResult and
140
+ // return the object
141
+ if ((left == null || right == null )
142
+ || !Objects .equals (left .getType (), right .getType ())
143
+ || !Objects .equals (left .getFormat (), right .getFormat ())) {
144
+ return getTypeChangedSchema (left , right , context );
145
+ }
146
+
147
+ // If schema type is same then get specific SchemaDiffResult and compare the properties
148
+ SchemaDiffResult result = SchemaDiff .getSchemaDiffResult (right .getClass (), openApiDiff );
149
+ return result .diff (refSet , leftComponents , rightComponents , left , right , context );
150
+ }
141
151
}
0 commit comments