From 3fa71ef7387b38cfad03dcbd0da424f9297dee2d Mon Sep 17 00:00:00 2001 From: Avishka-Shamendra Date: Wed, 18 Sep 2024 12:20:03 +0530 Subject: [PATCH] Fix API definition content search with anonymous user --- .../persistence/RegistryPersistenceImpl.java | 56 +++++++++---------- .../mappings/SearchResultMappingUtil.java | 2 +- .../v1/mappings/SearchResultMappingUtil.java | 2 +- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java index 0f0a95a58d56..dbd0900f19b6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java @@ -4114,7 +4114,7 @@ private void addAPIDefinitionSearchContent(String resourcePath, Registry registr } else { index = resourcePath.indexOf(APIConstants.API_OAS_DEFINITION_RESOURCE_NAME); // swagger.json is included in all types of APIs, hence we need to properly set the API type - setAPITypeForSwagger(resourcePath, index, content, registry); + setAPITypeForSwagger(resourcePath, index, content, registry, apiArtifactManager); } String apiPath = resourcePath.substring(0, index) + APIConstants.API_KEY; @@ -4160,39 +4160,37 @@ private void addAPIDefinitionSearchContent(String resourcePath, Registry registr * This method is used to set the correct API Type for swagger.json as all API types have a swagger.json * file in registry * - * @param resourcePath registry resourcePath - * @param index int index - * @param content APIDefSearchContent obj. - * @param registry registry obj. + * @param resourcePath registry resourcePath + * @param index int index + * @param content APIDefSearchContent obj. + * @param registry registry obj. + * @param artifactManager artifact manager * @throws RegistryException on failure */ private void setAPITypeForSwagger(String resourcePath, int index, - APIDefSearchContent content, Registry registry) throws RegistryException { - String directoryPath = resourcePath.substring(0, index); - Resource directoryResource = registry.get(directoryPath); - - if (directoryResource instanceof Collection) { - Collection collection = (Collection) directoryResource; - int childrenCount = collection.getChildCount(); - String[] childPaths = collection.getChildren(); - if (childrenCount > 2) { - for (String childPath : childPaths) { - if (childPath.contains(APIConstants.API_ASYNC_API_DEFINITION_RESOURCE_NAME)) { - content.setApiType(APIDefSearchContent.ApiType.ASYNC); - break; - } else if (childPath.contains(APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION)) { - content.setApiType(APIDefSearchContent.ApiType.GRAPHQL); - break; - } else if (childPath.contains(APIConstants.WSDL_FILE_EXTENSION)) { - content.setApiType(APIDefSearchContent.ApiType.SOAP); - break; - } - } - } - if (content.getApiType() == null) { + APIDefSearchContent content, Registry registry, + GenericArtifactManager + artifactManager) throws RegistryException { + + String apiPath = resourcePath.substring(0, index) + APIConstants.API_KEY; + Resource apiResource = registry.get(apiPath); + String apiArtifactId = apiResource.getUUID(); + if (apiArtifactId != null) { + GenericArtifact artifact = artifactManager.getGenericArtifact(apiArtifactId); + String type = artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE); + if (APIConstants.API_TYPE_SOAP.equals(type) || + APIConstants.API_TYPE_SOAPTOREST.equals(type)) { + content.setApiType(APIDefSearchContent.ApiType.SOAP); + } else if (APIConstants.API_TYPE_GRAPHQL.equals(type)) { + content.setApiType(APIDefSearchContent.ApiType.GRAPHQL); + } else if (APIConstants.API_TYPE_WS.equals(type) || + APIConstants.API_TYPE_WEBHOOK.equals(type) || + APIConstants.API_TYPE_SSE.equals(type) || + APIConstants.API_TYPE_WEBSUB.equals(type)) { + content.setApiType(APIDefSearchContent.ApiType.ASYNC); + } else { content.setApiType(APIDefSearchContent.ApiType.REST); } } - } } 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/SearchResultMappingUtil.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/SearchResultMappingUtil.java index e655ad7cb71f..6bddea50a028 100644 --- 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/SearchResultMappingUtil.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/SearchResultMappingUtil.java @@ -257,7 +257,7 @@ public static void setPaginationParams(SearchResultListDTO resultListDTO, String apiDefSearchResultDTO.setApiType(apiDefResult.getApiType()); apiDefSearchResultDTO.setAssociatedType(apiDefResult.getAssociatedType()); if (apiDefResult.getName().contains("swagger")) { - apiDefSearchResultDTO.setName(apiDefResult.getApiName() + " Swagger Definition"); + apiDefSearchResultDTO.setName(apiDefResult.getApiName() + " REST API Definition"); } else if (apiDefResult.getName().contains("graphql")) { apiDefSearchResultDTO.setName(apiDefResult.getApiName() + " GraphQL Definition"); } else if (apiDefResult.getName().contains("async")) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SearchResultMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SearchResultMappingUtil.java index ff0530f40d5a..6a137f84fefb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SearchResultMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SearchResultMappingUtil.java @@ -200,7 +200,7 @@ public static void setPaginationParams(SearchResultListDTO resultListDTO, String apiDefSearchResultDTO.setApiType(apiDefResult.getApiType()); apiDefSearchResultDTO.setAssociatedType(apiDefResult.getAssociatedType()); if (apiDefResult.getName().contains("swagger")) { - apiDefSearchResultDTO.setName(apiDefResult.getApiName() + " Swagger Definition"); + apiDefSearchResultDTO.setName(apiDefResult.getApiName() + " REST API Definition"); } else if (apiDefResult.getName().contains("graphql")) { apiDefSearchResultDTO.setName(apiDefResult.getApiName() + " GraphQL Definition"); } else if (apiDefResult.getName().contains("async")) {