New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NIFI-4416 CSVRecordReader does not accept escaped character as delimiter #2172
Conversation
|
||
@Override | ||
public ValidationResult validate(final String subject, final String input, final ValidationContext context) { | ||
final String unescaped = CSVUtils.unescape(input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently it appears that the properties that use this validator require a value, which means "input" will never be null. However the PR #2088 aims to allow for these to be undefined, which will cause a NullPointerException here. Best practice here would be to check for null input and return invalid. You might not be able to test that logic from the CSVRecordReader test, but a quick unit test for this class would suffice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow. Wasn't aware of the other PR. Thanks @mattyb149 - Will make the null checks for the cases you mentioned.
public static final Validator UNESCAPED_SINGLE_CHAR_VALIDATOR = new Validator() { | ||
@Override | ||
public ValidationResult validate(String subject, String input, ValidationContext context) { | ||
input = unescapeString(input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here for defensive code to avoid NPEs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it Matt, Made this change as well. Figured that I was mutating the argument, avoided that as well now.
} | ||
|
||
private String unescapeString(String input) { | ||
if (input.length() > 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here for defensive code to avoid NPEs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added up this as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also added testcases for the above NPE and other validation usecases.
@@ -0,0 +1,3 @@ | |||
idnamebalanceaddresscitystatezipCodecountry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All files need an Apache 2.0 License header, or if that is not feasible due to the nature/format of the file, it must be added to the Apache Rat exclusion list in the nifi-record-serialization-services POM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry. My bad. I ran an entire contrib check skipping rat checks. Added the new CSV to the exclusion list.
Did that with rat enabled for this bundle alone (for some reason rat checks fail at the parent level level - for nifi-commons).
@mattyb149 Squashed the commits with the review changes. Please let me know if I've been sloppy anywhere. |
.subject(subject) | ||
.input(unescapeString) | ||
.explanation("Only non-null single characters are supported") | ||
.valid((input.length() == 1 && input.charAt(0) != 0) || context.isExpressionLanguagePresent(input)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be checking unescapeString instead of input? If I put "\u0001" as the value of the character, unescapeString() gets the correct value, but then the property is marked invalid because the original input was 6 characters long. The SingleCharacterValidator checks the "unescaped" variable instead of the original input, for this reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattyb149 Terribly sorry about this. This is super embarrassing. Was wondering how the testcases failed me. It should have been "\\u0001" instead of "\u0001". Fixed the testcase and the class. Will run a manual test on the UI as well before pushing the changes.
ValidationResult result = validator.validate("Delimiter", "\\u0001", mockContext);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't touched the unit test, and I am having trouble pushing my branch, so please feel free to update and run your tests, and I will use this PR as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ironically I had been trying to emulate your unit test by pasting in the actual character for \u0001, but that doesn't make it through, an empty string gets passed to the validator. That's when I tried the actual string "\u0001" and how I found the issue.
I took the liberty of fixing the unescapeString issue above and verified that the PR's intended functionality is working (tried a Unicode escape character as a delimiter). I will add my commit to the merge. Thanks for the improvement! Merging to master |
@mattyb149 Tested it against the NiFi UI (after making the changes for your last review comment) and converted a bunch of records from CSV to JSON with \u0001 as the delimiter. Looks good to me. |
Yes please, let's use yours :) |
@mattyb149 Hahaha. Thanks a lot, Matt.
|
+1 LGTM, ran the unit tests and with a live NiFi, verified I could use Unicode and other escaped character(s) as a delimiter. Thanks again! This time I'm really merging to master :) |
@mattyb149 Really appreciate your time and patience, Matt. I learnt quite a lot from your comments. |
Thank you for submitting a contribution to Apache NiFi.
In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:
For all changes:
Is there a JIRA ticket associated with this PR? Is it referenced
in the commit message?
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.
Has your PR been rebased against the latest commit within the target branch (typically master)?
Is your initial contribution a single, squashed commit?
For code changes:
For documentation related changes:
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.