From e32eace82622ca2c4cdd98d732487b4b3ba5c1cd Mon Sep 17 00:00:00 2001 From: andurairaj Date: Mon, 29 Apr 2024 17:05:30 +0530 Subject: [PATCH] Add ref handling for callbacks --- .../processors/ExternalRefProcessor.java | 120 ++++++++++-------- .../v3/parser/processors/PathsProcessor.java | 3 + 2 files changed, 73 insertions(+), 50 deletions(-) diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java index fb01725329..b82b1bbc0e 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java @@ -34,7 +34,6 @@ import io.swagger.v3.parser.ResolverCache; import io.swagger.v3.parser.models.RefFormat; import io.swagger.v3.parser.models.RefType; -import io.swagger.v3.parser.util.RefUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; @@ -340,71 +339,75 @@ public PathItem processRefToExternalPathItem(String $ref, RefFormat refFormat) { cache.putRenamedRef($ref, newRef); if(pathItem != null) { - if(pathItem.readOperationsMap() != null) { - final Map operationMap = pathItem.readOperationsMap(); - for (PathItem.HttpMethod httpMethod : operationMap.keySet()) { - Operation operation = operationMap.get(httpMethod); - if (operation.getResponses() != null) { - final Map responses = operation.getResponses(); - if (responses != null) { - for (String responseCode : responses.keySet()) { - ApiResponse response = responses.get(responseCode); - if (response != null) { - Schema schema = null; - if (response.getContent() != null) { - Map content = response.getContent(); - for (String mediaName : content.keySet()) { - MediaType mediaType = content.get(mediaName); - if (mediaType.getSchema() != null) { - schema = mediaType.getSchema(); - if (schema != null) { - processRefSchemaObject(mediaType.getSchema(), $ref); - } - if (mediaType.getExamples() != null) { - processRefExamples(mediaType.getExamples(), $ref); - } + processPathItem(pathItem, $ref); + } + + return pathItem; + } + private void processPathItem(PathItem pathItem, String $ref) { + if(pathItem.readOperationsMap() != null) { + final Map operationMap = pathItem.readOperationsMap(); + for (PathItem.HttpMethod httpMethod : operationMap.keySet()) { + Operation operation = operationMap.get(httpMethod); + if (operation.getResponses() != null) { + final Map responses = operation.getResponses(); + if (responses != null) { + for (String responseCode : responses.keySet()) { + ApiResponse response = responses.get(responseCode); + if (response != null) { + Schema schema = null; + if (response.getContent() != null) { + Map content = response.getContent(); + for (String mediaName : content.keySet()) { + MediaType mediaType = content.get(mediaName); + if (mediaType.getSchema() != null) { + schema = mediaType.getSchema(); + if (schema != null) { + processRefSchemaObject(mediaType.getSchema(), $ref); + } + if (mediaType.getExamples() != null) { + processRefExamples(mediaType.getExamples(), $ref); } + } } - if (response.getLinks() != null) { - processRefLinks(response.getLinks(), $ref); - } - if (response.getHeaders() != null) { - processRefHeaders(response.getHeaders(), $ref); - } + } + if (response.getLinks() != null) { + processRefLinks(response.getLinks(), $ref); + } + if (response.getHeaders() != null) { + processRefHeaders(response.getHeaders(), $ref); } } } } - if (operation.getRequestBody() != null) { - RequestBody body = operation.getRequestBody(); - if (body.getContent() != null) { - Schema schema; - Map content = body.getContent(); - for (String mediaName : content.keySet()) { - MediaType mediaType = content.get(mediaName); - if (mediaType.getSchema() != null) { - schema = mediaType.getSchema(); - if (schema != null) { - processRefSchemaObject(mediaType.getSchema(), $ref); - } + } + if (operation.getRequestBody() != null) { + RequestBody body = operation.getRequestBody(); + if (body.getContent() != null) { + Schema schema; + Map content = body.getContent(); + for (String mediaName : content.keySet()) { + MediaType mediaType = content.get(mediaName); + if (mediaType.getSchema() != null) { + schema = mediaType.getSchema(); + if (schema != null) { + processRefSchemaObject(mediaType.getSchema(), $ref); } } } } + } - final List parameters = operation.getParameters(); - if (parameters != null) { - parameters.stream() - .filter(parameter -> parameter.getSchema() != null) - .forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref)); - } + final List parameters = operation.getParameters(); + if (parameters != null) { + parameters.stream() + .filter(parameter -> parameter.getSchema() != null) + .forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref)); } } } - - return pathItem; } private void processDiscriminator(Discriminator d, String file) { @@ -942,6 +945,23 @@ public String processRefToExternalCallback(String $ref, RefFormat refFormat) { processRefToExternalCallback(file + callback.get$ref(), RefFormat.RELATIVE); } } + } else { + for (String path : callback.keySet()) { + PathItem pathItem = callback.get(path); + if(pathItem.get$ref() != null) { + RefFormat pathRefFormat = computeRefFormat(pathItem.get$ref()); + String path$ref = pathItem.get$ref(); + if (isAnExternalRefFormat(refFormat)) { + pathItem = this.processRefToExternalPathItem(path$ref, pathRefFormat); + } else { + pathItem = cache.loadRef(pathItem.get$ref(), refFormat, PathItem.class); + } + + callback.put(path, pathItem); + } else { + this.processPathItem(pathItem, $ref); + } + } } } diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java index aa4f516bd2..bd16ba580c 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java @@ -156,6 +156,9 @@ protected void updateRefs(PathItem path, String pathRef) { for (String name : callbacks.keySet()) { Callback callback = callbacks.get(name); if (callback != null) { + if(callback.get$ref() != null) { + callback.set$ref(computeRef(callback.get$ref(), pathRef)); + } for(String callbackName : callback.keySet()) { PathItem pathItem = callback.get(callbackName); updateRefs(pathItem,pathRef);