6
6
import io .swagger .v3 .oas .models .media .Schema ;
7
7
import java .util .HashSet ;
8
8
import java .util .LinkedHashMap ;
9
+ import java .util .List ;
9
10
import java .util .Map ;
10
11
import java .util .Optional ;
11
12
import java .util .stream .Collectors ;
@@ -63,7 +64,8 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(
63
64
64
65
MapKeyDiff <String , Schema > mappingDiff =
65
66
MapKeyDiff .diff (
66
- getSchema (leftComponents , leftMapping ), getSchema (rightComponents , rightMapping ));
67
+ getSchema (leftComponents , leftMapping , leftComposedSchema ),
68
+ getSchema (rightComponents , rightMapping , rightComposedSchema ));
67
69
Map <String , ChangedSchema > changedMapping = new LinkedHashMap <>();
68
70
for (String key : mappingDiff .getSharedKey ()) {
69
71
Schema leftSchema = new Schema ();
@@ -88,10 +90,14 @@ public <T extends Schema<X>, X> Optional<ChangedSchema> diff(
88
90
}
89
91
}
90
92
91
- private Map <String , Schema > getSchema (Components components , Map <String , String > mapping ) {
93
+ private Map <String , Schema > getSchema (Components components , Map <String , String > mapping , ComposedSchema composedSchema ) {
92
94
Map <String , Schema > result = new LinkedHashMap <>();
93
95
mapping .forEach (
94
96
(key , value ) -> result .put (key , refPointer .resolveRef (components , new Schema (), value )));
97
+
98
+ result .putAll (getUnnamedSchemas (composedSchema .getAllOf (), "all-of" ));
99
+ result .putAll (getUnnamedSchemas (composedSchema .getOneOf (), "one-of" ));
100
+ result .putAll (getUnnamedSchemas (composedSchema .getAnyOf (), "any-of" ));
95
101
return result ;
96
102
}
97
103
@@ -100,7 +106,7 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
100
106
for (Schema schema : composedSchema .getOneOf ()) {
101
107
String ref = schema .get$ref ();
102
108
if (ref == null ) {
103
- throw new IllegalArgumentException ( "invalid oneOf schema" ) ;
109
+ continue ;
104
110
}
105
111
String schemaName = refPointer .getRefName (ref );
106
112
if (schemaName == null ) {
@@ -119,4 +125,25 @@ private Map<String, String> getMapping(ComposedSchema composedSchema) {
119
125
return reverseMapping .entrySet ().stream ()
120
126
.collect (Collectors .toMap (Map .Entry ::getValue , Map .Entry ::getKey ));
121
127
}
128
+
129
+ private Map <String , Schema > getUnnamedSchemas (List <Schema > schemas , String name ) {
130
+ Map <String , Schema > result = new LinkedHashMap <>();
131
+
132
+ if (schemas == null ) {
133
+ return result ;
134
+ }
135
+
136
+ for (int i = 0 ; i < schemas .size (); i ++) {
137
+ Schema schema = schemas .get (i );
138
+
139
+ // If the ref is named, then we ignore it since getMapping will handle it.
140
+ if (schema .get$ref () != null ) {
141
+ continue ;
142
+ }
143
+
144
+ result .put (String .format ("%s-%s" , name , i ), schema );
145
+ }
146
+
147
+ return result ;
148
+ }
122
149
}
0 commit comments