Skip to content

Commit

Permalink
- Graphql support in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Nov 21, 2023
1 parent ace24e5 commit 14d8480
Show file tree
Hide file tree
Showing 20 changed files with 1,293 additions and 49 deletions.
4 changes: 4 additions & 0 deletions modules/apim-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
<groupId>dev.failsafe</groupId>
<artifactId>failsafe</artifactId>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class APIManagerAPIAdapter {
ObjectMapper mapper = new ObjectMapper();
private final CoreParameters cmd;

private final List<String> queryStringPassThroughBreakingVersion = Arrays.asList("7.7.20220830", "7.7.20220530", "7.7.20220830", "7.7.20221130", "7.7.20230228");
private final List<String> queryStringPassThroughBreakingVersion = Arrays.asList("7.7.20220530", "7.7.20220830", "7.7.20221130", "7.7.20230228", "7.7.20230530", "7.7.20230830", "7.7.20231130");

/**
* Maps the provided status to the REST-API endpoint to change the status!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class OrganizationDeserializer extends StdDeserializer<Organization> {

private static final long serialVersionUID = 1L;

public OrganizationDeserializer() {
this(null);
}

public OrganizationDeserializer(Class<Organization> organization) {
super(organization);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public abstract class APISpecification {


public enum APISpecType {
SWAGGER_API_1x("Swagger 1.x", JSON),
SWAGGER_API_1x_YAML("Swagger 1.x (YAML)", YAML),
SWAGGER_API_1X("Swagger 1.x", JSON),
SWAGGER_API_1X_YAML("Swagger 1.x (YAML)", YAML),
SWAGGER_API_20("Swagger 2.0", JSON),
SWAGGER_API_20_YAML("Swagger 2.0 (YAML)", YAML),
OPEN_API_30("Open API 3.0", JSON),
Expand All @@ -35,6 +35,7 @@ public enum APISpecType {
"Please note: You need to use the OData-Routing policy for this API. See: https://github.com/Axway-API-Management-Plus/odata-routing-policy"),
ODATA_V3("OData V4", METADATA),
ODATA_V4("OData V4", METADATA),
GRAPHQL("Graphql", "graphql"),
UNKNOWN("Unknown", ".txt");

final String niceName;
Expand Down Expand Up @@ -125,10 +126,7 @@ public int hashCode() {

public abstract APISpecType getAPIDefinitionType() throws AppException;

public boolean parse(byte[] apiSpecificationContent) throws AppException {
this.apiSpecificationContent = apiSpecificationContent;
return true;
}
public abstract boolean parse(byte[] apiSpecificationContent) throws AppException;

protected void setMapperForDataFormat() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private APISpecificationFactory() {
add(Swagger1xSpecification.class);
add(OAS3xSpecification.class);
add(WSDLSpecification.class);
add(GraphqlSpecification.class);
add(WADLSpecification.class);
add(ODataV2Specification.class);
add(ODataV3Specification.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.axway.apim.api.specification;

import com.axway.apim.api.API;
import com.axway.apim.lib.error.AppException;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.errors.SchemaProblem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;

public class GraphqlSpecification extends APISpecification {

private static final Logger LOG = LoggerFactory.getLogger(GraphqlSpecification.class);

@Override
public byte[] getApiSpecificationContent() {
return this.apiSpecificationContent;

}

@Override
public void updateBasePath(String basePath, String host) {
// Not required
}

@Override
public void configureBasePath(String backendBasePath, API api) throws AppException {
// Not required
}

@Override
public String getDescription() {
return "";
}

@Override
public APISpecType getAPIDefinitionType() throws AppException {
return APISpecType.GRAPHQL;
}

@Override
public boolean parse(byte[] apiSpecificationContent){
this.apiSpecificationContent = apiSpecificationContent;
SchemaParser schemaParser = new SchemaParser();
try {
schemaParser.parse(new ByteArrayInputStream(apiSpecificationContent));
return true;
}catch (SchemaProblem schemaProblem){
LOG.error("Unable to parse graphql", schemaProblem);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,14 @@ public void overrideServerSection(String backendBasePath) {
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
public boolean parse(byte[] apiSpecificationContent) {
try {
super.parse(apiSpecificationContent);
this.apiSpecificationContent = apiSpecificationContent;
setMapperForDataFormat();
if (this.mapper == null) return false;
openApiNode = this.mapper.readTree(apiSpecificationContent);
LOG.debug("openapi tag value : {}", openApiNode.get(OPENAPI));
return openApiNode.has(OPENAPI) && openApiNode.get(OPENAPI).asText().startsWith("3.0.");
} catch (AppException e) {
if (e.getError() == ErrorCode.UNSUPPORTED_FEATURE) {
throw e;
}
return false;
} catch (Exception e) {
if (LOG.isDebugEnabled()) {
LOG.error("No OpenAPI 3.0 specification.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public void filterAPISpecification() {
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
public boolean parse(byte[] apiSpecificationContent) {
try {
super.parse(apiSpecificationContent);
this.apiSpecificationContent = apiSpecificationContent;
edm = EntityProvider.readMetadata(new ByteArrayInputStream(apiSpecificationContent), false);
this.openAPI = new OpenAPI();
Info info = new Info();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public void updateBasePath(String basePath, String host) { // implementation ign

@Override
public APISpecType getAPIDefinitionType() throws AppException {
return APISpecType.ODATA_V4;
return APISpecType.ODATA_V3;
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
public boolean parse(byte[] apiSpecificationContent) throws AppException{
String specStart = new String(apiSpecificationContent, 0, 500).toLowerCase();
if(specStart.contains("edmx") && specStart.contains("3.0")) {
throw new AppException("Detected OData V3 specification, which is not yet supported by the APIM-CLI.\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public APISpecType getAPIDefinitionType() throws AppException {
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
public boolean parse(byte[] apiSpecificationContent) {
try {
super.parse(apiSpecificationContent);
this.apiSpecificationContent = apiSpecificationContent;
ODataClient client = ODataClientFactory.getClient();
Edm edm = client.getReader().readMetadata(new ByteArrayInputStream(apiSpecificationContent));
this.openAPI = new OpenAPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.axway.apim.api.API;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.error.ErrorCode;
import com.axway.apim.lib.utils.Utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -19,9 +18,9 @@ public class Swagger1xSpecification extends APISpecification {
@Override
public APISpecType getAPIDefinitionType() throws AppException {
if (this.mapper.getFactory() instanceof YAMLFactory) {
return APISpecType.SWAGGER_API_1x_YAML;
return APISpecType.SWAGGER_API_1X_YAML;
}
return APISpecType.SWAGGER_API_1x;
return APISpecType.SWAGGER_API_1X;
}

@Override
Expand All @@ -33,7 +32,7 @@ public byte[] getApiSpecificationContent() {
public void updateBasePath(String basePath, String host) {
try {
String url = Utils.handleOpenAPIServerUrl(host, basePath);
((ObjectNode)swagger).put("basePath", url);
((ObjectNode) swagger).put("basePath", url);
this.apiSpecificationContent = this.mapper.writeValueAsBytes(swagger);
} catch (Exception e) {
LOG.error("Cannot replace host in provided Swagger-File. Continue with given host.", e);
Expand All @@ -55,18 +54,13 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
public boolean parse(byte[] apiSpecificationContent) {
try {
super.parse(apiSpecificationContent);
this.apiSpecificationContent = apiSpecificationContent;
setMapperForDataFormat();
if (this.mapper == null) return false;
swagger = this.mapper.readTree(apiSpecificationContent);
return swagger.has("swaggerVersion") && swagger.get("swaggerVersion").asText().startsWith("1.");
} catch (AppException e) {
if (e.getError() == ErrorCode.UNSUPPORTED_FEATURE) {
throw e;
}
return false;
} catch (Exception e) {
LOG.trace("No Swagger 1.x specification.", e);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
if (StringUtils.isNotEmpty(basePath)) {
LOG.debug("Overriding Swagger basePath with value : {}", basePath);
((ObjectNode) swagger).put(BASE_PATH, basePath);
}else {
} else {
LOG.debug("Not updating basePath value in swagger 2 as BackendBasePath : {} has empty basePath", backendBasePath);
}
((ObjectNode) swagger).put("host", url.getHost() + port);
Expand All @@ -135,18 +135,13 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
public boolean parse(byte[] apiSpecificationContent) {
try {
super.parse(apiSpecificationContent);
this.apiSpecificationContent = apiSpecificationContent;
setMapperForDataFormat();
if (this.mapper == null) return false;
swagger = this.mapper.readTree(apiSpecificationContent);
return swagger.has("swagger") && swagger.get("swagger").asText().startsWith("2.");
} catch (AppException e) {
if (e.getError() == ErrorCode.UNSUPPORTED_FEATURE) {
throw e;
}
return false;
} catch (Exception e) {
LOG.trace("Could load specification as Swagger 2.0", e);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public APISpecType getAPIDefinitionType() throws AppException {
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
public boolean parse(byte[] apiSpecificationContent) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
super.parse(apiSpecificationContent);
this.apiSpecificationContent = apiSpecificationContent;
if (apiSpecificationFile.toLowerCase().endsWith(".url")) {
apiSpecificationFile = Utils.getAPIDefinitionUriFromFile(apiSpecificationFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void configureBasePath(String backendBasePath, API api) throws AppExcepti

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
super.parse(apiSpecificationContent);
this.apiSpecificationContent = apiSpecificationContent;
if (apiSpecificationFile.toLowerCase().endsWith(".url")) {
apiSpecificationFile = Utils.getAPIDefinitionUriFromFile(apiSpecificationFile);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.axway.apim.api.specification;

import com.axway.apim.adapter.APIManagerAdapter;
import com.axway.apim.lib.error.AppException;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -62,16 +61,25 @@ public void getAPISpecificationOdataV4() throws AppException {
public void getAPISpecificationSwagger12() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("swagger12.json", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.valueOf("SWAGGER_API_1x"), apiSpecification.getAPIDefinitionType());
Assert.assertEquals(APISpecification.APISpecType.SWAGGER_API_1X, apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}
@Test
public void getAPISpecificationSwagger11() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("swagger11.json", specDirPath, "petstore");
Assert.assertEquals(APISpecification.APISpecType.valueOf("SWAGGER_API_1x"), apiSpecification.getAPIDefinitionType());
Assert.assertEquals(APISpecification.APISpecType.SWAGGER_API_1X, apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test
public void getAPISpecificationGraphql() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification("starwars.graphqls", specDirPath, "starwars");
Assert.assertEquals(APISpecification.APISpecType.GRAPHQL, apiSpecification.getAPIDefinitionType());
Assert.assertNotNull(apiSpecification.getApiSpecificationContent());
}

@Test(expectedExceptions = AppException.class, expectedExceptionsMessageRegExp = "Can't handle API specification. No suitable API-Specification implementation available.")
public void getAPISpecificationUnknown() throws AppException {
String specDirPath = classLoader.getResource("com/axway/apim/adapter/spec").getFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.axway.apim.api.specification;

import org.apache.commons.io.IOUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;

public class APISpecificationGraphqlTest {

private static final String testPackage = "/com/axway/apim/adapter/spec";

@Test
public void isGraphqlSpecificationBasedOnFile() throws IOException {
byte[] content = IOUtils.toByteArray(this.getClass().getResourceAsStream(testPackage + "/starwars.graphqls"));
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, testPackage + "/starwars.graphqls", "Starwars");
// Check, if the specification has been identified as a WSDL
Assert.assertTrue(apiDefinition instanceof GraphqlSpecification);
}
}
Loading

0 comments on commit 14d8480

Please sign in to comment.