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

JsonIgnoreProperties ignores value but not the resulting CSV column #77

Open
jeep87c opened this issue Mar 26, 2018 · 3 comments
Open
Labels

Comments

@jeep87c
Copy link

jeep87c commented Mar 26, 2018

Having a POJO with a sub-POJO as field, I want to ignore a specific field of this sub-POJO. JsonIgnoreProperties is useful ignoring the value but column for field remains.

Here's a full code example:

import static com.google.common.truth.Truth.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

public class FooTest {

  class Foo {
    String something = "value";
    @JsonUnwrapped(prefix = "bar.")
    @JsonIgnoreProperties({ "toBeIgnored" })
    Bar bar;

    public String getSomething() {
      return something;
    }

    public Bar getBar() {
      return bar;
    }
  }

  // In my actual scenario, I cannot edit this piece of code
  class Bar {
    String toBeIgnored;

    public String getToBeIgnored() {
      return toBeIgnored;
    }
  }

  @Test
  public void fooTest() throws IOException {
    CsvMapper mapper = new CsvMapper();
    CsvSchema schema = mapper.schemaFor(Foo.class).withHeader();
    ObjectWriter csvWriter = mapper.writer(schema);
    OutputStream outputStream = new ByteArrayOutputStream();
    SequenceWriter writer = csvWriter.writeValues(outputStream);

    Foo foo = new Foo();
    foo.bar = new Bar();
    String toBeIgnored = "should not be parsed";
    foo.bar.toBeIgnored = toBeIgnored;
    writer.write(foo);
    String result = outputStream.toString();

    assertThat(result).doesNotContain(toBeIgnored);
    assertThat(result).contains("something");
    assertThat(result).doesNotContain("bar.toBeIgnored");
  }
}

Note that this is a very simplified example. In my actual scenario, I cannot edit class Bar in anyway.

Is there a way to achieve that without having to write a wrapper class around Bar? Is it a bug?

Thank you

@cowtowncoder
Copy link
Member

Hmmh. I can't say for sure, since there are two different aspects.

First is that CSV being columnar data format has to retain some information across values (for example). So features that use dynamic filtering (like not @JsonInclude) can be used to block value, but not column (i.e. must leave empty placeholder). So for those cases, this would be expected.

However: when ignoral is static (that is, based only on type), the whole property should be removed, and as a result, column too.
This is more what we have here.

So it seems to me that behavior is not expected. Problem may be implementation, as ignoral is on property for unwrapped value, and thereby base type (class) definition does contain property, and it is only missing via reference through property annotated with ignorable. It is possible this case is not currently supported.

For now I will consider this a bug that we hope to address. Eventually.

@luismospinam
Copy link

@cowtowncoder Hello, is there any solution to this problem yet? I'm having the same issue when trying to ignore a column in a CSV when using @JsonUnwrapped

@cowtowncoder
Copy link
Member

@luismospinam @JsonUnwrapped and ignorals do not work very well together in general unfortunately, even in JSON. As to solution, no, I update issues with comments when there is related work.

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