Skip to content

Commit c36fb03

Browse files
author
Chris Wiechmann
committed
Added support for OAS 3.0
1 parent ed50ea3 commit c36fb03

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/main/java/com/axway/apim/openapi/validator/APIManagerSchemaProvider.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class APIManagerSchemaProvider {
1111

1212
public APIManagerSchemaProvider(String apiManagerUrl, String username, String password) throws Exception {
1313
super();
14+
Utils.traceMessage("Creating API-Manager OpenAPI provider for URL: "+apiManagerUrl, TraceLevel.INFO);
1415
this.apiManager = new APIManagerHttpClient(apiManagerUrl, username, password);
1516
}
1617

@@ -22,6 +23,16 @@ public String getSchema(String apiId) throws Exception {
2223
int statusCode = httpResponse.getStatusLine().getStatusCode();
2324
String response = EntityUtils.toString(httpResponse.getEntity());
2425
if(statusCode!=200) {
26+
if(statusCode==500 && response!=null && response.contains("Failed to download API")) {
27+
// Try to download an OAS 3.0 spec instead
28+
Utils.traceMessage("No SwaggerVersion 2.0 API-Specification found, trying to download OpenAPI 3.0 specification.", TraceLevel.DEBUG);
29+
httpResponse = apiManager.get("/discovery/swagger/api/id/"+apiId+"?swaggerVersion=3.0");
30+
statusCode = httpResponse.getStatusLine().getStatusCode();
31+
response = EntityUtils.toString(httpResponse.getEntity());
32+
if(statusCode==200) {
33+
return response;
34+
}
35+
}
2536
Utils.traceMessage("Error getting API-Specification from API-Manager. Received Status-Code: " +statusCode+ ", Response: '" + response + "'", TraceLevel.ERROR);
2637
throw new IllegalArgumentException("Error getting API-Specification from API-Manager");
2738
}

src/main/java/com/axway/apim/openapi/validator/OpenAPIValidator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static synchronized OpenAPIValidator getInstance(String apiId, String use
5353
if(instances4APIIDs.containsKey(apiId)) {
5454
return instances4APIIDs.get(apiId);
5555
} else {
56-
Utils.traceMessage("Creating new OpenAPI validator instance for given API-ID.", TraceLevel.INFO);
56+
Utils.traceMessage("Creating new OpenAPI validator instance for given API-ID: '"+apiId+"'", TraceLevel.INFO);
5757
OpenAPIValidator validator = new OpenAPIValidator(apiId, username, password, apiManagerUrl);
5858
instances4APIIDs.put(apiId, validator);
5959
return validator;
@@ -77,7 +77,7 @@ private OpenAPIValidator(String openAPISpec) {
7777
private OpenAPIValidator(String apiId, String username, String password, String apiManagerUrl) throws Exception {
7878
super();
7979
try {
80-
Utils.traceMessage("Creating OpenAPIValidator from for: [apiId: '"+apiId+"', username: '"+username+"', password: '*******', apiManagerUrl: '"+apiManagerUrl+"']", TraceLevel.INFO);
80+
Utils.traceMessage("Creating OpenAPIValidator for: [apiId: '"+apiId+"', username: '"+username+"', password: '*******', apiManagerUrl: '"+apiManagerUrl+"']", TraceLevel.INFO);
8181
APIManagerSchemaProvider schemaProvider = new APIManagerSchemaProvider(apiManagerUrl, username, password);
8282
String apiSpecification = schemaProvider.getSchema(apiId);
8383
this.validator = OpenApiInteractionValidator.createForInlineApiSpecification(apiSpecification).build();
@@ -98,7 +98,7 @@ public boolean isValidRequest(String payload, String verb, String path, QueryStr
9898
}
9999

100100
public boolean isValidResponse(String payload, String verb, String path, int status, HeaderSet headers) {
101-
Utils.traceMessage("Validate response: [verb: "+verb+", path: '"+path+"', status: +status+, payload: '"+Utils.getContentStart(payload, payloadLogMaxLength, true)+"']", TraceLevel.INFO);
101+
Utils.traceMessage("Validate response: [verb: "+verb+", path: '"+path+"', status: "+status+", payload: '"+Utils.getContentStart(payload, payloadLogMaxLength, true)+"']", TraceLevel.INFO);
102102
ValidationReport validationReport = validateResponse(payload, verb, path, status, headers);
103103
if (validationReport.hasErrors()) {
104104
return false;

src/test/java/com/axway/apim/openapi/validator/TestAPIManagerBasedOpenAPIValidator.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ public void getAPISpecForInvalidApiId() throws Exception {
5353
provider.getSchema("d45571e4-ec7d-444c-af09-99999999999");
5454
}
5555

56+
@SuppressWarnings("resource")
57+
@Test
58+
public void getAPISpecForValidOAS30ApiId() throws Exception {
59+
String failedToDownloadAPI = Files.readFile(this.getClass().getClassLoader().getResourceAsStream(TEST_PACKAGE + "FailedToDownloadAPI.json"));
60+
// Initially trying to download Swagger 2.0
61+
new MockServerClient("127.0.01", 1080)
62+
.when(request()
63+
.withPath("/api/portal/v1.3/discovery/swagger/api/id/d45571e4-ec7d-444c-af09-11265d75c446")
64+
.withQueryStringParameter("swaggerVersion", "2.0")
65+
)
66+
.respond(response().withStatusCode(500).withBody(new JsonBody(failedToDownloadAPI))
67+
);
68+
new MockServerClient("127.0.01", 1080)
69+
.when(request()
70+
.withPath("/api/portal/v1.3/discovery/swagger/api/id/d45571e4-ec7d-444c-af09-11265d75c446")
71+
.withQueryStringParameter("swaggerVersion", "3.0")
72+
)
73+
.respond(response().withStatusCode(200).withBody("{ This is supposed to be a swagger-file }")
74+
);
75+
76+
APIManagerSchemaProvider provider = new APIManagerSchemaProvider("https://localhost:1080", "user", "password");
77+
String apiSpec = provider.getSchema("d45571e4-ec7d-444c-af09-11265d75c446");
78+
Assert.assertEquals(apiSpec, "{ This is supposed to be a swagger-file }");
79+
}
80+
5681
@SuppressWarnings("resource")
5782
@Test
5883
public void testOpenAPIBasedOnAPIManagerApiID() throws Exception {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"errors":[{"code":500,"message":"Failed to download API"}]}

0 commit comments

Comments
 (0)