Skip to content
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

context.getSelectedHeaders() in RowProcessor processStarted() can return invalid results #416

Closed
efrench-novi opened this issue Sep 26, 2020 · 1 comment
Assignees
Labels
Milestone

Comments

@efrench-novi
Copy link

If you call context.getSelectedHeaders() and the header has not yet been extracted by the read input thread (which means extractedFieldIndexes will be null), it will force the header extraction, but return the extracted headers and not the selectedHeaders (see code below).

To work around you can call context.parsedHeaders() or context.headers() first, which will force header extraction, after which context.getSelectedHeaders() will return the correct values.

Debug output calling context.selectedHeaders() multiple times shows this (the first line is from outside of the parser prior to calling parse()).

loadTable columns: [a, c, e]

rowProcessor.processStarted()
context.extractedFieldIndexes(): null
context.selectedHeaders(): [a, b, c, d, e]
context.selectedHeaders(): [a, c, e]
context.extractedFieldIndexes(): [I@51555c54

The DefaultContext selectedHeaders() method:

    public String[] selectedHeaders() {
		int[] extractedFieldIndexes = extractedFieldIndexes();
		if (extractedFieldIndexes != null) {
			String[] extractedFields = new String[extractedFieldIndexes.length];
			String[] headers = headers();
			for (int i = 0; i < extractedFieldIndexes.length; i++) {
				extractedFields[i] = headers[extractedFieldIndexes[i]];
			}
			return extractedFields;
		}
		return headers();
	}

I can think of two simple ways to address the issue:

  1. The AbstractParser parser could call extractHeadersIfNeeded() prior to calling processStarted IF parserSettings has selectedFields.
  2. DefaultContext selectedHeaders() call could call headers() first, and then check extractedFieldIndexes() to see if not null
jbax added a commit that referenced this issue Oct 12, 2020
@jbax jbax self-assigned this Oct 12, 2020
@jbax jbax added the bug label Oct 12, 2020
@jbax jbax added this to the 2.9.1 milestone Oct 12, 2020
@jbax
Copy link
Member

jbax commented Oct 12, 2020

Thank you for reporting the issue and for giving the proposed solutions. I went with option 2 :)

I also released version 2.9.1-SNAPSHOT for you to test.

Thank you for using our parsers!

@jbax jbax closed this as completed Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants