|
4 | 4 | import java.net.URI; |
5 | 5 | import java.nio.file.Paths; |
6 | 6 | import java.util.Collection; |
| 7 | +import java.util.Collections; |
7 | 8 | import java.util.LinkedHashMap; |
8 | 9 | import java.util.List; |
9 | 10 | import java.util.Map; |
10 | 11 | import java.util.Objects; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.function.Function; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import java.util.stream.Stream; |
11 | 16 |
|
12 | 17 | import io.swagger.v3.oas.models.Components; |
13 | 18 | import io.swagger.v3.oas.models.OpenAPI; |
|
29 | 34 | import io.swagger.v3.parser.ResolverCache; |
30 | 35 | import io.swagger.v3.parser.models.RefFormat; |
31 | 36 | import io.swagger.v3.parser.models.RefType; |
| 37 | + |
32 | 38 | import org.apache.commons.io.FilenameUtils; |
33 | 39 | import org.apache.commons.lang3.StringUtils; |
34 | 40 | import org.slf4j.LoggerFactory; |
@@ -246,9 +252,31 @@ private void processSchema(Schema property, String file) { |
246 | 252 | } |
247 | 253 | if (property instanceof ComposedSchema) { |
248 | 254 | ComposedSchema composed = (ComposedSchema) property; |
| 255 | + final Map<String, String> refMap = Optional.ofNullable(composed.getDiscriminator()) |
| 256 | + .map(Discriminator::getMapping).orElse(Collections.emptyMap()).entrySet() |
| 257 | + .stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); |
| 258 | + Map<String, Schema> refCache = (!refMap.isEmpty() && |
| 259 | + (composed.getAnyOf() != null || composed.getOneOf() != null)) ? Stream.of( |
| 260 | + composed.getAnyOf(), composed.getOneOf() |
| 261 | + ) |
| 262 | + .filter(Objects::nonNull) |
| 263 | + .filter(l -> !l.isEmpty()) |
| 264 | + .flatMap(Collection::stream) |
| 265 | + .filter(s -> s.get$ref() != null) |
| 266 | + .collect(Collectors.toMap(Schema::get$ref, Function.identity())) : Collections.emptyMap(); |
| 267 | + |
249 | 268 | processProperties(composed.getAllOf(), file); |
250 | 269 | processProperties(composed.getAnyOf(), file); |
251 | 270 | processProperties(composed.getOneOf(), file); |
| 271 | + |
| 272 | + if (!refMap.isEmpty() && !refCache.isEmpty()) { |
| 273 | + refCache.entrySet() |
| 274 | + .stream().filter(e -> !e.getKey().equals(e.getValue().get$ref())) |
| 275 | + .forEach(entry -> { |
| 276 | + String newRef = entry.getValue().get$ref(); |
| 277 | + property.getDiscriminator().getMapping().put(refMap.get(entry.getKey()), newRef); |
| 278 | + }); |
| 279 | + } |
252 | 280 | } |
253 | 281 | } |
254 | 282 | } |
@@ -298,71 +326,75 @@ public PathItem processRefToExternalPathItem(String $ref, RefFormat refFormat) { |
298 | 326 | cache.putRenamedRef($ref, newRef); |
299 | 327 |
|
300 | 328 | if(pathItem != null) { |
301 | | - if(pathItem.readOperationsMap() != null) { |
302 | | - final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap(); |
303 | | - for (PathItem.HttpMethod httpMethod : operationMap.keySet()) { |
304 | | - Operation operation = operationMap.get(httpMethod); |
305 | | - if (operation.getResponses() != null) { |
306 | | - final Map<String, ApiResponse> responses = operation.getResponses(); |
307 | | - if (responses != null) { |
308 | | - for (String responseCode : responses.keySet()) { |
309 | | - ApiResponse response = responses.get(responseCode); |
310 | | - if (response != null) { |
311 | | - Schema schema = null; |
312 | | - if (response.getContent() != null) { |
313 | | - Map<String, MediaType> content = response.getContent(); |
314 | | - for (String mediaName : content.keySet()) { |
315 | | - MediaType mediaType = content.get(mediaName); |
316 | | - if (mediaType.getSchema() != null) { |
317 | | - schema = mediaType.getSchema(); |
318 | | - if (schema != null) { |
319 | | - processRefSchemaObject(mediaType.getSchema(), $ref); |
320 | | - } |
321 | | - if (mediaType.getExamples() != null) { |
322 | | - processRefExamples(mediaType.getExamples(), $ref); |
323 | | - } |
| 329 | + processPathItem(pathItem, $ref); |
| 330 | + } |
| 331 | + |
| 332 | + return pathItem; |
| 333 | + } |
324 | 334 |
|
| 335 | + private void processPathItem(PathItem pathItem, String $ref) { |
| 336 | + if(pathItem.readOperationsMap() != null) { |
| 337 | + final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap(); |
| 338 | + for (PathItem.HttpMethod httpMethod : operationMap.keySet()) { |
| 339 | + Operation operation = operationMap.get(httpMethod); |
| 340 | + if (operation.getResponses() != null) { |
| 341 | + final Map<String, ApiResponse> responses = operation.getResponses(); |
| 342 | + if (responses != null) { |
| 343 | + for (String responseCode : responses.keySet()) { |
| 344 | + ApiResponse response = responses.get(responseCode); |
| 345 | + if (response != null) { |
| 346 | + Schema schema = null; |
| 347 | + if (response.getContent() != null) { |
| 348 | + Map<String, MediaType> content = response.getContent(); |
| 349 | + for (String mediaName : content.keySet()) { |
| 350 | + MediaType mediaType = content.get(mediaName); |
| 351 | + if (mediaType.getSchema() != null) { |
| 352 | + schema = mediaType.getSchema(); |
| 353 | + if (schema != null) { |
| 354 | + processRefSchemaObject(mediaType.getSchema(), $ref); |
| 355 | + } |
| 356 | + if (mediaType.getExamples() != null) { |
| 357 | + processRefExamples(mediaType.getExamples(), $ref); |
325 | 358 | } |
| 359 | + |
326 | 360 | } |
327 | 361 | } |
328 | | - if (response.getLinks() != null) { |
329 | | - processRefLinks(response.getLinks(), $ref); |
330 | | - } |
331 | | - if (response.getHeaders() != null) { |
332 | | - processRefHeaders(response.getHeaders(), $ref); |
333 | | - } |
| 362 | + } |
| 363 | + if (response.getLinks() != null) { |
| 364 | + processRefLinks(response.getLinks(), $ref); |
| 365 | + } |
| 366 | + if (response.getHeaders() != null) { |
| 367 | + processRefHeaders(response.getHeaders(), $ref); |
334 | 368 | } |
335 | 369 | } |
336 | 370 | } |
337 | 371 | } |
338 | | - if (operation.getRequestBody() != null) { |
339 | | - RequestBody body = operation.getRequestBody(); |
340 | | - if (body.getContent() != null) { |
341 | | - Schema schema; |
342 | | - Map<String, MediaType> content = body.getContent(); |
343 | | - for (String mediaName : content.keySet()) { |
344 | | - MediaType mediaType = content.get(mediaName); |
345 | | - if (mediaType.getSchema() != null) { |
346 | | - schema = mediaType.getSchema(); |
347 | | - if (schema != null) { |
348 | | - processRefSchemaObject(mediaType.getSchema(), $ref); |
349 | | - } |
| 372 | + } |
| 373 | + if (operation.getRequestBody() != null) { |
| 374 | + RequestBody body = operation.getRequestBody(); |
| 375 | + if (body.getContent() != null) { |
| 376 | + Schema schema; |
| 377 | + Map<String, MediaType> content = body.getContent(); |
| 378 | + for (String mediaName : content.keySet()) { |
| 379 | + MediaType mediaType = content.get(mediaName); |
| 380 | + if (mediaType.getSchema() != null) { |
| 381 | + schema = mediaType.getSchema(); |
| 382 | + if (schema != null) { |
| 383 | + processRefSchemaObject(mediaType.getSchema(), $ref); |
350 | 384 | } |
351 | 385 | } |
352 | 386 | } |
353 | 387 | } |
| 388 | + } |
354 | 389 |
|
355 | | - final List<Parameter> parameters = operation.getParameters(); |
356 | | - if (parameters != null) { |
357 | | - parameters.stream() |
358 | | - .filter(parameter -> parameter.getSchema() != null) |
359 | | - .forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref)); |
360 | | - } |
| 390 | + final List<Parameter> parameters = operation.getParameters(); |
| 391 | + if (parameters != null) { |
| 392 | + parameters.stream() |
| 393 | + .filter(parameter -> parameter.getSchema() != null) |
| 394 | + .forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref)); |
361 | 395 | } |
362 | 396 | } |
363 | 397 | } |
364 | | - |
365 | | - return pathItem; |
366 | 398 | } |
367 | 399 |
|
368 | 400 | private void processDiscriminator(Discriminator d, String file) { |
@@ -838,6 +870,9 @@ public String processRefToExternalParameter(String $ref, RefFormat refFormat) { |
838 | 870 | if(parameter.getSchema() != null){ |
839 | 871 | processRefSchemaObject(parameter.getSchema(), $ref); |
840 | 872 | } |
| 873 | + if(parameter.getExamples() != null) { |
| 874 | + processRefExamples(parameter.getExamples(), $ref); |
| 875 | + } |
841 | 876 | } |
842 | 877 |
|
843 | 878 | return newRef; |
@@ -897,6 +932,24 @@ public String processRefToExternalCallback(String $ref, RefFormat refFormat) { |
897 | 932 | processRefToExternalCallback(file + callback.get$ref(), RefFormat.RELATIVE); |
898 | 933 | } |
899 | 934 | } |
| 935 | + } else { |
| 936 | + for (String path : callback.keySet()) { |
| 937 | + PathItem pathItem = callback.get(path); |
| 938 | + if(pathItem != null) { |
| 939 | + if (pathItem.get$ref() != null) { |
| 940 | + RefFormat pathRefFormat = computeRefFormat(pathItem.get$ref()); |
| 941 | + String path$ref = pathItem.get$ref(); |
| 942 | + if (isAnExternalRefFormat(refFormat)) { |
| 943 | + pathItem = this.processRefToExternalPathItem(path$ref, pathRefFormat); |
| 944 | + } else { |
| 945 | + pathItem = cache.loadRef(pathItem.get$ref(), refFormat, PathItem.class); |
| 946 | + } |
| 947 | + callback.put(path, pathItem); |
| 948 | + } else { |
| 949 | + this.processPathItem(pathItem, $ref); |
| 950 | + } |
| 951 | + } |
| 952 | + } |
900 | 953 | } |
901 | 954 | } |
902 | 955 |
|
|
0 commit comments