Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/303#discussion_r64142183
  
    --- Diff: 
nifi-commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/JsonPathEvaluator.java
 ---
    @@ -0,0 +1,98 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.attribute.expression.language.evaluation.functions;
    +
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Objects;
    +
    +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
    +import 
org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
    +import 
org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator;
    +import 
org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult;
    +
    +import com.jayway.jsonpath.Configuration;
    +import com.jayway.jsonpath.DocumentContext;
    +import com.jayway.jsonpath.InvalidJsonException;
    +import com.jayway.jsonpath.JsonPath;
    +import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
    +import com.jayway.jsonpath.spi.json.JsonProvider;
    +
    +
    +public class JsonPathEvaluator extends StringEvaluator {
    +
    +    private static final StringQueryResult NULL_RESULT = new 
StringQueryResult("");
    +    private static final Configuration STRICT_PROVIDER_CONFIGURATION = 
Configuration.builder().jsonProvider(new JacksonJsonProvider()).build();
    +    private static final JsonProvider JSON_PROVIDER = 
STRICT_PROVIDER_CONFIGURATION.jsonProvider();
    +
    +    private final Evaluator<String> subject;
    +    private final Evaluator<String> jsonPathExp;
    +
    +    public JsonPathEvaluator(final Evaluator<String> subject, final 
Evaluator<String> jsonPathExp) {
    +        this.subject = subject;
    +        this.jsonPathExp = jsonPathExp;
    +    }
    +
    +    @Override
    +    public QueryResult<String> evaluate(final Map<String, String> 
attributes) {
    +        final String subjectValue = 
subject.evaluate(attributes).getValue();
    +        if (subjectValue == null || subjectValue.length() == 0) {
    +            return NULL_RESULT;
    +        }
    +        DocumentContext documentContext = null;
    +        try {
    +            documentContext = 
validateAndEstablishJsonContext(subjectValue);
    +        } catch (InvalidJsonException e) {
    +            return NULL_RESULT;
    --- End diff --
    
    Instead of returning null when the incoming JSON is not valid, perhaps we 
should wrap the InvalidJsonException in a AttributeExpressionLanguageException 
and throw that, to make the error more visible to the user. Otherwise something 
like UpdateAttribute could succeed but getting a null value for the JSON Path 
result might not show up as a problem until later in the flow, making it harder 
to debug. If the JSON itself is bad, no JSON Path will succeed, so probably 
better to alert the user immediately (bulletin, e.g.)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to