From d01e5ce5d6fcc454fa8bee81825761e28f937ccf Mon Sep 17 00:00:00 2001 From: Ashan Rathnaweera Date: Wed, 22 Jan 2025 11:48:41 +0530 Subject: [PATCH] Add support for mock implementation endpoints in API Products --- .../v1/common/SynapseArtifactGenerator.java | 2 +- .../v1/common/TemplateBuilderUtil.java | 29 +++++++++++++++++-- .../template/ResourceConfigContext.java | 2 ++ 3 files changed, 29 insertions(+), 4 deletions(-) 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java index b05f7e4ed571..273a684d2bb3 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/SynapseArtifactGenerator.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/SynapseArtifactGenerator.java @@ -102,7 +102,7 @@ public RuntimeArtifactDto generateGatewayArtifact(List ap ImportUtils.retrieveValidatedSwaggerDefinitionFromArchive(extractedFolderPath); apiProduct.setDefinition(apiDefinitionValidationResponse.getContent()); gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(apiProduct, environment, - tenantDomain, extractedFolderPath); + tenantDomain, extractedFolderPath, apiDefinitionValidationResponse); } else { APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath); API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java index 7635288ab397..cb125dddcebd 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/TemplateBuilderUtil.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/TemplateBuilderUtil.java @@ -674,7 +674,8 @@ public static GatewayAPIDTO retrieveGatewayAPIDtoForStreamingAPI(API api, Enviro } public static GatewayAPIDTO retrieveGatewayAPIDto(APIProduct apiProduct, Environment environment, - String tenantDomain, String extractedFolderPath) + String tenantDomain, String extractedFolderPath, + APIDefinitionValidationResponse apiDefinitionValidationResponse) throws APIManagementException, XMLStreamException, APITemplateException { List clientCertificatesDTOListProduction = @@ -683,12 +684,33 @@ public static GatewayAPIDTO retrieveGatewayAPIDto(APIProduct apiProduct, Environ ImportUtils.retrieveClientCertificates(extractedFolderPath, APIConstants.API_KEY_TYPE_SANDBOX); Map apidtoMap = retrieveAssociatedApis(extractedFolderPath); Map associatedAPIsMap = convertAPIIdToDto(apidtoMap.values()); + Set uriTemplates = Collections.emptySet(); + if (apiDefinitionValidationResponse.isValid()) { + APIDefinition parser = apiDefinitionValidationResponse.getParser(); + String definition = apiDefinitionValidationResponse.getJsonContent(); + if (parser != null) { + uriTemplates = parser.getURITemplates(definition); + } + } + for (APIProductResource productResource : apiProduct.getProductResources()) { String apiId = productResource.getApiId(); APIDTO apidto = associatedAPIsMap.get(apiId); if (apidto != null) { API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider()); productResource.setApiIdentifier(api.getId()); + if (APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase(api.getImplementation())) { + // Sync URITemplate mediation scripts if matches are found + for (URITemplate uriTemplate : uriTemplates) { + URITemplate template = productResource.getUriTemplate(); + if (template.getHTTPVerb().equalsIgnoreCase(uriTemplate.getHTTPVerb()) && + template.getUriTemplate().equals(uriTemplate.getUriTemplate())) { + template.setMediationScript(uriTemplate.getMediationScript()); + template.setMediationScripts(uriTemplate.getHTTPVerb(), uriTemplate.getMediationScript()); + break; + } + } + } if (api.isAdvertiseOnly()) { productResource.setEndpointConfig(APIUtil.generateEndpointConfigForAdvertiseOnlyApi(api)); } else { @@ -762,8 +784,9 @@ private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environ // check the endpoint type if (!StringUtils.isEmpty(api.getEndpointConfig())) { JsonObject endpointConfObj = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject(); - if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals( - endpointConfObj.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) { + if (!APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfObj.get(API_ENDPOINT_CONFIG_PROTOCOL_TYPE) + .getAsString()) && !APIConstants.IMPLEMENTATION_TYPE_INLINE.equalsIgnoreCase( + api.getImplementation())) { addEndpoints(api, apiTemplateBuilder, productAPIDto); } } else { 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/template/ResourceConfigContext.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/template/ResourceConfigContext.java index 389b22aafd14..2006e44d2c0b 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/template/ResourceConfigContext.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/template/ResourceConfigContext.java @@ -84,6 +84,8 @@ public VelocityContext getContext() { resource.getApiIdentifier() + ":" + resource.getUriTemplate().getUriTemplate(); if (resourceKey.equals(productResourceKey)) { resource.getUriTemplate().setHttpVerbs(uriTemplate.getHTTPVerb()); + resource.getUriTemplate() + .setMediationScripts(uriTemplate.getHTTPVerb(), uriTemplate.getMediationScript()); } } } else {