Skip to content

Commit

Permalink
Add ref handling for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
andurairaj committed May 1, 2024
1 parent 6c43e7e commit e32eace
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -340,71 +339,75 @@ public PathItem processRefToExternalPathItem(String $ref, RefFormat refFormat) {
cache.putRenamedRef($ref, newRef);

if(pathItem != null) {
if(pathItem.readOperationsMap() != null) {
final Map<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
if (operation.getResponses() != null) {
final Map<String, ApiResponse> 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<String, MediaType> 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<PathItem.HttpMethod, Operation> operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
if (operation.getResponses() != null) {
final Map<String, ApiResponse> 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<String, MediaType> 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<String, MediaType> 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<String, MediaType> 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<Parameter> parameters = operation.getParameters();
if (parameters != null) {
parameters.stream()
.filter(parameter -> parameter.getSchema() != null)
.forEach(parameter -> this.processRefSchemaObject(parameter.getSchema(), $ref));
}
final List<Parameter> 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) {
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e32eace

Please sign in to comment.