Skip to content

Add mandatory property validation to API import flow #13061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public enum ExceptionCodes implements ErrorHandler {
CANNOT_CREATE_API_VERSION(900362, "New API Version cannot be created from a different provider", 409, "Initial provider of an API must be preserved in all versions of that API"),
INTERNAL_ERROR_WHILE_UPDATING_API(900363, "Internal Server Error occurred while updating the API", 500, "Internal Server Error. '%s'"),
ERROR_WHILE_UPDATING_MANDATORY_PROPERTIES(903010, "Error while updating required properties", 400, "Error while updating required properties."),
ERROR_WHILE_VALIDATING_MANDATORY_PROPERTIES(903015, "Error while validating required properties", 400, "Error while validating required properties."),

//Lifecycle related codes
API_UPDATE_FORBIDDEN_PER_LC(900380, "Insufficient permission to update the API", 403,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3354,6 +3354,19 @@ public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper
apiOrApiProductId = apiMgtDAO.getAPIProductId(apiTypeWrapper.getApiProduct().getId());
workflowType = WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE;
} else {
// validate mandatory API properties
if (StringUtils.equals(action, APIConstants.LC_PUBLISH_LC_STATE)) {
org.json.simple.JSONArray customProperties = APIUtil.getCustomProperties(this.tenantDomain);
List<String> errorProperties = APIUtil.validateMandatoryProperties(customProperties,
apiTypeWrapper.getApi().getAdditionalProperties());

if (!errorProperties.isEmpty()) {
String errorString = " : " + String.join(", ", errorProperties);
throw new APIManagementException(errorString, ExceptionCodes.from(ExceptionCodes
.ERROR_WHILE_VALIDATING_MANDATORY_PROPERTIES));
}
}

API api = apiTypeWrapper.getApi();
providerName = api.getId().getProviderName();
apiName = api.getId().getApiName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10203,9 +10203,7 @@ public static String retrieveDefaultReservedUsername() {
return defaultReservedUsername;
}

public static JSONArray getCustomProperties(String userId) throws APIManagementException {

String tenantDomain = MultitenantUtils.getTenantDomain(userId);
public static JSONArray getCustomProperties(String tenantDomain) throws APIManagementException {

JSONArray customPropertyAttributes = null;
JSONObject propertyConfig = getMandatoryPropertyKeysFromRegistry(tenantDomain);
Expand Down Expand Up @@ -11604,4 +11602,44 @@ public static void validateApiWithFederatedGateway(API api) throws APIManagement
+ api.getGatewayType(), e);
}
}

/**
* This method is used to validate the mandatory custom properties of an API
*
* @param customProperties custom properties of the API
* @param additionalPropertiesMap additional properties to validate
* @return list of erroneous property names. returns an empty array if there are no errors.
*/
public static List<String> validateMandatoryProperties(org.json.simple.JSONArray customProperties,
JSONObject additionalPropertiesMap) {

List<String> errorPropertyNames = new ArrayList<>();

for (int i = 0; i < customProperties.size(); i++) {
JSONObject property = (JSONObject) customProperties.get(i);
String propertyName = (String) property.get(APIConstants.CustomPropertyAttributes.NAME);
boolean isRequired = (boolean) property.get(APIConstants.CustomPropertyAttributes.REQUIRED);
if (isRequired) {
String mapPropertyDisplay = (String) additionalPropertiesMap.get(propertyName + "__display");
String mapProperty = (String) additionalPropertiesMap.get(propertyName);

if (mapProperty == null && mapPropertyDisplay == null) {
errorPropertyNames.add(propertyName);
continue;
}
String propertyValue = "";
String propertyValueDisplay = "";
if (mapProperty != null) {
propertyValue = mapProperty;
}
if (mapPropertyDisplay != null) {
propertyValueDisplay = mapPropertyDisplay;
}
if (propertyValue.isEmpty() && propertyValueDisplay.isEmpty()) {
errorPropertyNames.add(propertyName);
}
}
}
return errorPropertyNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ public Response updateAPI(String apiId, APIDTO body, String ifMatch, MessageCont
}

// validate custom properties
org.json.simple.JSONArray customProperties = APIUtil.getCustomProperties(username);
org.json.simple.JSONArray customProperties = APIUtil.getCustomProperties(organization);
List<String> errorProperties = PublisherCommonUtils.validateMandatoryProperties(customProperties, body);
if (!errorProperties.isEmpty()) {
String errorString = " : " + String.join(", ", errorProperties);
Expand Down Expand Up @@ -3670,6 +3670,8 @@ public Response changeAPILifecycle(String action, String apiId, String lifecycle
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(
"Authorization failure while updating the lifecycle of API " + apiId, e, log);
} else if (e.getErrorHandler().getErrorCode() == ExceptionCodes.ERROR_WHILE_UPDATING_MANDATORY_PROPERTIES.getErrorCode()) {
RestApiUtil.handleBadRequest(e.getErrorHandler().getErrorDescription() + " on API " + apiId, e, log);
} else {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Response getSettings(MessageContext messageContext){
SettingsDTO settingsDTO = settingsMappingUtil.fromSettingstoDTO(isUserAvailable, organization);
settingsDTO.setScopes(getScopeList());
settingsDTO.setGatewayTypes(APIUtil.getGatewayTypes());
settingsDTO.setCustomProperties(APIUtil.getCustomProperties(username));
settingsDTO.setCustomProperties(APIUtil.getCustomProperties(organization));
return Response.ok().entity(settingsDTO).build();
} catch (APIManagementException | IOException e) {
String errorMessage = "Error while retrieving Publisher Settings";
Expand Down
Loading