From b8136c58b1f319ec6b2318ef6553ef26a5836ff6 Mon Sep 17 00:00:00 2001 From: rathnapandi Date: Tue, 5 Nov 2024 14:26:26 -0700 Subject: [PATCH] - Fix the filename extension issue --- .../api/specification/OAS3xSpecification.java | 2 +- .../specification/Swagger2xSpecification.java | 2 +- .../apim/api/export/impl/ExportHelper.java | 42 +++++++++++++------ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/OAS3xSpecification.java b/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/OAS3xSpecification.java index a9d5e4e52..78e93c2ab 100644 --- a/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/OAS3xSpecification.java +++ b/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/OAS3xSpecification.java @@ -31,7 +31,7 @@ public OAS3xSpecification() { @Override public APISpecType getAPIDefinitionType() throws AppException { - if (this.mapper.getFactory() instanceof YAMLFactory) { + if (mapper.getFactory() instanceof YAMLFactory) { return APISpecType.OPEN_API_30_YAML; } return APISpecType.OPEN_API_30; diff --git a/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/Swagger2xSpecification.java b/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/Swagger2xSpecification.java index 99f6ccf08..9bc8816cf 100644 --- a/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/Swagger2xSpecification.java +++ b/modules/apim-adapter/src/main/java/com/axway/apim/api/specification/Swagger2xSpecification.java @@ -31,7 +31,7 @@ public Swagger2xSpecification() { @Override public APISpecType getAPIDefinitionType() throws AppException { - if (this.mapper.getFactory() instanceof YAMLFactory) { + if (mapper.getFactory() instanceof YAMLFactory) { return APISpecType.SWAGGER_API_20_YAML; } return APISpecType.SWAGGER_API_20; diff --git a/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java b/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java index 6d5f767bf..c9b661f64 100644 --- a/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java +++ b/modules/apis/src/main/java/com/axway/apim/api/export/impl/ExportHelper.java @@ -10,6 +10,8 @@ import com.axway.apim.api.model.CaCert; import com.axway.apim.api.model.Image; import com.axway.apim.api.specification.APISpecification; +import com.axway.apim.api.specification.OAS3xSpecification; +import com.axway.apim.api.specification.Swagger2xSpecification; import com.axway.apim.api.specification.WSDLSpecification; import com.axway.apim.lib.EnvironmentProperties; import com.axway.apim.lib.error.AppException; @@ -22,6 +24,7 @@ import com.fasterxml.jackson.databind.ser.FilterProvider; import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,18 +62,7 @@ public void saveAPILocally(ObjectMapper mapper, ExportAPI exportAPI, String conf throw new AppException("Backend API Definition is not available for the API : " + exportAPI.getName() + ", hence use the option -useFEAPIDefinition to export API", ErrorCode.BACKEND_API_DEF_NA); return; } - String targetFile = null; - try { - String fileName = Utils.replaceSpecialChars(exportAPI.getName()) + apiDef.getAPIDefinitionType().getFileExtension(); - targetFile = localFolder.getCanonicalPath() + "/" + fileName; - if (!(apiDef instanceof WSDLSpecification && EnvironmentProperties.RETAIN_BACKEND_URL) && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) { - Object spec = mapper.readValue(apiDef.getApiSpecificationContent(), Object.class); - mapper.writerWithDefaultPrettyPrinter().writeValue(new File(targetFile), spec); - exportAPI.getAPIDefinition().setApiSpecificationFile(fileName); - } - } catch (IOException e) { - throw new AppException("Can't save API-Definition locally to file: " + targetFile, ErrorCode.UNXPECTED_ERROR, e); - } + writeSpec(mapper, apiDef, exportAPI, localFolder); Image image = exportAPI.getAPIImage(); if (image != null && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) { writeBytesToFile(image.getImageContent(), localFolder + File.separator + image.getBaseFilename()); @@ -151,6 +143,32 @@ private void storePrivateCerts(File localFolder, List aut } } + public void writeSpec(ObjectMapper mapper, APISpecification apiDef, ExportAPI exportAPI, File localFolder) throws AppException { + String targetFile = null; + try { + if (!(apiDef instanceof WSDLSpecification && EnvironmentProperties.RETAIN_BACKEND_URL) && (!EnvironmentProperties.PRINT_CONFIG_CONSOLE)) { + String fileName = Utils.replaceSpecialChars(exportAPI.getName()); + String fileExtension = apiDef.getAPIDefinitionType().getFileExtension(); + if(apiDef instanceof Swagger2xSpecification || apiDef instanceof OAS3xSpecification){ + if (mapper.getFactory() instanceof YAMLFactory) { + fileExtension = APISpecification.APISpecType.SWAGGER_API_20_YAML.getFileExtension(); + }else { + fileExtension = APISpecification.APISpecType.SWAGGER_API_20.getFileExtension(); + } + targetFile = localFolder.getCanonicalPath() + "/" + fileName + fileExtension; + Object spec = mapper.readValue(apiDef.getApiSpecificationContent(), Object.class); + mapper.writerWithDefaultPrettyPrinter().writeValue(new File(targetFile), spec); + }else { + targetFile = localFolder.getCanonicalPath() + "/" + fileName + fileExtension; + writeBytesToFile(apiDef.getApiSpecificationContent(), targetFile); + } + exportAPI.getAPIDefinition().setApiSpecificationFile(fileName); + } + } catch (IOException e) { + throw new AppException("Can't save API-Definition locally to file: " + targetFile, ErrorCode.UNXPECTED_ERROR, e); + } + } + public void writeContent(ObjectMapper mapper, ExportAPI exportAPI, File localFolder, String configFile) throws AppException { try {