Skip to content

Commit

Permalink
Merge pull request #414 from gcornacchia/develop
Browse files Browse the repository at this point in the history
pull request handling host field when overrideBasePathSpec = true
  • Loading branch information
rathnapandi authored Jul 17, 2023
2 parents cc2b5bd + 6fe32e6 commit d6c3f56
Show file tree
Hide file tree
Showing 5 changed files with 1,352 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
import com.axway.apim.lib.CoreParameters;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.error.ErrorCode;
import com.axway.apim.lib.utils.URLParser;
import com.axway.apim.lib.utils.Utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;

public class OAS3xSpecification extends APISpecification {
Expand Down Expand Up @@ -103,7 +109,7 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
if (urlJsonNode != null) {
String serverUrl = urlJsonNode.asText();
if (CoreParameters.getInstance().isOverrideSpecBasePath()) {
overrideServerSection(backendBasePath); // override openapi url with backendBaseapath
overrideServerSection(backendBasePath,serverUrl); // override openapi url with backendBaseapath
} else if (!serverUrl.startsWith("http")) { // If url does not have hostname, add hostname from backendBasepath
updateServerSection(backendBasePath, serverUrl);
}
Expand Down Expand Up @@ -131,11 +137,19 @@ public void updateServerSection(String backendBasePath, String serverUrl) throws
((ObjectNode) openAPI).set(SERVERS, mapper.createArrayNode().add(newServer));
}

public void overrideServerSection(String backendBasePath) {
if (backendBasePath.endsWith("/"))
backendBasePath = backendBasePath.substring(0, backendBasePath.length() - 1);
LOG.info("overriding openapi Servers url with value : {}", backendBasePath);
ObjectNode newServer = createObjectNode("url", backendBasePath);
public void overrideServerSection(String backendBasePath, String serverUrl) throws MalformedURLException, URISyntaxException {
URL backendBasePathURL = new URL(backendBasePath);
URI serverURL = URI.create(serverUrl);
String newUrl = backendBasePath;
if (StringUtils.isEmpty(backendBasePathURL.getPath())){
newUrl = new URIBuilder(URI.create(serverUrl))
.setHost(backendBasePathURL.getHost())
.setPort(backendBasePathURL.getPort())
.setScheme(backendBasePathURL.getProtocol())
.build().toString();
}
LOG.info("overriding openapi Servers url with value : {}", newUrl);
ObjectNode newServer = createObjectNode("url", newUrl);
((ObjectNode) openAPI).set(SERVERS, mapper.createArrayNode().add(newServer));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,36 @@ 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?
if (swagger.get("schemes") == null) {
ArrayNode newSchemes = this.mapper.createArrayNode();
newSchemes.add(url.getProtocol());
LOG.debug("Adding protocol: {} to Swagger-Definition", url.getProtocol());
((ObjectNode) swagger).set("schemes", newSchemes);
} else {
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();
schemes.add(url.getProtocol());
LOG.debug("Setting protocol: {} to Swagger-Definition", url.getProtocol());
}
}
if (swagger.get("basePath") == null) {
LOG.info("Adding default basePath / to swagger");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ public void replaceServerURLIfHostNameIsNotPresent() throws IOException {
Assert.assertEquals("https://myhost.customer.com:8767/api/v3", swagger.get("servers").get(0).get("url").asText());
}

@Test
public void replaceServerURLIfHostNameIsPresent() throws IOException {
CoreParameters coreParameters = CoreParameters.getInstance();
coreParameters.setOverrideSpecBasePath(false);
byte[] content = getSwaggerContent(TEST_PACKAGE + "/openapi-with-host.json");
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "TestAPI");
apiDefinition.configureBasePath("https://myhost.customer.com:8767", null);
// Check if the Swagger-File has been changed
Assert.assertTrue(apiDefinition instanceof OAS3xSpecification);
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
Assert.assertEquals(swagger.get("servers").size(), 1, "Expected to get only one server url");
Assert.assertEquals("https://myhost/api/v3", swagger.get("servers").get(0).get("url").asText());
}

@Test
public void replaceServerURLIfHostNameIsPresent2() throws IOException {
CoreParameters coreParameters = CoreParameters.getInstance();
coreParameters.setOverrideSpecBasePath(true);
byte[] content = getSwaggerContent(TEST_PACKAGE + "/openapi-with-host.json");
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "TestAPI");
apiDefinition.configureBasePath("https://myhost.customer.com:8767", null);
// Check if the Swagger-File has been changed
Assert.assertTrue(apiDefinition instanceof OAS3xSpecification);
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
Assert.assertEquals(swagger.get("servers").size(), 1, "Expected to get only one server url");
Assert.assertEquals("https://myhost.customer.com:8767/api/v3", swagger.get("servers").get(0).get("url").asText());
}

@Test
public void replaceServerURLIfHostNameIsNotPresentWithBackendBasePath() throws IOException {
CoreParameters coreParameters = CoreParameters.getInstance();
Expand Down Expand Up @@ -259,4 +287,18 @@ public void overrideServerURLWithBackendBasePath() throws IOException {
Assert.assertEquals(swagger.get("servers").size(), 1, "Expected to get only one server url");
Assert.assertEquals("https://myhost.customer.com:8767/test", swagger.get("servers").get(0).get("url").asText());
}

@Test
public void overrideServerURLWithBackendBasePath2() throws IOException {
CoreParameters coreParameters = CoreParameters.getInstance();
coreParameters.setOverrideSpecBasePath(true);
byte[] content = getSwaggerContent(TEST_PACKAGE + "/openapi.json");
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "TestAPI");
apiDefinition.configureBasePath("https://myhost.customer.com:8767", null);
// Check if the Swagger-File has been changed
Assert.assertTrue(apiDefinition instanceof OAS3xSpecification);
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
Assert.assertEquals(swagger.get("servers").size(), 1, "Expected to get only one server url");
Assert.assertEquals("https://myhost.customer.com:8767/api/v3", swagger.get("servers").get(0).get("url").asText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,60 @@ public void overrideBackendBasePath() throws IOException {
CoreParameters.getInstance().setOverrideSpecBasePath(true);
byte[] content = getSwaggerContent(testPackage + "/petstore-only-https-scheme.json");
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "Test-API");
apiDefinition.configureBasePath("https://petstore.swagger.io/test", null);
apiDefinition.configureBasePath("http://petstore.swagger.io/test", null);

Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
Assert.assertEquals(swagger.get("host").asText(), "petstore.swagger.io");
Assert.assertEquals(swagger.get("basePath").asText(), "/test");
Assert.assertEquals(swagger.get("schemes").get(0).asText(), "http");
Assert.assertEquals(swagger.get("schemes").size(), 1);
}

@Test
public void testReplaceHostInSwaggerFalse() throws IOException{
CoreParameters.getInstance().setOverrideSpecBasePath(true);
byte[] content = getSwaggerContent(testPackage + "/petstore-only-https-scheme.json");
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "Test-API");
apiDefinition.configureBasePath("https://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(), "/test");
Assert.assertEquals(swagger.get("schemes").get(0).asText(), "https");
Assert.assertEquals(swagger.get("schemes").size(), 1);
}

@Test
public void testReplaceAlsoHostInSwagger() throws IOException{
CoreParameters.getInstance().setOverrideSpecBasePath(true);
byte[] content = getSwaggerContent(testPackage + "/petstore-only-https-scheme.json");
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "Test-API");
apiDefinition.configureBasePath("https://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(), "/test");
Assert.assertEquals(swagger.get("schemes").get(0).asText(), "https");
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);
}


}
Loading

0 comments on commit d6c3f56

Please sign in to comment.