Skip to content

Commit

Permalink
feat: adding test case for Issue #77. Fixing an issue where an except…
Browse files Browse the repository at this point in the history
…ion was thrown when resolving the main contract.

Signed-off-by: Gerrett <[email protected]>
  • Loading branch information
CheesyBoy123 committed Jul 24, 2024
1 parent b0247f6 commit 8216533
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/main/java/io/vertx/openapi/contract/OpenAPIVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ private Future<Void> checkOutputUnit(OutputUnit ou) {

public Future<JsonObject> resolve(Vertx vertx, SchemaRepository repo, JsonObject contract) {
return vertx.executeBlocking(p -> {
JsonSchema schema = JsonSchema.of(contract);
repo.dereference(schema);
JsonObject resolved = repo.resolve(contract);
p.complete(resolved);
try {
JsonSchema schema = JsonSchema.of(contract);
repo.dereference(schema);
JsonObject resolved = repo.resolve(contract);
p.complete(resolved);
} catch(Throwable t) {
p.fail(createInvalidContract(t.getMessage(), t));
}
});
}

Expand Down
52 changes: 49 additions & 3 deletions src/test/java/io/vertx/openapi/contract/OpenAPIContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
package io.vertx.openapi.contract;

import com.google.common.collect.ImmutableMap;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.json.schema.JsonSchemaValidationException;
import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import org.checkerframework.checker.units.qual.Time;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -113,7 +115,34 @@ void testFromFailsInvalidSpec(Vertx vertx, VertxTestContext testContext) {
}

@Test
//@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
void testValidJsonSchemaProvidedAsAdditionalSpecFiles(Vertx vertx, VertxTestContext testContext) {
Path resourcePath = getRelatedTestResourcePath(OpenAPIContractTest.class).resolve("split");
JsonObject contract = loadJson(vertx, resourcePath.resolve("petstore.json"));
JsonObject invalidComponents = loadJson(vertx, resourcePath.resolve("validJsonSchemaComponents.json"));
JsonObject validComponents = loadJson(vertx, resourcePath.resolve("components.json"));

Map<String, JsonObject> additionalValidSpecFiles = ImmutableMap.of("https://example.com/petstore", validComponents);
Map<String, JsonObject> additionalInvalidSpecFiles = ImmutableMap.of("https://example.com/petstore", invalidComponents);

OpenAPIContract.from(vertx, contract.copy(), additionalValidSpecFiles)
.compose(validResp -> Future.succeededFuture(validResp.getRawContract()))
.onSuccess(validJsonRef -> {

OpenAPIContract.from(vertx, contract.copy(), additionalInvalidSpecFiles)
.onSuccess(splitResp -> {
testContext.verify(() -> {
assertThat(splitResp.getRawContract()).isEqualTo(validJsonRef);
testContext.completeNow();
});
})
.onFailure(testContext::failNow);
})
.onFailure(testContext::failNow);
}

@Test
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
void testInvalidAdditionalSpecFiles(Vertx vertx, VertxTestContext testContext) {
Path resourcePath = getRelatedTestResourcePath(OpenAPIContractTest.class).resolve("split");
JsonObject contract = loadJson(vertx, resourcePath.resolve("petstore.json"));
Expand All @@ -122,15 +151,32 @@ void testInvalidAdditionalSpecFiles(Vertx vertx, VertxTestContext testContext) {

OpenAPIContract.from(vertx, contract, additionalSpecFiles)
.onComplete(testContext.failing(t -> testContext.verify(() -> {
t.printStackTrace();
assertThat(t).isInstanceOf(OpenAPIContractException.class);
String expectedErrorMessage =
"The passed OpenAPI contract is invalid: Found issue in specification for reference: https://example" +
".com/petstore";
"The passed OpenAPI contract is invalid: Can't resolve " +
"'https://example.com/petstore#/components/schemas/Pet', only internal refs are supported.";
assertThat(t).hasMessageThat().isEqualTo(expectedErrorMessage);
testContext.completeNow();
})));
}

@Test
@Timeout(value = 2, timeUnit = TimeUnit.HOURS)
public void testAdditionalSchemaFiles(Vertx vertx, VertxTestContext testContext) {
Path resourcePath = getRelatedTestResourcePath(OpenAPIContractTest.class).resolve("additional_schema_files");
Path contractPath = resourcePath.resolve("openapi.yaml");
Path componentsPath = resourcePath.resolve("name.yaml");

Map<String, String> additionalSpecFiles = ImmutableMap.of("https://schemas/Name.yaml", componentsPath.toString());
OpenAPIContract.from(vertx, contractPath.toString(), additionalSpecFiles)
.onComplete(testContext.succeeding(c -> {
System.out.println(c.getRawContract());

testContext.completeNow();
}));
}

@Test
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
void testSplitSpec(Vertx vertx, VertxTestContext testContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"components": {
"schemas": {
"RequestBody": {
"type": "object",
"properties": {
"name": {
"$ref": "https://schemas/Name.yaml"
}
}
}
}
},
"openapi": "3.1.0",
"paths": {
"/v1/post": {
"post": {
"summary": "Some POST request",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "app:///#/components/schemas/RequestBody"
}
}
}
},
"operationId": "postBody",
"responses": {
"200": {
"description": "Success"
},
"default": {
"description": "An unexpected error occurred"
}
}
}
}
},
"info": {
"title": "My Service",
"version": "1.0.0"
},
"tags": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$schema: https://spec.openapis.org/oas/3.1/dialect/base
description: A case-insensitive string of 1-255 characters, serving as a name (unique identifier)
type: string
minLength: 1
maxLength: 255
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.1.0
info:
title: My Service
version: 1.0.0
tags: []
paths:
/v1/post:
post:
summary: Some POST request
operationId: postBody
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/RequestBody'
responses:
200:
description: Success
default:
description: An unexpected error occurred
components:
schemas:
RequestBody:
type: object
properties:
name:
description: The unique name of the object
$ref: 'https://schemas/Name.yaml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"$id": "petstore",
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"license": {
"identifier": "MIT",
"name": "MIT License"
}
},
"components": {
"schemas": {
"Pet": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"Error": {
"type": "object",
"required": [
"code",
"message"
],
"properties": {
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
}
}
}
}
}
}

0 comments on commit 8216533

Please sign in to comment.