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 NPE in skip unknown nested key #108

Merged
merged 1 commit into from
Aug 31, 2017
Merged

[ProtoBuf]fix NPE in skip unknown nested key #108

merged 1 commit into from
Aug 31, 2017

Conversation

marsqing
Copy link
Contributor

Should _skipUnknownField() in _handleNestedKey() just like in _handleRootKey().
Unit test to reproduce NPE.

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 PBHandleUnknownNestedKeyTest {

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

    NestedTwoField nestedTwoField = new NestedTwoField();
    nestedTwoField.setNested1(1);
    nestedTwoField.setNested2(2);
    MoreNestedField moreNestedField = new MoreNestedField();
    moreNestedField.setF1(nestedTwoField);

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

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

    assertEquals(moreNestedField.getF1().getNested2(), lessNestedField.getF1().getNested2());
  }

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


  public static class LessNestedField {

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

    public NestedOneField getF1() {
      return f1;
    }

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

  public static class MoreNestedField {

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

    public NestedTwoField getF1() {
      return f1;
    }

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

  public static class NestedOneField {

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

    public int getNested2() {
      return nested2;
    }

    public void setNested2(int nested2) {
      this.nested2 = nested2;
    }
  }

  public static class NestedTwoField {

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

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

    public int getNested1() {
      return nested1;
    }

    public void setNested1(int nested1) {
      this.nested1 = nested1;
    }

    public int getNested2() {
      return nested2;
    }

    public void setNested2(int nested2) {
      this.nested2 = nested2;
    }
  }

}

@marsqing marsqing changed the title fix NPE in skip unknown nested key [ProtoBuf]fix NPE in skip unknown nested key Aug 31, 2017
@cowtowncoder
Copy link
Member

Thank you again! I'll try to get this in 2.8 as well (although not sure if there'll be 2.8.11, or when, but just in case).

@cowtowncoder cowtowncoder merged commit a82e734 into FasterXML:master Aug 31, 2017
cowtowncoder added a commit that referenced this pull request Aug 31, 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