From 6fe32e6bade40d5097f8023838a66c619d8e95e8 Mon Sep 17 00:00:00 2001 From: gcornacchia Date: Wed, 12 Jul 2023 18:23:59 +0200 Subject: [PATCH] added test in case scheme is https but backendbasepath is http --- .../specification/Swagger2xSpecification.java | 5 ++++- .../APISpecificationSwagger2xTest.java | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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 5a68e202c..d931696bb 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 @@ -93,17 +93,20 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti } URL url = new URL(backendBasePath); String port = url.getPort() == -1 ? ":" + url.getDefaultPort() : ":" + url.getPort(); + Boolean adjustedHost = false; if (port.equals(":443") || port.equals(":80")) port = ""; if (swagger.get("host") == null) { LOG.debug("Adding new host {}{} to Swagger-File based on backendBasePath: {}", url.getHost(), port, backendBasePath); ((ObjectNode) swagger).put("host", url.getHost() + port); LOG.info("Used the backendBasePath: {} to adjust host the API-Specification.", backendBasePath); + adjustedHost = true; }else { if(swagger.get("host").asText().equals(url.getHost()+port)) { LOG.debug("Swagger Host: '{}' already matches backendBasePath: '{}'. Nothing to do.",swagger.get("host").asText(),backendBasePath); } else if (CoreParameters.getInstance().isOverrideSpecBasePath()){ LOG.debug("Replacing existing host: '"+swagger.get("host").asText()+"' in Swagger-File to '"+url.getHost()+port+"' based on configured backendBasePath: '"+backendBasePath+"'"); ((ObjectNode)swagger).put("host", url.getHost()+port); + adjustedHost = true; } } //what if the backendBasePath is http? @@ -113,7 +116,7 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti LOG.debug("Adding protocol: {} to Swagger-Definition", url.getProtocol()); ((ObjectNode) swagger).set("schemes", newSchemes); } else { - if (CoreParameters.getInstance().isOverrideSpecBasePath()) { + if (CoreParameters.getInstance().isOverrideSpecBasePath() || adjustedHost) { //I may have a situation where a backendbasepath in http must overwrite host but in swagger file it's declared a scheme in https which is not coherent ArrayNode schemes = (ArrayNode) swagger.get("schemes"); schemes.removeAll(); diff --git a/modules/apim-adapter/src/test/java/com/axway/apim/api/specification/APISpecificationSwagger2xTest.java b/modules/apim-adapter/src/test/java/com/axway/apim/api/specification/APISpecificationSwagger2xTest.java index fbd07e442..060ed0b01 100644 --- a/modules/apim-adapter/src/test/java/com/axway/apim/api/specification/APISpecificationSwagger2xTest.java +++ b/modules/apim-adapter/src/test/java/com/axway/apim/api/specification/APISpecificationSwagger2xTest.java @@ -278,4 +278,20 @@ public void testReplaceAlsoHostInSwagger() throws IOException{ Assert.assertEquals(swagger.get("schemes").size(), 1); } + @Test + public void testSwaggerWithoutHost() throws IOException{ + CoreParameters.getInstance().setOverrideSpecBasePath(false); + byte[] content = getSwaggerContent(testPackage + "/petstore-without-host.json"); + APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "Test-API"); + apiDefinition.configureBasePath("http://anotherHost/test", null); + + Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification); + JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent()); + Assert.assertEquals(swagger.get("host").asText(), "anotherHost"); + Assert.assertEquals(swagger.get("basePath").asText(), "/v2"); + Assert.assertEquals(swagger.get("schemes").get(0).asText(), "http"); + Assert.assertEquals(swagger.get("schemes").size(), 1); + } + + }