@@ -65,7 +65,7 @@ public void processPaths() {
65
65
String pathRef = pathItem .get$ref ().split ("#" )[0 ];
66
66
67
67
if (resolvedPath != null ) {
68
- updateLocalRefs (resolvedPath , pathRef );
68
+ updateRefs (resolvedPath , pathRef );
69
69
//we need to put the resolved path into swagger object
70
70
openAPI .path (pathStr , resolvedPath );
71
71
pathItem = resolvedPath ;
@@ -124,27 +124,27 @@ private void addParametersToEachOperation(PathItem pathItem) {
124
124
}
125
125
}
126
126
127
- protected void updateLocalRefs (PathItem path , String pathRef ) {
127
+ protected void updateRefs (PathItem path , String pathRef ) {
128
128
if (path .getParameters () != null ) {
129
129
List <Parameter > params = path .getParameters ();
130
130
for (Parameter param : params ) {
131
- updateLocalRefs (param , pathRef );
131
+ updateRefs (param , pathRef );
132
132
}
133
133
}
134
134
List <Operation > ops = path .readOperations ();
135
135
for (Operation op : ops ) {
136
136
if (op .getParameters () != null ) {
137
137
for (Parameter param : op .getParameters ()) {
138
- updateLocalRefs (param , pathRef );
138
+ updateRefs (param , pathRef );
139
139
}
140
140
}
141
141
if (op .getResponses () != null ) {
142
142
for (ApiResponse response : op .getResponses ().values ()) {
143
- updateLocalRefs (response , pathRef );
143
+ updateRefs (response , pathRef );
144
144
}
145
145
}
146
146
if (op .getRequestBody () != null ){
147
- updateLocalRefs (op .getRequestBody (),pathRef );
147
+ updateRefs (op .getRequestBody (),pathRef );
148
148
}
149
149
if (op .getCallbacks () != null ){
150
150
Map <String ,Callback > callbacks = op .getCallbacks ();
@@ -153,83 +153,75 @@ protected void updateLocalRefs(PathItem path, String pathRef) {
153
153
if (callback != null ) {
154
154
for (String callbackName : callback .keySet ()) {
155
155
PathItem pathItem = callback .get (callbackName );
156
- updateLocalRefs (pathItem ,pathRef );
156
+ updateRefs (pathItem ,pathRef );
157
157
}
158
158
}
159
159
}
160
160
}
161
161
}
162
162
}
163
163
164
- protected void updateLocalRefs (ApiResponse response , String pathRef ) {
164
+ protected void updateRefs (ApiResponse response , String pathRef ) {
165
165
if (response .get$ref () != null ){
166
- if (isLocalRef (response .get$ref ())) {
167
- response .set$ref (computeLocalRef (response .get$ref (), pathRef ));
168
- }
166
+ response .set$ref (computeRef (response .get$ref (), pathRef ));
169
167
}
170
168
if (response .getContent () != null ) {
171
169
Map <String , MediaType > content = response .getContent ();
172
170
for (String key : content .keySet ()) {
173
171
MediaType mediaType = content .get (key );
174
172
if (mediaType .getSchema () != null ) {
175
- updateLocalRefs (mediaType .getSchema (), pathRef );
173
+ updateRefs (mediaType .getSchema (), pathRef );
176
174
}
177
175
Map <String , Example > examples = content .get (key ).getExamples ();
178
176
if (examples != null ) {
179
177
for ( Example example :examples .values ()){
180
- updateLocalRefs (example , pathRef );
178
+ updateRefs (example , pathRef );
181
179
}
182
180
}
183
181
}
184
182
}
185
183
}
186
184
187
- protected void updateLocalRefs (Example example , String pathRef ) {
185
+ protected void updateRefs (Example example , String pathRef ) {
188
186
if (example .get$ref () != null ) {
189
- if (isLocalRef (example .get$ref ())) {
190
- example .set$ref (computeLocalRef (example .get$ref (), pathRef ));
191
- }
187
+ example .set$ref (computeRef (example .get$ref (), pathRef ));
192
188
}
193
189
}
194
190
195
- protected void updateLocalRefs (Parameter param , String pathRef ) {
191
+ protected void updateRefs (Parameter param , String pathRef ) {
196
192
if (param .get$ref () != null ){
197
- if (isLocalRef (param .get$ref ())) {
198
- param .set$ref (computeLocalRef (param .get$ref (), pathRef ));
199
- }
193
+ param .set$ref (computeRef (param .get$ref (), pathRef ));
200
194
}
201
195
if (param .getSchema () != null ) {
202
- updateLocalRefs (param .getSchema (), pathRef );
196
+ updateRefs (param .getSchema (), pathRef );
203
197
}
204
198
if (param .getContent () != null ) {
205
199
Map <String , MediaType > content = param .getContent ();
206
200
for (String key : content .keySet ()) {
207
201
MediaType mediaType = content .get (key );
208
202
if (mediaType .getSchema () != null ) {
209
- updateLocalRefs (mediaType .getSchema (), pathRef );
203
+ updateRefs (mediaType .getSchema (), pathRef );
210
204
}
211
205
}
212
206
}
213
207
214
208
}
215
209
216
- protected void updateLocalRefs (RequestBody body , String pathRef ) {
210
+ protected void updateRefs (RequestBody body , String pathRef ) {
217
211
if (body .get$ref () != null ){
218
- if (isLocalRef (body .get$ref ())) {
219
- body .set$ref (computeLocalRef (body .get$ref (), pathRef ));
220
- }
212
+ body .set$ref (computeRef (body .get$ref (), pathRef ));
221
213
}
222
214
if (body .getContent () != null ) {
223
215
Map <String , MediaType > content = body .getContent ();
224
216
for (String key : content .keySet ()) {
225
217
MediaType mediaType = content .get (key );
226
218
if (mediaType .getSchema () != null ) {
227
- updateLocalRefs (mediaType .getSchema (), pathRef );
219
+ updateRefs (mediaType .getSchema (), pathRef );
228
220
}
229
221
Map <String , Example > examples = content .get (key ).getExamples ();
230
222
if (examples != null ) {
231
223
for (Example example : examples .values ()) {
232
- updateLocalRefs (example , pathRef );
224
+ updateRefs (example , pathRef );
233
225
}
234
226
}
235
227
}
@@ -238,11 +230,9 @@ protected void updateLocalRefs(RequestBody body, String pathRef) {
238
230
}
239
231
}
240
232
241
- protected void updateLocalRefs (Schema model , String pathRef ) {
233
+ protected void updateRefs (Schema model , String pathRef ) {
242
234
if (model .get$ref () != null ) {
243
- if (isLocalRef (model .get$ref ())) {
244
- model .set$ref (computeLocalRef (model .get$ref (), pathRef ));
245
- }
235
+ model .set$ref (computeRef (model .get$ref (), pathRef ));
246
236
}
247
237
else if (model .getProperties () != null ) {
248
238
// process properties
@@ -251,7 +241,7 @@ else if(model.getProperties() != null) {
251
241
for (String key : properties .keySet ()) {
252
242
Schema property = properties .get (key );
253
243
if (property != null ) {
254
- updateLocalRefs (property , pathRef );
244
+ updateRefs (property , pathRef );
255
245
}
256
246
}
257
247
}
@@ -260,22 +250,22 @@ else if(model instanceof ComposedSchema) {
260
250
ComposedSchema composedSchema = (ComposedSchema ) model ;
261
251
if (composedSchema .getAllOf () != null ) {
262
252
for (Schema innerModel : composedSchema .getAllOf ()) {
263
- updateLocalRefs (innerModel , pathRef );
253
+ updateRefs (innerModel , pathRef );
264
254
}
265
255
}if (composedSchema .getAnyOf () != null ) {
266
256
for (Schema innerModel : composedSchema .getAnyOf ()) {
267
- updateLocalRefs (innerModel , pathRef );
257
+ updateRefs (innerModel , pathRef );
268
258
}
269
259
}if (composedSchema .getOneOf () != null ) {
270
260
for (Schema innerModel : composedSchema .getOneOf ()) {
271
- updateLocalRefs (innerModel , pathRef );
261
+ updateRefs (innerModel , pathRef );
272
262
}
273
263
}
274
264
}
275
265
else if (model instanceof ArraySchema ) {
276
266
ArraySchema arraySchema = (ArraySchema ) model ;
277
267
if (arraySchema .getItems () != null ) {
278
- updateLocalRefs (arraySchema .getItems (), pathRef );
268
+ updateRefs (arraySchema .getItems (), pathRef );
279
269
}
280
270
}
281
271
}
@@ -287,6 +277,27 @@ protected boolean isLocalRef(String ref) {
287
277
}
288
278
return false ;
289
279
}
280
+
281
+ protected boolean isAbsoluteRef (String ref ) {
282
+ if (ref .startsWith ("./" )) {
283
+ return true ;
284
+ }
285
+ return false ;
286
+ }
287
+
288
+ protected String computeRef (String ref , String prefix ) {
289
+ if (isLocalRef (ref )) return computeLocalRef (ref , prefix );
290
+ if (isAbsoluteRef (ref )) return ref ;
291
+ return computeRelativeRef (ref , prefix );
292
+ }
293
+
294
+ protected String computeRelativeRef (String ref , String prefix ) {
295
+ int iIdxOfSlash = prefix .lastIndexOf ('/' );
296
+ if (iIdxOfSlash != -1 ) {
297
+ return prefix .substring (0 , iIdxOfSlash +1 ) + ref ;
298
+ }
299
+ return prefix + ref ;
300
+ }
290
301
291
302
protected String computeLocalRef (String ref , String prefix ) {
292
303
return prefix + ref ;
0 commit comments