Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Extra column delimiter added when column is escaped and follows empty column #38

Closed
Magrath opened this issue May 2, 2014 · 3 comments
Milestone

Comments

@Magrath
Copy link
Contributor

Magrath commented May 2, 2014

See below unit test:-

package com.fasterxml.jackson.dataformat.csv.failing;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;
import com.google.common.collect.ImmutableMap;

public class TestWriterWithSomeMoreMissingValues extends ModuleTestBase {

    public void testWithAStringAndAUuid() throws JsonProcessingException {
        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string1", "hello");
        builder.put("string2", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals("hello,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithTwoStringsAndAUuid() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string1", "hello");
        builder.put("string2", "world");
        builder.put("string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals("hello,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithANullAStringAndAUuid() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string2", "world");
        builder.put("string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals(",world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithAStringANullAndAUuid() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string1", "hello");
        builder.put("string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals("hello,,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithThreeStringsAndAUuid() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .addColumn("string4", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string1", "hello");
        builder.put("string2", "dear");
        builder.put("string3", "world");
        builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals("hello,dear,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithANullAStringAStringAndAUuid() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .addColumn("string4", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string2", "hello");
        builder.put("string3", "world");
        builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals(",hello,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithAStringANullAStringAndAUuid() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .addColumn("string4", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string1", "hello");
        builder.put("string3", "world");
        builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals("hello,,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithTwoStringsANullAndAUuid() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .addColumn("string4", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string1", "hello");
        builder.put("string2", "world");
        builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals("hello,world,,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
    }

    public void testWithTwoStringsANullAndAString() throws JsonProcessingException {

        final CsvSchema schema = new CsvSchema.Builder()
                .addColumn("string1", CsvSchema.ColumnType.STRING)
                .addColumn("string2", CsvSchema.ColumnType.STRING)
                .addColumn("string3", CsvSchema.ColumnType.STRING)
                .addColumn("string4", CsvSchema.ColumnType.STRING)
                .build();
        final ObjectWriter writer = new CsvMapper().writer().withSchema(schema);

        final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
        builder.put("string1", "hello");
        builder.put("string2", "world");
        builder.put("string4", "again");

        final String csv = writer.writeValueAsString(builder.build());

        assertEquals("hello,world,,again\n", csv);
    }

}

All tests except testWithAStringANullAStringAndAUuid pass.

testWithAStringANullAStringAndAUuid expects hello,,world,"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3" but gets hello,,world,,"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3".

Magrath pushed a commit to Magrath/jackson-dataformat-csv that referenced this issue May 2, 2014
@Magrath
Copy link
Contributor Author

Magrath commented May 2, 2014

This issue is only reproducible in 2.3.3 and later versions. It is not reproducible in 2.3.2.

@cowtowncoder
Copy link
Member

Sounds like it might be regression due to fix for #33 (of 2 fixes in 2.3.3 that seems relevant, other not so).

cowtowncoder added a commit that referenced this issue May 2, 2014
cowtowncoder added a commit that referenced this issue May 3, 2014
@cowtowncoder cowtowncoder added this to the 2.3.4 milestone May 3, 2014
@Magrath
Copy link
Contributor Author

Magrath commented May 3, 2014

That was quick! Thanks :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants