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

Jeff Maxwell updated AVRO-2131:
-------------------------------
    External issue URL: https://github.com/apache/avro/pull/278
           Description: 
Records with unions with self references fail to parse.

The example below fails to parse with {{"Type not supported: Node"}}
{code:javascript}
[
  {
    "namespace": "tree",
    "type": "record",
    "name": "Node",
    "fields": [
      {
        "name": "left",
        "type": [
          "null",
          {
            "type": "Node"
          }
        ],
        "default": null
      },
      {
        "name": "right",
        "type": [
          "null",
          {
            "type": "Node"
          }
        ],
        "default": null
      }
    ]
  }
]
 {code}
When we don't allow nullability it parses successfully:
{code:javascript}
[
  {
    "namespace": "tree",
    "type": "record",
    "name": "Node",
    "fields": [
      {
        "name": "left",
        "type": "Node"
      },
      {
        "name": "right",
        "type": "Node"
      }
    ]
  }
]
 {code}
The root cause: When the second element of the union, {{{"type":"Node"}}}, is 
parsed there is no path that can successfully handle the {{JsonNode}}.

The solution is to add this logic to the {{Schema.parse(JsonNode schema, Names 
names)}} method:
{code:java}
      } else {  //For unions with self reference
        Name nameFromType = new Name(type, names.space);
        if (names.containsKey(nameFromType)) {
          return names.get(nameFromType);
        }
        throw new SchemaParseException("Type not supported: "+type);
      }
{code}

Pull request attached

  was:
Records with unions with self references fail to parse.

The example below fails to parse with {{"Type not supported: Node"}}
{code:javascript}
[
  {
    "namespace": "tree",
    "type": "record",
    "name": "Node",
    "fields": [
      {
        "name": "left",
        "type": [
          "null",
          {
            "type": "Node"
          }
        ],
        "default": null
      },
      {
        "name": "right",
        "type": [
          "null",
          {
            "type": "Node"
          }
        ],
        "default": null
      }
    ]
  }
]
 {code}
When we don't allow nullability it parses successfully:
{code:javascript}
[
  {
    "namespace": "tree",
    "type": "record",
    "name": "Node",
    "fields": [
      {
        "name": "left",
        "type": "Node"
      },
      {
        "name": "right",
        "type": "Node"
      }
    ]
  }
]
 {code}
The root cause: When the second element of the union, {{{"type":"Node"}}}, is 
parsed there is no path that can successfully handle the {{JsonNode}}.

The solution is to add this logic to the {{Schema.parse(JsonNode schema, Names 
names)}} method:
{code:java}
      } else {  //For unions with self reference
        Name nameFromType = new Name(type, names.space);
        if (names.containsKey(nameFromType)) {
          return names.get(nameFromType);
        }
        throw new SchemaParseException("Type not supported: "+type);
      }
{code}


> Records with unions with self references fail to parse.
> -------------------------------------------------------
>
>                 Key: AVRO-2131
>                 URL: https://issues.apache.org/jira/browse/AVRO-2131
>             Project: Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.8.1
>         Environment: Java 1.8.0_152-b1
>            Reporter: Jeff Maxwell
>            Priority: Major
>
> Records with unions with self references fail to parse.
> The example below fails to parse with {{"Type not supported: Node"}}
> {code:javascript}
> [
>   {
>     "namespace": "tree",
>     "type": "record",
>     "name": "Node",
>     "fields": [
>       {
>         "name": "left",
>         "type": [
>           "null",
>           {
>             "type": "Node"
>           }
>         ],
>         "default": null
>       },
>       {
>         "name": "right",
>         "type": [
>           "null",
>           {
>             "type": "Node"
>           }
>         ],
>         "default": null
>       }
>     ]
>   }
> ]
>  {code}
> When we don't allow nullability it parses successfully:
> {code:javascript}
> [
>   {
>     "namespace": "tree",
>     "type": "record",
>     "name": "Node",
>     "fields": [
>       {
>         "name": "left",
>         "type": "Node"
>       },
>       {
>         "name": "right",
>         "type": "Node"
>       }
>     ]
>   }
> ]
>  {code}
> The root cause: When the second element of the union, {{{"type":"Node"}}}, is 
> parsed there is no path that can successfully handle the {{JsonNode}}.
> The solution is to add this logic to the {{Schema.parse(JsonNode schema, 
> Names names)}} method:
> {code:java}
>       } else {  //For unions with self reference
>         Name nameFromType = new Name(type, names.space);
>         if (names.containsKey(nameFromType)) {
>           return names.get(nameFromType);
>         }
>         throw new SchemaParseException("Type not supported: "+type);
>       }
> {code}
> Pull request attached



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to