[
https://issues.apache.org/jira/browse/NIFI-4272?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16495714#comment-16495714
]
ASF GitHub Bot commented on NIFI-4272:
--------------------------------------
GitHub user ottobackwards opened a pull request:
https://github.com/apache/nifi/pull/2748
NIFI-4272 support multiple captures when el is present
From the Jira Statement:
```
I am using the ReplaceText processor to take a string input (example:
{"name":"Smith","middle":"nifi","firstname":"John"}
) and change all the filed names to all uppercase.
Using above input as an example, I expect output like
{"NAME":"Smith","MIDDLE":"nifi","FIRSTNAME":"John"}
I expect I should be able to do this with ReplaceText processor; however, I
see some unexpected behavior:
-------
Test 1: (uses EL in the replacement value property)
Search value: \"([a-z]?)\":\"(.?)\"
Replacement Value: \"${'$1':toUpper()}":\"$2\"
Result:
{"NAME":"Smith","NAME":"nifi","NAME":"John"}
-------
Test 2: (Does not use EL in the replacement Value property)
Search value: \"([a-z]?)\":\"(.?)\"
Replacement Value: \"new$1":\"$2\"
Result:
{"newname":"Smith","newmiddle":"nifi","newfirstname":"John"}
```
The issue is that the processor evaluates the expression language before
executing the regex and capture replacement. The expression replaces the
capture with the first value, and that is why the user was seeing that value
repeated.
This pr changes the Regex evaluation of the processor to evaluate the regex
first, and then run the expression on the result.
Some changes where required for escaping values.
I added tests for the reported issue and for escaping newlines etc that
would break EL even if '' in a literal.
### For all changes:
- [x] Is there a JIRA ticket associated with this PR? Is it referenced
in the commit message?
- [x] Does your PR title start with NIFI-XXXX where XXXX is the JIRA number
you are trying to resolve? Pay particular attention to the hyphen "-" character.
- [x] Has your PR been rebased against the latest commit within the target
branch (typically master)?
- [x] Is your initial contribution a single, squashed commit?
### For code changes:
- [x] Have you ensured that the full suite of tests is executed via mvn
-Pcontrib-check clean install at the root nifi folder?
- [x] Have you written or updated unit tests to verify your changes?
- [-] If adding new dependencies to the code, are these dependencies
licensed in a way that is compatible for inclusion under [ASF
2.0](http://www.apache.org/legal/resolved.html#category-a)?
- [-] If applicable, have you updated the LICENSE file, including the main
LICENSE file under nifi-assembly?
- [-] If applicable, have you updated the NOTICE file, including the main
NOTICE file found under nifi-assembly?
- [ ] If adding new Properties, have you added .displayName in addition to
.name (programmatic access) for each of the new properties?
### For documentation related changes:
- [-] Have you ensured that format looks appropriate for the output in
which it is rendered?
### Note:
Please ensure that once the PR is submitted, you check travis-ci for build
issues and submit an update to your PR as soon as possible.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/ottobackwards/nifi replace-text-el
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/nifi/pull/2748.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #2748
----
commit 75638bc201d7a492fa09cdb8358201398cebe586
Author: Otto Fowler <ottobackwards@...>
Date: 2018-05-30T20:53:55Z
NIFI-4272 support multiple captures when el is present
----
> ReplaceText processor does not properly iterate multiple replacement values
> when EL is used
> -------------------------------------------------------------------------------------------
>
> Key: NIFI-4272
> URL: https://issues.apache.org/jira/browse/NIFI-4272
> Project: Apache NiFi
> Issue Type: Bug
> Components: Core Framework
> Affects Versions: 1.1.0, 1.2.0, 1.3.0
> Reporter: Matthew Clarke
> Assignee: Otto Fowler
> Priority: Major
>
> I am using the ReplaceText processor to take a string input (example:
> {"name":"Smith","middle":"nifi","firstname":"John"} ) and change all the
> filed names to all uppercase.
> Using above input as an example, I expect output like
> {"NAME":"Smith","MIDDLE":"nifi","FIRSTNAME":"John"}
> I expect I should be able to do this with ReplaceText processor; however, I
> see some unexpected behavior:
> -------
> Test 1: (uses EL in the replacement value property)
> Search value: \"([a-z]+?)\":\"(.+?)\"
> Replacement Value: \"*${'$1':toUpper()}*":\"$2\"
> Result: {"NAME":"Smith","NAME":"nifi","NAME":"John"}
> -------
> Test 2: (Does not use EL in the replacement Value property)
> Search value: \"([a-z]+?)\":\"(.+?)\"
> Replacement Value: \"new$1":\"$2\"
> Result: {"newname":"Smith","newmiddle":"nifi","newfirstname":"John"}
> --------
> As you can see if I use a NiFi expression Language statement in the
> Replacement Value property it no longer iterates as expect through the
> various $1 captured values. It repeatedly uses the EL result from the first
> EL evaluation in every iteration while $2 correctly iterates through the
> search values.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)