Skip to content

Commit

Permalink
Fixes #113 - Support parameterization of handling nullable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ddobrin committed Sep 8, 2019
1 parent 118ae1b commit 30529ae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ private Status validateRequestBody(Object requestBody, final OpenApiOperation op
}
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setTypeLoose(false);
config.setHandleNullableField(ValidatorHandler.config.isHandleNullableField());

return schemaValidator.validate(requestBody, Overlay.toJson((SchemaImpl)specBody.getContentMediaType("application/json").getSchema()), config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public Status validateResponseContent(Object responseContent, OpenApiOperation o
return new Status(VALIDATOR_RESPONSE_CONTENT_UNEXPECTED, openApiOperation.getMethod(), openApiOperation.getPathString().original());
}
config.setTypeLoose(false);
config.setHandleNullableField(ValidatorHandler.config.isHandleNullableField());
return schemaValidator.validate(responseContent, schema, config);
}

Expand Down Expand Up @@ -257,6 +258,7 @@ private Status validateHeader(HttpServerExchange exchange, String headerName, He
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
//header won't tell if it's a real string or not. needs trying to convert.
config.setTypeLoose(true);
config.setHandleNullableField(ValidatorHandler.config.isHandleNullableField());
if ((headerValues == null || headerValues.isEmpty())) {
if(Boolean.TRUE.equals(operationHeader.getRequired())) {
return new Status(REQUIRED_RESPONSE_HEADER_MISSING, headerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ValidatorConfig {
boolean logError;
boolean skipBodyValidation = false;
boolean validateResponse;
boolean handleNullableField = true;

public ValidatorConfig() {
}
Expand Down Expand Up @@ -56,4 +57,12 @@ public boolean isValidateResponse() {
public void setValidateResponse(boolean validateResponse) {
this.validateResponse = validateResponse;
}

public boolean isHandleNullableField() {
return handleNullableField;
}

public void setHandleNullableField(boolean handleNullableField) {
this.handleNullableField = handleNullableField;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ logError: true
# Skip body validation set to true if used in light-router, light-proxy and light-spring-boot.
skipBodyValidation: false
# Enable response validation.
validateResponse: false
validateResponse: false
# When a field is set as nullable in the OpenAPI specification, the schema validator validates that it is nullable
# however continues with validation against the nullable field

# If handleNullableField is set to true && incoming field is nullable && value is field: null --> succeed
# If handleNullableField is set to false && incoming field is nullable && value is field: null --> it is up to the type
# validator using the SchemaValidator to handle it.
handleNullableField: true
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ enabled: true
logError: true
# Skip body validation set to true if used in light-router, light-proxy and light-spring-boot.
skipBodyValidation: false
validateResponse: true
validateResponse: true

# When a field is set as nullable in the OpenAPI specification, the schema validator validates that it is nullable
# however continues with validation against the nullable field

# If handleNullableField is set to true && incoming field is nullable && value is field: null --> succeed
# If handleNullableField is set to false && incoming field is nullable && value is field: null --> it is up to the type
# validator using the SchemaValidator to handle it.
handleNullableField: true

0 comments on commit 30529ae

Please sign in to comment.