Skip to content
Permalink
Browse files

Fixed bug where model attribute params did not override matcher param…

…s. Now, as expected, the descending order of precedence for params is input attributes > model attributes > matchers.
  • Loading branch information...
davemoore- committed Apr 1, 2019
1 parent 1ad9127 commit 3e0728ba4b9dca6b87b7dd72a257eb473d0c6eec
@@ -144,13 +144,6 @@ else if (paramValue.isNull())
else
this.params().put(paramField, paramValue.asText());
}

if (value.isObject() || value.isArray())
this.params().put(name, Json.MAPPER.writeValueAsString(value));
else if (value.isNull())
this.params().put(name, "null");
else
this.params().put(name, value.asText());
break;
default:
throw new ValidationException("'matchers." + this.name + "." + name + "' is not a recognized field.");
@@ -192,11 +192,11 @@ public static boolean canQuery(Model model, String indexName, String resolverNam
*
* @param matcher The matcher object.
* @param indexFieldName The name of the index field to populate in the clause.
* @param value The value to populate in the clause.
* @param attribute The attribute object.
* @param value The value of the attribute to populate in the clause.
* @param params The values of the parameters (if any) to pass to the matcher.
* @return A "bool" clause that references the desired field and value.
*/
public static String populateMatcherClause(Matcher matcher, String indexFieldName, String value, Attribute attribute) throws ValidationException {
public static String populateMatcherClause(Matcher matcher, String indexFieldName, String value, Map<String, String> params) throws ValidationException {
String matcherClause = matcher.clause();
for (String variable : matcher.variables().keySet()) {
Pattern pattern = matcher.variables().get(variable);
@@ -211,13 +211,9 @@ public static String populateMatcherClause(Matcher matcher, String indexFieldNam
java.util.regex.Matcher m = Patterns.VARIABLE_PARAMS.matcher(variable);
if (m.find()) {
String var = m.group(1);
String paramValue;
if (attribute.params().containsKey(var))
paramValue = attribute.params().get(var);
else if (matcher.params().containsKey(var))
paramValue = matcher.params().get(var);
else
if (!params.containsKey(var))
throw new ValidationException("'matchers." + matcher.name() + "' was given no value for '{{ " + variable + " }}'");
String paramValue = params.get(var);
matcherClause = pattern.matcher(matcherClause).replaceAll(paramValue);
}
break;
@@ -248,18 +244,28 @@ else if (matcher.params().containsKey(var))
continue;

// Construct a clause for each input value for this attribute.
String matcherName = model.indices().get(indexName).fields().get(indexFieldName).matcher();
Matcher matcher = model.matchers().get(matcherName);
List<String> valueClauses = new ArrayList<>();
Attribute attribute = attributes.get(attributeName);

// Determine which values to pass to the matcher parameters.
// Order of precedence:
// - Input attribute params override model attribute params
// - Model attribute params override matcher attribute params
Map<String, String> params = new TreeMap<>();
params.putAll(matcher.params());
params.putAll(model.attributes().get(attributeName).params());
params.putAll(attributes.get(attributeName).params());

for (Value value : attribute.values()) {

// Skip value if it's blank.
if (value.serialized() == null || value.serialized().equals(""))
continue;

// Populate the {{ field }} and {{ value }} variables of the matcher template.
String matcherName = model.indices().get(indexName).fields().get(indexFieldName).matcher();
Matcher matcher = model.matchers().get(matcherName);
valueClauses.add(populateMatcherClause(matcher, indexFieldName, value.serialized(), attribute));
// Populate the {{ field }}, {{ value }}, and {{ param.* }} variables of the matcher template.
valueClauses.add(populateMatcherClause(matcher, indexFieldName, value.serialized(), params));
}
if (valueClauses.size() == 0)
continue;
@@ -19,6 +19,12 @@

private final Map<String, String> params = Collections.emptyMap();

private final StringEntity TEST_PAYLOAD_JOB_NO_SCOPE = new StringEntity("{\n" +
" \"attributes\": {\n" +
" \"attribute_a\": [ \"a_00\" ]\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);

private final StringEntity TEST_PAYLOAD_JOB_ATTRIBUTES = new StringEntity("{\n" +
" \"attributes\": {\n" +
" \"attribute_a\": [ \"a_00\" ]\n" +
@@ -292,6 +298,18 @@ private void prepareTestResources() throws IOException {
return docsActual;
}

public void testJobNoScope() throws Exception {
try {
prepareTestResources();
String endpoint = "_zentity/resolution/zentity_test_entity_a";
Response response = client.performRequest("POST", endpoint, params, TEST_PAYLOAD_JOB_NO_SCOPE);
JsonNode json = Json.MAPPER.readTree(response.getEntity().getContent());
assertEquals(json.get("hits").get("total").asInt(), 40);
} finally {
destroyTestResources();
}
}

public void testJobAttributes() throws Exception {
try {
prepareTestResources();
Oops, something went wrong.

0 comments on commit 3e0728b

Please sign in to comment.
You can’t perform that action at this time.