Skip to content

Commit

Permalink
Fix #483: improve location tracking for CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 22, 2024
1 parent 3756666 commit 4c28c14
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,14 @@ public JsonLocation getTokenLocation()
public JsonLocation getCurrentLocation()
{
int ptr = _inputPtr;
/* One twist: when dealing with a "pending LF", need to
* go back one position when calculating location
*/
// One twist: when dealing with a "pending LF", need to
// go back one position when calculating location
if (_pendingLF > 1) { // 1 is used as marker for end-of-input
--ptr;
}
int col = ptr - _currInputRowStart + 1; // 1-based
return new JsonLocation(_ioContext.contentReference(),
_currInputProcessed + ptr - 1, _currInputRow, col);
_currInputProcessed + ptr - 1L, _currInputRow, col);
}

public final int getCurrentRow() {
Expand Down Expand Up @@ -471,7 +470,7 @@ protected void _closeInput() throws IOException
_inputReader = null;
}
}

protected final boolean loadMore() throws IOException
{
_currInputProcessed += _inputEnd;
Expand All @@ -480,13 +479,12 @@ protected final boolean loadMore() throws IOException
if (_inputReader != null) {
int count = _inputReader.read(_inputBuffer, 0, _inputBuffer.length);
_inputEnd = count;
_inputPtr = 0;
if (count > 0) {
_inputPtr = 0;
return true;
}
/* End of input; close here -- but note, do NOT yet call releaseBuffers()
* as there may be buffered input to handle
*/
// End of input; close here -- but note, do NOT yet call releaseBuffers()
// as there may be buffered input to handle
_closeInput();
// Should never return 0, so let's fail
if (count == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.fasterxml.jackson.dataformat.csv.deser;

import java.util.*;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvParser;
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;

public class ParserLocation483Test extends ModuleTestBase
{
private final CsvMapper MAPPER = mapperForCsv();

// [dataformats-text#483]: Location incorrect
public void testAsSequence() throws Exception
{
try (MappingIterator<List<String>> reader = MAPPER
.readerForListOf(String.class)
.with(CsvParser.Feature.WRAP_AS_ARRAY)
.readValues("name,dob\n\"string without end")) {
reader.readAll();
} catch (JacksonException e) {
verifyException(e, "Missing closing quote");
assertEquals(2, e.getLocation().getLineNr());
// This is not always accurate but should be close:
assertEquals(20, e.getLocation().getColumnNr());
}
}
}
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Active Maintainers:
(contributed by David P)
#482: (yaml) Allow passing `ParserImpl` by a subclass or overwrite the events
(contributed by Heiko B)
#483: (csv) Incorrect location of CSV errors
(reported by @RafeArnold)
#485: (csv) CSVDecoder: No Long and Int out of range exceptions
(reported by Burdyug P)

Expand Down

0 comments on commit 4c28c14

Please sign in to comment.