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

[protobuf] fix calling _skipUnknownValue() twice #106

Merged
merged 1 commit into from
Aug 25, 2017
Merged

[protobuf] fix calling _skipUnknownValue() twice #106

merged 1 commit into from
Aug 25, 2017

Conversation

marsqing
Copy link
Contributor

When continue, will _skipUnknownValue(wireType) again. Is this the right way to fix?

@cowtowncoder
Copy link
Member

Would be great to have a unit test to reproduce the problem... I'll need to see if I can do that.

@cowtowncoder
Copy link
Member

And yes, just looking at code I think you are right, that should be removed.

@marsqing
Copy link
Contributor Author

Reproduction unit test. Two unknown fields are required to reproduce.

import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.dataformat.protobuf.ProtobufMapper;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
import com.fasterxml.jackson.dataformat.protobuf.schemagen.ProtobufSchemaGenerator;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.junit.Test;

public class PBSkipFieldTest {

  @Test
  public void test() throws Exception {
    ProtobufMapper mapper = new ProtobufMapper();
    mapper.configure(JsonParser.Feature.IGNORE_UNDEFINED, true);

    ThreeField threeField = new ThreeField();
    threeField.setF1(1);
    threeField.setF2(2);
    threeField.setF3(3);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectWriter writer = mapper.writerFor(ThreeField.class).with(schemaForClass(ThreeField.class, mapper));
    writer.writeValue(out, threeField);

    ObjectReader reader = mapper.readerFor(OneField.class).with(schemaForClass(OneField.class, mapper));
    OneField oneField = reader.readValue(new ByteArrayInputStream(out.toByteArray()));

    assertEquals(threeField.getF3(), oneField.getF3());
  }

  private ProtobufSchema schemaForClass(Class<?> clazz, ProtobufMapper mapper) throws Exception {
    ProtobufSchemaGenerator gen = new ProtobufSchemaGenerator();
    mapper.acceptJsonFormatVisitor(clazz, gen);
    return gen.getGeneratedSchema();
  }


  public static class OneField {

    @JsonProperty(value = "f3", index = 3)
    private int f3;

    public int getF3() {
      return f3;
    }

    public void setF3(int f3) {
      this.f3 = f3;
    }
  }

  public static class ThreeField {

    @JsonProperty(value = "f1", index = 1)
    private int f1;

    @JsonProperty(value = "f2", index = 2)
    private int f2;

    @JsonProperty(value = "f3", index = 3)
    private int f3;

    public int getF1() {
      return f1;
    }

    public void setF1(int f1) {
      this.f1 = f1;
    }

    public int getF2() {
      return f2;
    }

    public void setF2(int f2) {
      this.f2 = f2;
    }

    public int getF3() {
      return f3;
    }

    public void setF3(int f3) {
      this.f3 = f3;
    }
  }

}

@cowtowncoder
Copy link
Member

Excellent, thank you! I was about to write it so this saves me some time.

@cowtowncoder cowtowncoder merged commit b8c02d0 into FasterXML:master Aug 25, 2017
@cowtowncoder cowtowncoder added this to the 2.8.11 milestone Aug 25, 2017
@cowtowncoder cowtowncoder changed the title fix calling _skipUnknownValue() twice [protobuf] fix calling _skipUnknownValue() twice Aug 25, 2017
cowtowncoder added a commit that referenced this pull request Aug 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants