Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
fix(policy): handle default values during policy configuration valida…
Browse files Browse the repository at this point in the history
  • Loading branch information
tcompiegne authored and jhaeyaert committed Feb 22, 2021
1 parent 79b1f66 commit e63d9d7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Expand Up @@ -46,6 +46,8 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static io.gravitee.rest.api.service.validator.PolicyHelper.clearNullValues;
Expand Down Expand Up @@ -93,11 +95,23 @@ private String validatePolicyConfiguration(String policyName, String configurati
JsonNode jsonSchema = JsonLoader.fromString(schema);
ListProcessingReport report = (ListProcessingReport) jsonSchemaFactory.getValidator().validate(jsonSchema, jsonConfiguration, true);
if (!report.isSuccess()) {
boolean hasDefaultValue = false;
String msg = "";
if (report.iterator().hasNext()) {
msg = " : " + report.iterator().next().getMessage();
Pattern pattern = Pattern.compile("\\(\\[\\\"(.*?)\\\"\\]\\)");
Matcher matcher = pattern.matcher(msg);
if (matcher.find()) {
String field = matcher.group(1);
JsonNode properties = jsonSchema.get("properties");
hasDefaultValue = properties != null &&
properties.get(field) != null &&
properties.get(field).get("default") != null;
}
}
if (!hasDefaultValue) {
throw new InvalidDataException("Invalid policy configuration" + msg);
}
throw new InvalidDataException("Invalid policy configuration" + msg);
}
}

Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
Expand Down Expand Up @@ -275,4 +276,34 @@ public void shouldRejectInvalidJsonConfigurationWithCustomJavaRegexFormat() thro
throw e;
}
}

@Test
public void shouldAcceptValidJssonConfiguration_defaultValue() throws Exception {
final String JSON_SCHEMA = "{\n" +
" \"type\": \"object\",\n" +
" \"id\": \"urn:jsonschema:io:gravitee:policy:test\",\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"title\": \"Name\",\n" +
" \"type\": \"string\",\n" +
" \"default\": \"test\"\n" +
" },\n" +
" \"valid\": {\n" +
" \"title\": \"Valid\",\n" +
" \"type\": \"boolean\",\n" +
" \"default\": false\n" +
" }\n" +
" },\n" +
" \"required\": [\n" +
" \"name\"\n" +
" ]\n" +
"}";

Policy policy = new Policy();
policy.setName("my-policy");
policy.setConfiguration("{ \"valid\": true }");
when(policyManager.getSchema("my-policy")).thenReturn(JSON_SCHEMA);

policyService.validatePolicyConfiguration(policy);
}
}

0 comments on commit e63d9d7

Please sign in to comment.