Skip to content

Commit

Permalink
Merge pull request #12835 from AqeelMuhammad/fix/3526-description-issue
Browse files Browse the repository at this point in the history
Fix: 3526 - Add _overview doc validation for document creation API
  • Loading branch information
hisanhunais authored Feb 28, 2025
2 parents 1ca402c + 81dca90 commit 83cefb5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,18 @@ void deleteAPIProduct(APIProductIdentifier identifier, String apiProductUUID, St
*/
boolean isDocumentationExist(String uuid, String docName, String organization) throws APIManagementException;

/**
* Checks whether the given document already exists for the given api/product
*
* @param uuid API/Product id
* @param documentId updating document id
* @param docOtherTypeName Name of the other document type
* @param organization Identifier of the organization
* @return true if document already exists for the given api/product
* @throws APIManagementException if failed to check existence of the documentation
*/
boolean isAnotherOverviewDocumentationExist(String uuid, String documentId, String docOtherTypeName, String organization) throws APIManagementException;

/**
* Add WSDL to the api. wsdl can be provided either as a url or a resource file
* @param apiId ID of the API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2518,6 +2518,27 @@ public boolean isDocumentationExist(String uuid, String docName, String organiza
return exist;
}

@Override
public boolean isAnotherOverviewDocumentationExist(String uuid, String documentId, String docOtherTypeName, String organization) throws APIManagementException {
boolean exist = false;
UserContext ctx = null;
try {
DocumentSearchResult result = apiPersistenceInstance.searchDocumentation(new Organization(organization), uuid, 0, 0,
"other:_overview", ctx);
if (result != null && result.getDocumentationList() != null && !result.getDocumentationList().isEmpty()) {
String returnDocOtherTypeName = result.getDocumentationList().get(0).getOtherTypeName();
String returnDocId = result.getDocumentationList().get(0).getId();
if ((documentId == null || !documentId.equals(returnDocId))
&& returnDocOtherTypeName != null && returnDocOtherTypeName.equals(docOtherTypeName)) {
exist = true;
}
}
} catch (DocumentationPersistenceException e) {
handleException("Failed to search documentation for other type name " + docOtherTypeName, e);
}
return exist;
}

/**
* Returns the details of all the life-cycle changes done per API or API Product
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2817,6 +2817,13 @@ public DocumentSearchResult searchDocumentation(Organization org, String apiId,
if (doc.getName().equalsIgnoreCase(requestedDocName)) {
documentationList.add(doc);
}
}
else if (searchQuery.toLowerCase().startsWith("other:")) {
String requestedDocOtherTypeName = searchQuery.split(":")[1];
if (doc.getOtherTypeName() != null
&& doc.getOtherTypeName().equalsIgnoreCase(requestedDocOtherTypeName)) {
documentationList.add(doc);
}
} else {
log.warn("Document search not implemented for the query " + searchQuery);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2613,6 +2613,11 @@ public static Documentation addDocumentationToAPI(DocumentDTO documentDto, Strin
throw new APIManagementException("Requested document '" + documentName + "' already exists",
ExceptionCodes.DOCUMENT_ALREADY_EXISTS);
}
if (documentDto.getType() == DocumentDTO.TypeEnum.OTHER && documentDto.getOtherTypeName() != null && apiProvider
.isAnotherOverviewDocumentationExist(apiId, null, documentDto.getOtherTypeName(), organization)) {
throw new APIManagementException("Requested other document type _overview already exists",
ExceptionCodes.DOCUMENT_ALREADY_EXISTS);
}
documentation = apiProvider.addDocumentation(apiId, documentation, organization);

return documentation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ public Response updateAPIProductDocument(String apiProductId, String documentId,
RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
return null;
}
if (body.getType() == DocumentDTO.TypeEnum.OTHER
&& body.getOtherTypeName() != null
&& apiProvider.isAnotherOverviewDocumentationExist(apiProductId, documentId, body.getOtherTypeName(), organization)) {
RestApiUtil.handleBadRequest("Requested other document type _overview already exists", log);
return null;
}

//overriding some properties
body.setName(oldDocument.getName());
Expand Down Expand Up @@ -449,6 +455,11 @@ public Response addAPIProductDocument(String apiProductId, DocumentDTO body,
String errorMessage = "Requested document '" + documentName + "' already exists";
RestApiUtil.handleResourceAlreadyExistsError(errorMessage, log);
}
if (body.getType() == DocumentDTO.TypeEnum.OTHER
&& body.getOtherTypeName() != null
&& apiProvider.isAnotherOverviewDocumentationExist(apiProductId, null, body.getOtherTypeName(), organization)) {
RestApiUtil.handleBadRequest("Requested other document type _overview already exists", log);
}
documentation = apiProvider.addDocumentation(apiProductId, documentation, organization);

DocumentDTO newDocumentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,12 @@ public Response updateAPIDocument(String apiId, String documentId, DocumentDTO b
RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
return null;
}
if (body.getType() == DocumentDTO.TypeEnum.OTHER
&& body.getOtherTypeName() != null
&& apiProvider.isAnotherOverviewDocumentationExist(apiId, documentId, body.getOtherTypeName(), organization)) {
RestApiUtil.handleBadRequest("Requested other document type _overview already exists", log);
return null;
}

//overriding some properties
body.setName(oldDocument.getName());
Expand Down

0 comments on commit 83cefb5

Please sign in to comment.