Skip to content

Commit

Permalink
Fix issue 414
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Jul 17, 2023
1 parent d6c3f56 commit f15e021
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@
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 @@ -109,7 +103,7 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
if (urlJsonNode != null) {
String serverUrl = urlJsonNode.asText();
if (CoreParameters.getInstance().isOverrideSpecBasePath()) {
overrideServerSection(backendBasePath,serverUrl); // override openapi url with backendBaseapath
overrideServerSection(backendBasePath); // override openapi url with backendBaseapath
} else if (!serverUrl.startsWith("http")) { // If url does not have hostname, add hostname from backendBasepath
updateServerSection(backendBasePath, serverUrl);
}
Expand All @@ -118,8 +112,9 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
}
} else {
if (CoreParameters.getInstance().isOverrideSpecBasePath()) {
overrideServerSection(backendBasePath); // override openapi url to fix issue #412
}else {
if (backendBasePath != null)
overrideServerSection(backendBasePath); // override openapi url to fix issue #412
} else {
updateServerSection(backendBasePath, "/");
}
}
Expand All @@ -137,19 +132,11 @@ public void updateServerSection(String backendBasePath, String serverUrl) throws
((ObjectNode) openAPI).set(SERVERS, mapper.createArrayNode().add(newServer));
}

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);
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);
((ObjectNode) openAPI).set(SERVERS, mapper.createArrayNode().add(newServer));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,17 @@ 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 All @@ -131,8 +112,10 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
if (CoreParameters.getInstance().isOverrideSpecBasePath()) {
String basePath = url.getPath();
if (StringUtils.isNotEmpty(basePath)) {
LOG.info("Overriding Swagger basePath with value : {}", basePath);
LOG.debug("Overriding Swagger basePath with value : {}", basePath);
((ObjectNode) swagger).put("basePath", basePath);
}else {
LOG.debug("Not updating basePath as BackendBasepath : {} has empty basePath", backendBasePath);
}
}
this.apiSpecificationContent = this.mapper.writeValueAsBytes(swagger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void replaceServerURLIfHostNameIsPresent2() throws IOException {
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);
apiDefinition.configureBasePath("https://myhost.customer.com:8767/api/v3", null);
// Check if the Swagger-File has been changed
Assert.assertTrue(apiDefinition instanceof OAS3xSpecification);
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
Expand Down Expand Up @@ -294,7 +294,7 @@ public void overrideServerURLWithBackendBasePath2() throws IOException {
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);
apiDefinition.configureBasePath("https://myhost.customer.com:8767/api/v3", null);
// Check if the Swagger-File has been changed
Assert.assertTrue(apiDefinition instanceof OAS3xSpecification);
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void overrideBackendBasePath() throws IOException {
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").get(0).asText(), "https");
Assert.assertEquals(swagger.get("schemes").size(), 1);
}

Expand All @@ -257,7 +257,6 @@ public void testReplaceHostInSwaggerFalse() throws IOException{

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);
Expand All @@ -272,7 +271,6 @@ public void testReplaceAlsoHostInSwagger() throws IOException{

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);
Expand All @@ -289,7 +287,7 @@ public void testSwaggerWithoutHost() throws IOException{
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").get(0).asText(), "https");
Assert.assertEquals(swagger.get("schemes").size(), 1);
}

Expand Down

0 comments on commit f15e021

Please sign in to comment.