diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java index af4f5241ea75..4d32f5bf0e85 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java @@ -18,6 +18,8 @@ package org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import graphql.language.FieldDefinition; import graphql.language.ObjectTypeDefinition; @@ -29,6 +31,7 @@ import graphql.schema.idl.errors.SchemaProblem; import graphql.schema.validation.SchemaValidationError; import graphql.schema.validation.SchemaValidator; +import io.swagger.v3.parser.ObjectMapperFactory; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -1468,9 +1471,11 @@ private static void prepareForUpdateSwagger(String apiId, APIDefinitionValidatio existingAPI.setUriTemplates(uriTemplates); existingAPI.setScopes(scopes); try { - JSONObject updatedApiJson = (JSONObject) new JSONParser().parse(apiDefinition); - JSONObject newProductionEndpointJson = (JSONObject) updatedApiJson.get(APIConstants.X_WSO2_PRODUCTION_ENDPOINTS); - JSONObject newSandboxEndpointJson = (JSONObject) updatedApiJson.get(APIConstants.X_WSO2_SANDBOX_ENDPOINTS); + ObjectMapper mapper = ObjectMapperFactory.createJson(); + JsonNode newProductionEndpointJson = mapper.readTree(apiDefinition) + .get(APIConstants.X_WSO2_PRODUCTION_ENDPOINTS); + JsonNode newSandboxEndpointJson = mapper.readTree(apiDefinition) + .get(APIConstants.X_WSO2_SANDBOX_ENDPOINTS); String existingEndpointConfigString = existingAPI.getEndpointConfig(); if (StringUtils.isNotEmpty(existingEndpointConfigString)) { //check if endpoints are configured @@ -1486,9 +1491,9 @@ private static void prepareForUpdateSwagger(String apiId, APIDefinitionValidatio for (int i = 0; i < productionConfigsJson.size(); i++) { if (!(((JSONObject) productionConfigsJson.get(i)).containsKey(APIConstants .API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - if (newProductionEndpointJson.containsKey(APIConstants + if (newProductionEndpointJson.has(APIConstants .ADVANCE_ENDPOINT_CONFIG)) { - JSONObject advanceConfig = (JSONObject) newProductionEndpointJson + JsonNode advanceConfig = newProductionEndpointJson .get(APIConstants.ADVANCE_ENDPOINT_CONFIG); ((JSONObject) productionConfigsJson.get(i)) .put(APIConstants.ADVANCE_ENDPOINT_CONFIG, advanceConfig); @@ -1504,8 +1509,8 @@ private static void prepareForUpdateSwagger(String apiId, APIDefinitionValidatio } else { JSONObject productionConfigsJson = (JSONObject) existingEndpointConfigJson .get(APIConstants.ENDPOINT_PRODUCTION_ENDPOINTS); - if (newProductionEndpointJson.containsKey(APIConstants.ADVANCE_ENDPOINT_CONFIG)) { - JSONObject advanceConfig = (JSONObject) newProductionEndpointJson + if (newProductionEndpointJson.has(APIConstants.ADVANCE_ENDPOINT_CONFIG)) { + JsonNode advanceConfig = newProductionEndpointJson .get(APIConstants.ADVANCE_ENDPOINT_CONFIG); productionConfigsJson.put(APIConstants.ADVANCE_ENDPOINT_CONFIG, advanceConfig); } else { @@ -1527,9 +1532,9 @@ private static void prepareForUpdateSwagger(String apiId, APIDefinitionValidatio for (int i = 0; i < sandboxConfigsJson.size(); i++) { if (!(((JSONObject) sandboxConfigsJson.get(i)).containsKey(APIConstants .API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) { - if (newSandboxEndpointJson.containsKey(APIConstants + if (newSandboxEndpointJson.has(APIConstants .ADVANCE_ENDPOINT_CONFIG)) { - JSONObject advanceConfig = (JSONObject) newSandboxEndpointJson + JsonNode advanceConfig = newSandboxEndpointJson .get(APIConstants.ADVANCE_ENDPOINT_CONFIG); ((JSONObject) sandboxConfigsJson.get(i)) .put(APIConstants.ADVANCE_ENDPOINT_CONFIG, advanceConfig); @@ -1545,8 +1550,8 @@ private static void prepareForUpdateSwagger(String apiId, APIDefinitionValidatio } else { JSONObject sandboxConfigsJson = (JSONObject) existingEndpointConfigJson .get(APIConstants.ENDPOINT_SANDBOX_ENDPOINTS); - if (newSandboxEndpointJson.containsKey(APIConstants.ADVANCE_ENDPOINT_CONFIG)) { - JSONObject advanceConfig = (JSONObject) newSandboxEndpointJson + if (newSandboxEndpointJson.has(APIConstants.ADVANCE_ENDPOINT_CONFIG)) { + JsonNode advanceConfig = newSandboxEndpointJson .get(APIConstants.ADVANCE_ENDPOINT_CONFIG); sandboxConfigsJson.put(APIConstants.ADVANCE_ENDPOINT_CONFIG, advanceConfig); } else { @@ -1559,7 +1564,7 @@ private static void prepareForUpdateSwagger(String apiId, APIDefinitionValidatio } existingAPI.setEndpointConfig(existingEndpointConfigJson.toString()); } - } catch (ParseException e) { + } catch (ParseException | JsonProcessingException e) { throw new APIManagementException("Error when parsing endpoint configurations ", e); }