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

Incorrect location of CSV errors #483

Closed
RafeArnold opened this issue Jul 24, 2024 · 3 comments
Closed

Incorrect location of CSV errors #483

RafeArnold opened this issue Jul 24, 2024 · 3 comments
Labels
2.18 Fix or feature targeted at 2.18 release csv

Comments

@RafeArnold
Copy link

Currently, when a JsonProcessingException is thrown when parsing CSV with a missing closing quote, the reported location of the column where the error occurred is incorrect. For example,

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

import java.io.IOException;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        try (MappingIterator<List<String>> reader = new CsvMapper()
                .readerForListOf(String.class)
                .with(CsvParser.Feature.WRAP_AS_ARRAY)
                .readValues("name,dob\n\"an invalid string,2020-05-01")) {
            reader.readAll();
        } catch (JsonProcessingException e) {
            System.out.println("line number: " + e.getLocation().getLineNr());
            System.out.println("column number: " + e.getLocation().getColumnNr());
        }
    }
}

will output

line number: 2
column number: 68

The row number is correct, but the column number is not (there aren't even 68 characters in the entire CSV). I would expect the column number to be more like 30.

@RafeArnold RafeArnold changed the title Fix incorrect location of CSV errors Incorrect location of CSV errors Jul 24, 2024
@cowtowncoder
Copy link
Member

First of all, thank you for reporting this issue.

I think it should be possible to improve things: totally out of range column may be easy to fix. There are some challenges wrt fully accurate location due to way tokenization is done (it's not quite as incremental as, say, JSON decoding), but it should at least be possible to improve accuracy.

@cowtowncoder cowtowncoder added the 2.18 Fix or feature targeted at 2.18 release label Aug 22, 2024
@cowtowncoder
Copy link
Member

Looks like this was easy enough to resolve; only affects location at the end of content.

Fix goes in 2.18 for inclusion in 2.18.0.

@RafeArnold
Copy link
Author

thanks @cowtowncoder !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.18 Fix or feature targeted at 2.18 release csv
Projects
None yet
Development

No branches or pull requests

2 participants