io.swagger.parser.v3.processors.CallbackProcessor Maven / Gradle / Ivy
package io.swagger.parser.v3.processors;
import io.swagger.oas.models.OpenAPI;
import io.swagger.oas.models.Operation;
import io.swagger.oas.models.PathItem;
import io.swagger.oas.models.callbacks.Callback;
import io.swagger.oas.models.parameters.Parameter;
import io.swagger.parser.v3.ResolverCache;
import io.swagger.parser.v3.models.RefFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static io.swagger.parser.v3.util.RefUtils.computeRefFormat;
import static io.swagger.parser.v3.util.RefUtils.isAnExternalRefFormat;
/**
* Created by gracekarina on 23/06/17.
*/
public class CallbackProcessor {
private final ResolverCache cache;
private final OperationProcessor operationProcessor;
private final ParameterProcessor parameterProcessor;
private final OpenAPI openAPI;
private final ExternalRefProcessor externalRefProcessor;
public CallbackProcessor(ResolverCache cache, OpenAPI openAPI) {
this.cache = cache;
this.operationProcessor = new OperationProcessor(cache, openAPI);
this.parameterProcessor = new ParameterProcessor(cache,openAPI);
this.externalRefProcessor = new ExternalRefProcessor(cache, openAPI);
this.openAPI = openAPI;
}
public void processCallback(Callback callback) {
if (callback.get("$ref") != null){
processReferenceCallback(callback);
}
//Resolver PathItem
for(String name : callback.keySet()){
PathItem pathItem = callback.get(name);
final Map operationMap = pathItem.readOperationsMap();
for (PathItem.HttpMethod httpMethod : operationMap.keySet()) {
Operation operation = operationMap.get(httpMethod);
operationProcessor.processOperation(operation);
}
List parameters = pathItem.getParameters();
if (parameters != null) {
for (Parameter parameter: parameters){
parameterProcessor.processParameter(parameter);
}
}
}
}
public void processReferenceCallback(Callback callback ){
String $ref = callback.get("$ref").get$ref();
RefFormat refFormat = computeRefFormat($ref);
if (isAnExternalRefFormat(refFormat)){
final String newRef = externalRefProcessor.processRefToExternalCallback($ref, refFormat);
if (newRef != null) {
callback.get("$ref").set$ref("#/components/callbacks/"+newRef);
}
}
}
}