Skip to content

Commit

Permalink
Fix to save advance configs under x-wso2 production and sandbox endpo…
Browse files Browse the repository at this point in the history
…ints.

Fixes wso2/api-manager#2280
  • Loading branch information
RusJaI committed Dec 11, 2023
1 parent 5961120 commit 1daeb9e
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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 {
Expand All @@ -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);
}

Expand Down

0 comments on commit 1daeb9e

Please sign in to comment.