[ 
https://issues.apache.org/jira/browse/AVRO-4273?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Cédric Jäger updated AVRO-4273:
-------------------------------
    Description: 
The actual specification for 
[unions|https://avro.apache.org/docs/++version++/specification/#unions] states 
: "the type of the default value must match with *one* element of the union"

The actual implementation of the GenericData class on [line 
1338|https://github.com/apache/avro/blob/840dc8139f4b3d1bfa8e8c8f1ac3be949b440634/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java#L1338],
 however, checks only for the first element of the union :
{code:java}
    if (json.isNull() && (field.schema().getType() == Type.NULL
        || (field.schema().getType() == Type.UNION && 
field.schema().getTypes().get(0).getType() == Type.NULL))) {
      return null;
    } {code}
This causes a problem when there is a nullable field with a default value of 
null and null is not defined as the first element of the union in the schema.

Instead, the code should iterate through the elements of the union to check if 
one of them is of type NULL, and return null if it is.

  was:
The actual specification for 
[unions|https://avro.apache.org/docs/++version++/specification/#unions] states 
: "the type of the default value must match with *one* element of the union"

The actual implementation of the GenericData class on [line 
1338|https://github.com/apache/avro/blob/840dc8139f4b3d1bfa8e8c8f1ac3be949b440634/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java#L1338],
 however, checks only for the first element of the union :
{code:java}
    if (json.isNull() && (field.schema().getType() == Type.NULL
        || (field.schema().getType() == Type.UNION && 
field.schema().getTypes().get(0).getType() == Type.NULL))) {
      return null;
    } {code}
This causes a problem when there is a nullable field with a default value of 
null and null is not defined as the first element of the union in the schema :

Instead, the code should iterate through the elements of the union to check if 
one of them is of type NULL, and return null if it is.


> The type of the default value must match with one element of the union
> ----------------------------------------------------------------------
>
>                 Key: AVRO-4273
>                 URL: https://issues.apache.org/jira/browse/AVRO-4273
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.12.1
>            Reporter: Cédric Jäger
>            Priority: Minor
>             Fix For: 1.12.2
>
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> The actual specification for 
> [unions|https://avro.apache.org/docs/++version++/specification/#unions] 
> states : "the type of the default value must match with *one* element of the 
> union"
> The actual implementation of the GenericData class on [line 
> 1338|https://github.com/apache/avro/blob/840dc8139f4b3d1bfa8e8c8f1ac3be949b440634/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java#L1338],
>  however, checks only for the first element of the union :
> {code:java}
>     if (json.isNull() && (field.schema().getType() == Type.NULL
>         || (field.schema().getType() == Type.UNION && 
> field.schema().getTypes().get(0).getType() == Type.NULL))) {
>       return null;
>     } {code}
> This causes a problem when there is a nullable field with a default value of 
> null and null is not defined as the first element of the union in the schema.
> Instead, the code should iterate through the elements of the union to check 
> if one of them is of type NULL, and return null if it is.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to