20
20
public class SwaggerResolver {
21
21
Logger LOGGER = LoggerFactory .getLogger (SwaggerResolver .class );
22
22
protected Swagger swagger ;
23
- protected Map <String , ResolutionContext > resolutionMap = new HashMap <String , ResolutionContext >();
23
+ protected Map <String , List < ResolutionContext >> resolutionMap = new HashMap <String , List < ResolutionContext > >();
24
24
25
25
protected ResolverOptions opts ;
26
26
public SwaggerResolver (){}
@@ -57,70 +57,71 @@ public void applyResolutions(List<AuthorizationValue> auths) {
57
57
objectList = new ArrayList <Object >();
58
58
hostToObjectMap .put (host , objectList );
59
59
}
60
- ResolutionContext ctx = resolutionMap .get (path );
61
-
62
- Object mapping = ctx .object ;
63
- Object target = ctx .parent ;
64
- try {
65
- String contents = null ;
66
- if (host .startsWith ("http" ))
67
- contents = new RemoteUrl ().urlToString (host , auths );
68
- else
69
- contents = Json .mapper ().writeValueAsString (swagger );
70
- JsonNode location = null ;
71
- String locationName = null ;
72
- if (contents != null ) {
73
- location = Json .mapper ().readTree (contents );
74
- String [] objectPath = definitionPath .split ("/" );
75
- for (String objectPathPart : objectPath ) {
76
- LOGGER .debug ("getting part " + objectPathPart );
77
- if (objectPathPart .length () > 0 && location != null ) {
78
- location = location .get (objectPathPart );
79
- locationName = objectPathPart ;
80
- }
81
- }
82
- }
83
- if (location != null ) {
84
- // convert the node to the proper type
85
- if (mapping instanceof Property ) {
86
- Model model = Json .mapper ().convertValue (location , Model .class );
87
- if (mapping instanceof RefProperty ) {
88
- RefProperty ref = (RefProperty ) mapping ;
89
- ref .set$ref (locationName );
90
- swagger .addDefinition (locationName , model );
60
+ List <ResolutionContext > contexts = resolutionMap .get (path );
61
+ for (ResolutionContext ctx : contexts ) {
62
+ Object mapping = ctx .object ;
63
+ Object target = ctx .parent ;
64
+ try {
65
+ String contents = null ;
66
+ if (host .startsWith ("http" ))
67
+ contents = new RemoteUrl ().urlToString (host , auths );
68
+ else
69
+ contents = Json .mapper ().writeValueAsString (swagger );
70
+ JsonNode location = null ;
71
+ String locationName = null ;
72
+ if (contents != null ) {
73
+ location = Json .mapper ().readTree (contents );
74
+ String [] objectPath = definitionPath .split ("/" );
75
+ for (String objectPathPart : objectPath ) {
76
+ LOGGER .debug ("getting part " + objectPathPart );
77
+ if (objectPathPart .length () > 0 && location != null ) {
78
+ location = location .get (objectPathPart );
79
+ locationName = objectPathPart ;
80
+ }
91
81
}
92
82
}
93
- else if (target instanceof Parameter ) {
94
- if (mapping instanceof RefModel ) {
83
+ if (location != null ) {
84
+ // convert the node to the proper type
85
+ if (mapping instanceof Property ) {
95
86
Model model = Json .mapper ().convertValue (location , Model .class );
96
- RefModel ref = (RefModel ) mapping ;
97
- ref .set$ref (locationName );
98
- swagger .addDefinition (locationName , model );
87
+ if (mapping instanceof RefProperty ) {
88
+ RefProperty ref = (RefProperty ) mapping ;
89
+ ref .set$ref (locationName );
90
+ swagger .addDefinition (locationName , model );
91
+ }
99
92
}
100
- }
101
- else if (target instanceof Operation ) {
102
-
103
- // get the operation position
104
- Operation operation = (Operation ) target ;
105
- int position = 0 ;
106
- for (Parameter param : operation .getParameters ()) {
107
-
108
- if (param instanceof RefParameter ) {
109
- RefParameter ref = (RefParameter ) param ;
110
- if (ref .getSimpleRef ().equals (locationName )) {
111
- // found a match!
112
- Parameter remoteParam = Json .mapper ().convertValue (location , Parameter .class );
113
- operation .getParameters ().set (position , remoteParam );
93
+ else if (target instanceof Parameter ) {
94
+ if (mapping instanceof RefModel ) {
95
+ Model model = Json .mapper ().convertValue (location , Model .class );
96
+ RefModel ref = (RefModel ) mapping ;
97
+ ref .set$ref (locationName );
98
+ swagger .addDefinition (locationName , model );
99
+ }
100
+ }
101
+ else if (target instanceof Operation ) {
102
+
103
+ // get the operation position
104
+ Operation operation = (Operation ) target ;
105
+ int position = 0 ;
106
+ for (Parameter param : operation .getParameters ()) {
107
+
108
+ if (param instanceof RefParameter ) {
109
+ RefParameter ref = (RefParameter ) param ;
110
+ if (ref .getSimpleRef ().equals (locationName )) {
111
+ // found a match!
112
+ Parameter remoteParam = Json .mapper ().convertValue (location , Parameter .class );
113
+ operation .getParameters ().set (position , remoteParam );
114
+ }
114
115
}
116
+ position += 1 ;
115
117
}
116
- position += 1 ;
117
118
}
118
119
}
119
120
}
120
- }
121
- catch ( Exception e ) {
122
- // failed to get it
123
- e . printStackTrace ();
121
+ catch ( Exception e ) {
122
+ // failed to get it
123
+ e . printStackTrace ();
124
+ }
124
125
}
125
126
}
126
127
}
@@ -140,16 +141,29 @@ public void detectOperationRefs() {
140
141
BodyParameter bp = (BodyParameter ) parameter ;
141
142
if (bp .getSchema () != null && bp .getSchema () instanceof RefModel ) {
142
143
RefModel ref = (RefModel )bp .getSchema ();
143
- if (ref .get$ref ().startsWith ("http" )) {
144
- LOGGER .debug ("added reference to " + ref .get$ref ());
145
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , bp , "ref" ));
144
+ String key = ref .get$ref ();
145
+ if (key .startsWith ("http" )) {
146
+ LOGGER .debug ("added reference to " + key );
147
+ List <ResolutionContext > m = resolutionMap .get (key );
148
+ if (m == null ) {
149
+ m = new ArrayList <ResolutionContext >();
150
+ }
151
+ m .add (new ResolutionContext (ref , bp , "ref" ));
152
+ resolutionMap .put (key , m );
146
153
}
147
154
}
148
155
}
149
156
else if (parameter instanceof RefParameter ) {
150
157
RefParameter ref = (RefParameter ) parameter ;
151
- LOGGER .debug ("added reference to " + ref .get$ref ());
152
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , operation , "inline" ));
158
+ String key = ref .get$ref ();
159
+ LOGGER .debug ("added reference to " + ref );
160
+
161
+ List <ResolutionContext > m = resolutionMap .get (key );
162
+ if (m == null ) {
163
+ m = new ArrayList <ResolutionContext >();
164
+ }
165
+ m .add (new ResolutionContext (ref , operation , "inline" ));
166
+ resolutionMap .put (key , m );
153
167
}
154
168
}
155
169
}
@@ -160,8 +174,15 @@ else if(parameter instanceof RefParameter) {
160
174
Property schema = response .getSchema ();
161
175
if (schema instanceof RefProperty ) {
162
176
RefProperty ref = (RefProperty ) schema ;
163
- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
164
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , response , "ref" ));
177
+ String key = ref .get$ref ();
178
+
179
+ if (key != null && key .startsWith ("http" )) {
180
+ List <ResolutionContext > m = resolutionMap .get (key );
181
+ if (m == null ) {
182
+ m = new ArrayList <ResolutionContext >();
183
+ }
184
+ m .add (new ResolutionContext (ref , response , "ref" ));
185
+ resolutionMap .put (key , m );
165
186
}
166
187
}
167
188
}
@@ -179,18 +200,30 @@ public void detectModelRefs() {
179
200
Model model = models .get (modelName );
180
201
if (model instanceof RefModel ) {
181
202
RefModel ref = (RefModel ) model ;
182
- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
183
- LOGGER .debug ("added reference to " + ref .get$ref ());
184
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , swagger .getDefinitions (), "ref" ));
203
+ String key = ref .get$ref ();
204
+ if (key != null && key .startsWith ("http" )) {
205
+ LOGGER .debug ("added reference to " + key );
206
+ List <ResolutionContext > m = resolutionMap .get (key );
207
+ if (m == null ) {
208
+ m = new ArrayList <ResolutionContext >();
209
+ }
210
+ m .add (new ResolutionContext (ref , swagger .getDefinitions (), "ref" ));
211
+ resolutionMap .put (key , m );
185
212
}
186
213
}
187
214
else if (model instanceof ArrayModel ) {
188
215
ArrayModel arrayModel = (ArrayModel ) model ;
189
216
if (arrayModel .getItems () != null && arrayModel .getItems () instanceof RefProperty ) {
190
217
RefProperty ref = (RefProperty )arrayModel .getItems ();
191
- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
192
- LOGGER .debug ("added reference to " + ref .get$ref ());
193
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , swagger .getDefinitions (), "ref" ));
218
+ String key = ref .get$ref ();
219
+ if (key != null && key .startsWith ("http" )) {
220
+ LOGGER .debug ("added reference to " + key );
221
+ List <ResolutionContext > m = resolutionMap .get (key );
222
+ if (m == null ) {
223
+ m = new ArrayList <ResolutionContext >();
224
+ }
225
+ m .add (new ResolutionContext (ref , swagger .getDefinitions (), "ref" ));
226
+ resolutionMap .put (key , m );
194
227
}
195
228
}
196
229
}
@@ -202,28 +235,46 @@ else if(model instanceof ModelImpl) {
202
235
Property property = properties .get (propertyName );
203
236
if (property instanceof RefProperty ) {
204
237
RefProperty ref = (RefProperty )property ;
205
- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
206
- LOGGER .debug ("added reference to " + ref .get$ref ());
207
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , impl , "ref" ));
238
+ String key = ref .get$ref ();
239
+ if (key != null && key .startsWith ("http" )) {
240
+ LOGGER .debug ("added reference to " + key );
241
+ List <ResolutionContext > m = resolutionMap .get (key );
242
+ if (m == null ) {
243
+ m = new ArrayList <ResolutionContext >();
244
+ }
245
+ m .add (new ResolutionContext (ref , impl , "ref" ));
246
+ resolutionMap .put (key , m );
208
247
}
209
248
}
210
249
else if (property instanceof ArrayProperty ) {
211
250
ArrayProperty arrayProperty = (ArrayProperty ) property ;
212
251
if (arrayProperty .getItems () != null && arrayProperty .getItems () instanceof RefProperty ) {
213
252
RefProperty ref = (RefProperty )arrayProperty .getItems ();
214
- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
215
- LOGGER .debug ("added reference to " + ref .get$ref ());
216
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , arrayProperty , "ref" ));
253
+ String key = ref .get$ref ();
254
+ if (key != null && key .startsWith ("http" )) {
255
+ LOGGER .debug ("added reference to " + key );
256
+ List <ResolutionContext > m = resolutionMap .get (key );
257
+ if (m == null ) {
258
+ m = new ArrayList <ResolutionContext >();
259
+ }
260
+ m .add (new ResolutionContext (ref , arrayProperty , "ref" ));
261
+ resolutionMap .put (key , m );
217
262
}
218
263
}
219
264
}
220
265
else if (property instanceof MapProperty ) {
221
266
MapProperty mp = (MapProperty ) property ;
222
267
if (mp .getAdditionalProperties () != null && mp .getAdditionalProperties () instanceof RefProperty ) {
223
268
RefProperty ref = (RefProperty )mp .getAdditionalProperties ();
224
- if (ref .get$ref () != null && ref .get$ref ().startsWith ("http" )) {
225
- LOGGER .debug ("added reference to " + ref .get$ref ());
226
- resolutionMap .put (ref .get$ref (), new ResolutionContext (ref , mp , "ref" ));
269
+ String key = ref .get$ref ();
270
+ if (key != null && key .startsWith ("http" )) {
271
+ LOGGER .debug ("added reference to " + key );
272
+ List <ResolutionContext > m = resolutionMap .get (key );
273
+ if (m == null ) {
274
+ m = new ArrayList <ResolutionContext >();
275
+ }
276
+ m .add (new ResolutionContext (ref , mp , "ref" ));
277
+ resolutionMap .put (key , m );
227
278
}
228
279
}
229
280
}
0 commit comments