You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Doug Cutting (JIRA)" <ji...@apache.org> on 2013/12/13 00:47:08 UTC

[jira] [Commented] (AVRO-1408) JsonDecoder fails when fields with default values are omitted

    [ https://issues.apache.org/jira/browse/AVRO-1408?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13846925#comment-13846925 ] 

Doug Cutting commented on AVRO-1408:
------------------------------------

Avro requires the schema that was used to write the data (the writer schema) when reading.  A second schema (the reader schema) may also be provided that is used to interpret the data.  When the writer schema is missing a field that the reader schema has then the default value from the reader schema is used.

You are not providing the writer schema when reading, but only the reader schema.

Here's a test that illustrates:

{code}
  @Test public void testDefault() throws Exception {
    String writerJson = 
      "{\"type\":\"record\",\"name\":\"Foo\",\"fields\":["
      +"{\"type\":\"int\",\"name\":\"x\"}]}";
    String readerJson = 
      "{\"type\":\"record\",\"name\":\"Foo\",\"fields\":["
      +"{\"type\":\"int\",\"name\":\"x\"},"
      +"{\"type\":\"int\",\"name\":\"y\",\"default\":-1}]}";
    Schema writerSchema = Schema.parse(writerJson);
    Schema readerSchema = Schema.parse(readerJson);
    DatumReader<GenericRecord> reader =
      new GenericDatumReader<GenericRecord>(writerSchema, readerSchema);

    Decoder decoder =
      DecoderFactory.get().jsonDecoder(writerSchema, "{\"x\":1}");
    GenericRecord r = reader.read(null, decoder);
    Assert.assertEquals(-1, r.get("y"));
  }
{code}

> JsonDecoder fails when fields with default values are omitted
> -------------------------------------------------------------
>
>                 Key: AVRO-1408
>                 URL: https://issues.apache.org/jira/browse/AVRO-1408
>             Project: Avro
>          Issue Type: Bug
>    Affects Versions: 1.7.4, 1.7.5
>            Reporter: Adam Cataldo
>
> Start with an IDL like
> protocol Foo {
>   record Bar {
>     int x;
>     int y = 0;
>   }
> }
> Then use JsonDecoder to deserialize:
> {"x":30}
> Bug: this fails because y is not provided.



--
This message was sent by Atlassian JIRA
(v6.1.4#6159)