Skip to content

Getters needed for ignored properties #476

@maroony

Description

@maroony

I'm wondering that I have to write getters for ignored properties. Here's an working example:

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

@JsonPropertyOrder({
    "text",
    "ignored",
    "number"
})
@JsonIgnoreProperties(ignoreUnknown = true)
public class Example {

  private String text;
  private Integer number;

  @JsonCreator()
  public Example(String text, Integer number) {
    this.text = text;
    this.number = number;
  }

  public String getText() {
    return text;
  }

  public Integer getNumber() {
    return number;
  }

  // jackson needed getter
  public Object getIgnored() {
    return null;
  }

  public static void main(String[] args) throws IOException {

    CsvMapper csvMapper = new CsvMapper();
    csvMapper.registerModule(new ParameterNamesModule());
    CsvSchema csvSchema = csvMapper.schemaFor(Example.class);

    String value = "foo, ignoredValue, 123";

    MappingIterator<Example> iterator = csvMapper.readerFor(Example.class)
        .with(csvSchema)
        .readValues(value);

    var nextValue = iterator.next();

    System.out.println(nextValue.getText() + ", " + nextValue.getNumber());

  }
}

If you delete the getter getIgnored(), the following exception will be thrown:

Exception in thread "main" com.fasterxml.jackson.databind.RuntimeJsonMappingException: Cannot deserialize value of type `java.lang.Integer` from String "ignoredValue": not a valid `java.lang.Integer` value
 at [Source: (StringReader); line: 1, column: 5] (through reference chain: Example["number"])
        at com.fasterxml.jackson.databind.MappingIterator._handleMappingException(MappingIterator.java:416)
        at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:201)
        at Example.main(Example.java:55)
Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.lang.Integer` from String "ignoredValue": not a valid `java.lang.Integer` value
 at [Source: (StringReader); line: 1, column: 5] (through reference chain: Example["number"])
        at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
        at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1958)
        at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:1245)
        at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseInteger(StdDeserializer.java:848)
        at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseInteger(StdDeserializer.java:827)
        at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:531)
        at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:506)
        at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:545)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:570)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:440)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1493)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
        at com.fasterxml.jackson.databind.MappingIterator.nextValue(MappingIterator.java:283)
        at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:199)
        ... 1 more

Is this expected behavior? Is there something I can do to omit writing these getters?

What's also interesting: You got a different exception when the ignored column is the last:

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

@JsonPropertyOrder({
    "text",
    "number",
    "ignored"
})
@JsonIgnoreProperties(ignoreUnknown = true)
public class Example2 {

  private String text;
  private Integer number;

  @JsonCreator()
  public Example2(String text, Integer number) {
    this.text = text;
    this.number = number;
  }

  public String getText() {
    return text;
  }

  public Integer getNumber() {
    return number;
  }

  // jackson needed getter
  public Object getIgnored() {
    return null;
  }

  public static void main(String[] args) throws IOException {

    CsvMapper csvMapper = new CsvMapper();
    csvMapper.registerModule(new ParameterNamesModule());
    CsvSchema csvSchema = csvMapper.schemaFor(Example2.class);

    String value = "foo, 123, ignoredValue";

    MappingIterator<Example2> iterator = csvMapper.readerFor(Example2.class)
        .with(csvSchema)
        .readValues(value);

    var nextValue = iterator.next();

    System.out.println(nextValue.getText() + ", " + nextValue.getNumber());
  }
}

If you now delete the getter getIgnored(), the following exception will be thrown:

Exception in thread "main" com.fasterxml.jackson.databind.RuntimeJsonMappingException: Too many entries: expected at most 2 (value #2 (12 chars) "ignoredValue")
 at [Source: (StringReader); line: 1, column: 9]
        at com.fasterxml.jackson.databind.MappingIterator._handleMappingException(MappingIterator.java:416)
        at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:201)
        at Example2.main(Example2.java:56)
Caused by: com.fasterxml.jackson.dataformat.csv.CsvReadException: Too many entries: expected at most 2 (value #2 (12 chars) "ignoredValue")
 at [Source: (StringReader); line: 1, column: 9]
        at com.fasterxml.jackson.dataformat.csv.CsvReadException.from(CsvReadException.java:25)
        at com.fasterxml.jackson.dataformat.csv.CsvParser._reportCsvMappingError(CsvParser.java:1421)
        at com.fasterxml.jackson.dataformat.csv.CsvParser._handleExtraColumn(CsvParser.java:1163)
        at com.fasterxml.jackson.dataformat.csv.CsvParser._handleNextEntry(CsvParser.java:1030)
        at com.fasterxml.jackson.dataformat.csv.CsvParser.nextToken(CsvParser.java:758)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:442)
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1493)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
        at com.fasterxml.jackson.databind.MappingIterator.nextValue(MappingIterator.java:283)
        at com.fasterxml.jackson.databind.MappingIterator.next(MappingIterator.java:199)
        ... 1 more

Jackson-Version: 2.17.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    csvhas-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions