Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing compilation errors due to json-schema changes. #79

Merged
merged 4 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.vertx.openapi.contract.Parameter;

import static io.vertx.openapi.validation.ValidatorErrorType.INVALID_VALUE;
import static io.vertx.openapi.validation.ValidatorErrorType.MISSING_REQUIRED_PARAMETER;

/**
* A SchemaValidationException is a special case of a {@link ValidatorException} and is thrown, if the validation of a
Expand Down Expand Up @@ -51,6 +52,25 @@ public static SchemaValidationException createInvalidValueResponseBody(OutputUni
return new SchemaValidationException(msg, INVALID_VALUE, outputUnit, cause);
}

public static SchemaValidationException createMissingValueRequestBody(OutputUnit outputUnit,
JsonSchemaValidationException cause) {
String msg = String.format("The value of the request body is missing. Reason: %s", extractReason(outputUnit));
return new SchemaValidationException(msg, MISSING_REQUIRED_PARAMETER, outputUnit, cause);
}

public static SchemaValidationException createErrorFromOutputUnitType(Parameter parameter, OutputUnit outputUnit,
JsonSchemaValidationException cause) {
switch(outputUnit.getErrorType()) {
case MISSING_VALUE:
return createMissingValueRequestBody(outputUnit, cause);
case INVALID_VALUE:
case NONE:
default:
return createInvalidValueParameter(parameter, outputUnit, cause);
}

CheesyBoy123 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Returns the related OutputUnit of the validation error.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import static io.vertx.openapi.contract.Style.LABEL;
import static io.vertx.openapi.contract.Style.MATRIX;
import static io.vertx.openapi.contract.Style.SIMPLE;
import static io.vertx.openapi.validation.SchemaValidationException.createInvalidValueParameter;
import static io.vertx.openapi.validation.SchemaValidationException.createErrorFromOutputUnitType;
import static io.vertx.openapi.validation.SchemaValidationException.createInvalidValueRequestBody;
import static io.vertx.openapi.validation.ValidatorErrorType.MISSING_REQUIRED_PARAMETER;
import static io.vertx.openapi.validation.ValidatorErrorType.UNSUPPORTED_VALUE_FORMAT;
Expand Down Expand Up @@ -132,7 +132,7 @@ public RequestParameter validateParameter(Parameter parameter, RequestParameter
result.checkValidity();
return new RequestParameterImpl(transformedValue);
} catch (JsonSchemaValidationException e) {
throw createInvalidValueParameter(parameter, result, e);
throw createErrorFromOutputUnitType(parameter, result, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.google.common.collect.ImmutableList;
import io.vertx.json.schema.JsonSchema;
import io.vertx.json.schema.JsonSchemaValidationException;
import io.vertx.json.schema.OutputErrorType;
import io.vertx.json.schema.OutputUnit;
import io.vertx.openapi.contract.Parameter;
import io.vertx.openapi.validation.SchemaValidationException;
Expand All @@ -23,6 +24,7 @@

import static com.google.common.truth.Truth.assertThat;
import static io.vertx.json.schema.common.dsl.Schemas.intSchema;
import static io.vertx.openapi.validation.ValidatorErrorType.MISSING_REQUIRED_PARAMETER;
import static io.vertx.tests.MockHelper.mockParameter;
import static io.vertx.openapi.contract.Location.PATH;
import static io.vertx.openapi.contract.Style.LABEL;
Expand All @@ -34,15 +36,19 @@ class SchemaValidationExceptionTest {
mockParameter("dummy", PATH, LABEL, false, JsonSchema.of(intSchema().toJson()));

private static final JsonSchemaValidationException DUMMY_CAUSE = new JsonSchemaValidationException("dummy",
new Exception(), "dummyLocation");
new Exception(), "dummyLocation", OutputErrorType.INVALID_VALUE);
private static final OutputUnit DUMMY_ERROR_UNIT = new OutputUnit("instanceLocation", "absoluteKeywordLocation",
"keywordLocation", "error");
"keywordLocation", "error", OutputErrorType.INVALID_VALUE);
private static final OutputUnit DUMMY_OUTPUT_UNIT = new OutputUnit("instanceLocation2", "absoluteKeywordLocation2",
"keywordLocation2", "error2");
"keywordLocation2", "error2", OutputErrorType.MISSING_VALUE);

private static final OutputUnit DUMMY_OUTPUT_UNIT_INVALID = new OutputUnit("instanceLocation2", "absoluteKeywordLocation2",
"keywordLocation2", "error2", OutputErrorType.INVALID_VALUE);

@BeforeAll
static void setup() {
DUMMY_OUTPUT_UNIT.setErrors(ImmutableList.of(DUMMY_ERROR_UNIT));
DUMMY_OUTPUT_UNIT_INVALID.setErrors(ImmutableList.of(DUMMY_ERROR_UNIT));
}

@Test
Expand Down Expand Up @@ -77,4 +83,40 @@ void testCreateInvalidValueResponseBody() {
String excpectedMsg = "The value of the response body is invalid. Reason: error at instanceLocation";
assertThat(exception).hasMessageThat().isEqualTo(excpectedMsg);
}

@Test
void testCreateMissingValueRequestBody() {
SchemaValidationException exception = SchemaValidationException.createMissingValueRequestBody(DUMMY_OUTPUT_UNIT,
DUMMY_CAUSE);

assertThat(exception.getOutputUnit()).isEqualTo(DUMMY_OUTPUT_UNIT);
assertThat(exception.getCause()).isEqualTo(DUMMY_CAUSE);
assertThat(exception.type()).isEqualTo(MISSING_REQUIRED_PARAMETER);
String excpectedMsg = "The value of the request body is missing. Reason: error at instanceLocation";
assertThat(exception).hasMessageThat().isEqualTo(excpectedMsg);
}

@Test
void testCreateErrorFromOutputUnitType() {

SchemaValidationException exception = SchemaValidationException.createErrorFromOutputUnitType(DUMMY_PARAMETER,
DUMMY_OUTPUT_UNIT, DUMMY_CAUSE);

assertThat(exception.getOutputUnit()).isEqualTo(DUMMY_OUTPUT_UNIT);
assertThat(exception.getCause()).isEqualTo(DUMMY_CAUSE);
assertThat(exception.type()).isEqualTo(MISSING_REQUIRED_PARAMETER);
String excpectedMsg = "The value of the request body is missing. Reason: error at instanceLocation";
assertThat(exception).hasMessageThat().isEqualTo(excpectedMsg);

SchemaValidationException exception_invalid = SchemaValidationException.createErrorFromOutputUnitType(DUMMY_PARAMETER,
DUMMY_OUTPUT_UNIT_INVALID, DUMMY_CAUSE);

assertThat(exception_invalid.getOutputUnit()).isEqualTo(DUMMY_OUTPUT_UNIT_INVALID);
assertThat(exception_invalid.getCause()).isEqualTo(DUMMY_CAUSE);
assertThat(exception_invalid.type()).isEqualTo(INVALID_VALUE);
excpectedMsg = "The value of path parameter dummy is invalid. Reason: error at instanceLocation";
assertThat(exception_invalid).hasMessageThat().isEqualTo(excpectedMsg);

CheesyBoy123 marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading