Skip to content

Commit d076aab

Browse files
authored
Fix #483: improve location tracking for CSV (#493)
1 parent 3756666 commit d076aab

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvDecoder.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,14 @@ public JsonLocation getTokenLocation()
394394
public JsonLocation getCurrentLocation()
395395
{
396396
int ptr = _inputPtr;
397-
/* One twist: when dealing with a "pending LF", need to
398-
* go back one position when calculating location
399-
*/
397+
// One twist: when dealing with a "pending LF", need to
398+
// go back one position when calculating location
400399
if (_pendingLF > 1) { // 1 is used as marker for end-of-input
401400
--ptr;
402401
}
403402
int col = ptr - _currInputRowStart + 1; // 1-based
404403
return new JsonLocation(_ioContext.contentReference(),
405-
_currInputProcessed + ptr - 1, _currInputRow, col);
404+
_currInputProcessed + ptr - 1L, _currInputRow, col);
406405
}
407406

408407
public final int getCurrentRow() {
@@ -471,7 +470,7 @@ protected void _closeInput() throws IOException
471470
_inputReader = null;
472471
}
473472
}
474-
473+
475474
protected final boolean loadMore() throws IOException
476475
{
477476
_currInputProcessed += _inputEnd;
@@ -480,13 +479,12 @@ protected final boolean loadMore() throws IOException
480479
if (_inputReader != null) {
481480
int count = _inputReader.read(_inputBuffer, 0, _inputBuffer.length);
482481
_inputEnd = count;
482+
_inputPtr = 0;
483483
if (count > 0) {
484-
_inputPtr = 0;
485484
return true;
486485
}
487-
/* End of input; close here -- but note, do NOT yet call releaseBuffers()
488-
* as there may be buffered input to handle
489-
*/
486+
// End of input; close here -- but note, do NOT yet call releaseBuffers()
487+
// as there may be buffered input to handle
490488
_closeInput();
491489
// Should never return 0, so let's fail
492490
if (count == 0) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fasterxml.jackson.dataformat.csv.deser;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.core.JacksonException;
6+
import com.fasterxml.jackson.databind.MappingIterator;
7+
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
8+
import com.fasterxml.jackson.dataformat.csv.CsvParser;
9+
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
10+
11+
public class ParserLocation483Test extends ModuleTestBase
12+
{
13+
private final CsvMapper MAPPER = mapperForCsv();
14+
15+
// [dataformats-text#483]: Location incorrect
16+
public void testAsSequence() throws Exception
17+
{
18+
try (MappingIterator<List<String>> reader = MAPPER
19+
.readerForListOf(String.class)
20+
.with(CsvParser.Feature.WRAP_AS_ARRAY)
21+
.readValues("name,dob\n\"string without end")) {
22+
reader.readAll();
23+
} catch (JacksonException e) {
24+
verifyException(e, "Missing closing quote");
25+
assertEquals(2, e.getLocation().getLineNr());
26+
// This is not always accurate but should be close:
27+
assertEquals(20, e.getLocation().getColumnNr());
28+
}
29+
}
30+
}

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Active Maintainers:
2323
(contributed by David P)
2424
#482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events
2525
(contributed by Heiko B)
26+
#483: (csv) Incorrect location of CSV errors
27+
(reported by @RafeArnold)
2628
#485: (csv) CSVDecoder: No Long and Int out of range exceptions
2729
(reported by Burdyug P)
2830

0 commit comments

Comments
 (0)