Skip to content

Commit a3c981b

Browse files
Add mandatory property validation to API import flow
1 parent b08daad commit a3c981b

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java

+15
Original file line numberDiff line numberDiff line change
@@ -3342,6 +3342,21 @@ public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper
33423342
boolean isApiProduct = apiTypeWrapper.isAPIProduct();
33433343
String workflowType;
33443344

3345+
if (!apiTypeWrapper.isAPIProduct()){
3346+
// validate custom API properties
3347+
if (StringUtils.equals(action, APIConstants.LC_PUBLISH_LC_STATE)) {
3348+
org.json.simple.JSONArray customProperties = APIUtil.getCustomProperties(this.tenantDomain);
3349+
List<String> errorProperties = APIUtil.validateMandatoryProperties(customProperties,
3350+
apiTypeWrapper.getApi().getAdditionalProperties());
3351+
3352+
if (!errorProperties.isEmpty()) {
3353+
String errorString = " : " + String.join(", ", errorProperties);
3354+
throw new APIManagementException(errorString, ExceptionCodes.from(ExceptionCodes
3355+
.ERROR_WHILE_UPDATING_MANDATORY_PROPERTIES));
3356+
}
3357+
}
3358+
}
3359+
33453360
if (isApiProduct) {
33463361
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
33473362
providerName = apiProduct.getId().getProviderName();

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

+41-3
Original file line numberDiff line numberDiff line change
@@ -10203,9 +10203,7 @@ public static String retrieveDefaultReservedUsername() {
1020310203
return defaultReservedUsername;
1020410204
}
1020510205

10206-
public static JSONArray getCustomProperties(String userId) throws APIManagementException {
10207-
10208-
String tenantDomain = MultitenantUtils.getTenantDomain(userId);
10206+
public static JSONArray getCustomProperties(String tenantDomain) throws APIManagementException {
1020910207

1021010208
JSONArray customPropertyAttributes = null;
1021110209
JSONObject propertyConfig = getMandatoryPropertyKeysFromRegistry(tenantDomain);
@@ -11604,4 +11602,44 @@ public static void validateApiWithFederatedGateway(API api) throws APIManagement
1160411602
+ api.getGatewayType(), e);
1160511603
}
1160611604
}
11605+
11606+
/**
11607+
* This method is used to validate the mandatory custom properties of an API
11608+
*
11609+
* @param customProperties custom properties of the API
11610+
* @param additionalPropertiesMap additional properties to validate
11611+
* @return list of erroneous property names. returns an empty array if there are no errors.
11612+
*/
11613+
public static List<String> validateMandatoryProperties(org.json.simple.JSONArray customProperties,
11614+
JSONObject additionalPropertiesMap) {
11615+
11616+
List<String> errorPropertyNames = new ArrayList<>();
11617+
11618+
for (int i = 0; i < customProperties.size(); i++) {
11619+
JSONObject property = (JSONObject) customProperties.get(i);
11620+
String propertyName = (String) property.get(APIConstants.CustomPropertyAttributes.NAME);
11621+
boolean isRequired = (boolean) property.get(APIConstants.CustomPropertyAttributes.REQUIRED);
11622+
if (isRequired) {
11623+
String mapPropertyDisplay = (String) additionalPropertiesMap.get(propertyName + "__display");
11624+
String mapProperty = (String) additionalPropertiesMap.get(propertyName);
11625+
11626+
if (mapProperty == null && mapPropertyDisplay == null) {
11627+
errorPropertyNames.add(propertyName);
11628+
continue;
11629+
}
11630+
String propertyValue = "";
11631+
String propertyValueDisplay = "";
11632+
if (mapProperty != null) {
11633+
propertyValue = mapProperty;
11634+
}
11635+
if (mapPropertyDisplay != null) {
11636+
propertyValueDisplay = mapPropertyDisplay;
11637+
}
11638+
if (propertyValue.isEmpty() && propertyValueDisplay.isEmpty()) {
11639+
errorPropertyNames.add(propertyName);
11640+
}
11641+
}
11642+
}
11643+
return errorPropertyNames;
11644+
}
1160711645
}

Diff for: components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/ApisApiServiceImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ public Response updateAPI(String apiId, APIDTO body, String ifMatch, MessageCont
975975
}
976976

977977
// validate custom properties
978-
org.json.simple.JSONArray customProperties = APIUtil.getCustomProperties(username);
978+
org.json.simple.JSONArray customProperties = APIUtil.getCustomProperties(organization);
979979
List<String> errorProperties = PublisherCommonUtils.validateMandatoryProperties(customProperties, body);
980980
if (!errorProperties.isEmpty()) {
981981
String errorString = " : " + String.join(", ", errorProperties);
@@ -3670,6 +3670,8 @@ public Response changeAPILifecycle(String action, String apiId, String lifecycle
36703670
} else if (isAuthorizationFailure(e)) {
36713671
RestApiUtil.handleAuthorizationFailure(
36723672
"Authorization failure while updating the lifecycle of API " + apiId, e, log);
3673+
} else if (e.getErrorHandler().getErrorCode() == ExceptionCodes.ERROR_WHILE_UPDATING_MANDATORY_PROPERTIES.getErrorCode()) {
3674+
RestApiUtil.handleBadRequest(e.getErrorHandler().getErrorDescription() + " on API " + apiId, e, log);
36733675
} else {
36743676
throw e;
36753677
}

Diff for: components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/impl/SettingsApiServiceImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Response getSettings(MessageContext messageContext){
5656
SettingsDTO settingsDTO = settingsMappingUtil.fromSettingstoDTO(isUserAvailable, organization);
5757
settingsDTO.setScopes(getScopeList());
5858
settingsDTO.setGatewayTypes(APIUtil.getGatewayTypes());
59-
settingsDTO.setCustomProperties(APIUtil.getCustomProperties(username));
59+
settingsDTO.setCustomProperties(APIUtil.getCustomProperties(organization));
6060
return Response.ok().entity(settingsDTO).build();
6161
} catch (APIManagementException | IOException e) {
6262
String errorMessage = "Error while retrieving Publisher Settings";

0 commit comments

Comments
 (0)