use of org.wso2.carbon.mediation.commons.rest.api.swagger.SwaggerConstants.OPERATION_HTTP_PATCH in project carbon-mediation by wso2.
the class OpenAPIProcessor method getUpdatedSwaggerFromApi.
/**
* Update a given swagger definition of the Synapse API.
*
* @param existingSwagger swagger definition needs to be updated.
* @param isJSONIn input swagger data type JSON / YAML.
* @param isJSONOut output swagger data type JSON / YAML.
* @return updated swagger definition as string.
*/
public String getUpdatedSwaggerFromApi(String existingSwagger, boolean isJSONIn, boolean isJSONOut) throws APIGenException {
if (api == null) {
throw new APIGenException("Provided API is null");
}
if (StringUtils.isEmpty(existingSwagger)) {
throw new APIGenException("Provided swagger definition is empty");
}
if (isJSONIn) {
JsonNode jsonNodeTree = null;
try {
jsonNodeTree = new ObjectMapper().readTree(existingSwagger);
existingSwagger = new YAMLMapper().writeValueAsString(jsonNodeTree);
} catch (JsonProcessingException e) {
throw new APIGenException("Error occurred while converting the swagger to YAML format", e);
}
}
OpenAPIV3Parser apiv3Parser = new OpenAPIV3Parser();
SwaggerParseResult swaggerParseResult = apiv3Parser.readContents(existingSwagger);
OpenAPI openAPI = swaggerParseResult.getOpenAPI();
Paths paths = openAPI.getPaths();
Paths newPaths = new Paths();
final Map<String, Object> dataMap = GenericApiObjectDefinition.getPathMap(api);
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
boolean pathItemExists = false;
PathItem pathItem;
if (paths.containsKey(entry.getKey())) {
pathItem = paths.get(entry.getKey());
pathItemExists = true;
} else {
pathItem = new PathItem();
}
Map<String, Object> methodMap = (Map<String, Object>) entry.getValue();
List<String> newMethodsList = new ArrayList<>();
for (Map.Entry<String, Object> methodEntry : methodMap.entrySet()) {
Operation operation = null;
boolean operationExists = false;
if (pathItemExists) {
newMethodsList.add(methodEntry.getKey());
switch(methodEntry.getKey()) {
case OPERATION_HTTP_GET:
operation = pathItem.getGet();
break;
case OPERATION_HTTP_POST:
operation = pathItem.getPost();
break;
case OPERATION_HTTP_DELETE:
operation = pathItem.getDelete();
break;
case OPERATION_HTTP_PUT:
operation = pathItem.getPut();
break;
case OPERATION_HTTP_HEAD:
operation = pathItem.getHead();
break;
case OPERATION_HTTP_PATCH:
operation = pathItem.getPatch();
break;
case OPERATION_HTTP_OPTIONS:
operation = pathItem.getOptions();
}
}
if (operation == null) {
operation = new Operation();
} else {
operationExists = true;
}
Object[] paramArr = (Object[]) ((Map<String, Object>) methodEntry.getValue()).get(PARAMETERS);
if (operationExists) {
List<Parameter> parameters = operation.getParameters();
List<Parameter> newParameter = new ArrayList<>();
if (paramArr != null && paramArr.length > 0) {
for (Object o : paramArr) {
String paramType = (String) ((Map<String, Object>) o).get(PARAMETER_IN);
String paramName = (String) ((Map<String, Object>) o).get(PARAMETER_NAME);
Optional<Parameter> existing = null;
switch(paramType) {
case PARAMETER_IN_PATH:
existing = parameters.stream().filter(c -> c.getName().equals(paramName) && c instanceof PathParameter).findFirst();
break;
case PARAMETER_IN_QUERY:
existing = parameters.stream().filter(c -> c.getName().equals(paramName) && c instanceof QueryParameter).findFirst();
break;
}
if (existing == null || !existing.isPresent()) {
// if we found parameter do not update
updatePathQueryAndBodyParams(operation, paramType, paramName, newParameter);
} else {
newParameter.add(existing.get());
}
updateDefaultResponseAndPathItem(pathItem, operation, methodEntry, operationExists);
}
} else {
// no parameters defined ( default resource in the API )
updateDefaultResponseAndPathItem(pathItem, operation, methodEntry, operationExists);
}
// remove deleted parameters from swagger
if (newParameter.size() > 0) {
parameters.removeIf(c -> !newParameter.contains(c));
}
} else {
populateParameters(pathItem, methodMap);
}
}
if (pathItemExists) {
// Remove additional methods
List<String> allMethodsList = Arrays.asList(new String[] { "get", "post", "put", "delete", "head", "options", "patch" });
List<String> differences = allMethodsList.stream().filter(element -> !newMethodsList.contains(element)).collect(Collectors.toList());
for (String method : differences) {
switch(method) {
case OPERATION_HTTP_GET:
pathItem.setGet(null);
break;
case OPERATION_HTTP_POST:
pathItem.setPost(null);
break;
case OPERATION_HTTP_DELETE:
pathItem.setDelete(null);
break;
case OPERATION_HTTP_PUT:
pathItem.setPut(null);
break;
case OPERATION_HTTP_HEAD:
pathItem.setHead(null);
break;
case OPERATION_HTTP_PATCH:
pathItem.setPatch(null);
break;
case OPERATION_HTTP_OPTIONS:
pathItem.setOptions(null);
break;
}
}
}
newPaths.put(entry.getKey(), pathItem);
}
// Adding the new path map
openAPI.setPaths(newPaths);
updateInfoSection(openAPI);
try {
updateServersSection(openAPI);
} catch (AxisFault axisFault) {
throw new APIGenException("Error occurred while getting host details", axisFault);
}
try {
if (isJSONOut) {
return Json.mapper().writeValueAsString(openAPI);
}
return Yaml.mapper().writeValueAsString(openAPI);
} catch (JsonProcessingException e) {
throw new APIGenException("Error occurred while creating the output JAML/JSON", e);
}
}
Aggregations