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

CsvMapper does not respect CsvSchema.ColumnType when using @JsonAnySetter #60

Open
curtw opened this issue Dec 1, 2017 · 2 comments
Open
Labels

Comments

@curtw
Copy link

curtw commented Dec 1, 2017

Variation of the AnySetterTest.java test...
Following code fails at assertEquals(true, entry.stuff.get("extra")) because value in stuff is a string, not a boolean. Expected that setting CsvSchema.ColumnType.BOOLEAN on the schema would cause mapper to pass a boolean to @JsonAnySetter method:

public static void testManualSchemaWithAnySetter() throws Exception
{
    CsvMapper mapper = new CsvMapper();
    CsvSchema.Builder schemaBuilder = CsvSchema.builder()
        .addColumn("name", CsvSchema.ColumnType.STRING)
        .addColumn("age", CsvSchema.ColumnType.NUMBER)
        .addColumn("gender", CsvSchema.ColumnType.STRING)
        .addColumn("extra", CsvSchema.ColumnType.BOOLEAN);

    CsvSchema schema = schemaBuilder.build().withHeader();
    Entry entry = mapper.readerFor(Entry.class).with(schema).readValue(
            "name,age,gender,extra\nBarbara,35,F,true\n");
    assertEquals(35, entry.age);
    assertEquals("F", entry.stuff.get("gender"));
    assertEquals(true, entry.stuff.get("extra"));
    assertEquals(2, entry.stuff.size());
}

Unchanged from original test:

static class Entry {
    Map<String,Object> stuff = new LinkedHashMap<String,Object>();

    public int age;
    public String name;

    @JsonAnySetter
    public void set(String key, Object value) {
        // for secondary test, where name remains empty:
        if (key.isEmpty()) {
            key = String.valueOf(stuff.size());
        }
        stuff.put(key, value);
    }
}
@cowtowncoder
Copy link
Member

At this point schema type is not used much for anything, but I agree it should.
Any-setter is one example; another would be JsonNode (tree) which also should use coerced type.

@rjhalvorson
Copy link

+1 - running into both issues

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

3 participants