[
https://issues.apache.org/jira/browse/AVRO-1872?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15355310#comment-15355310
]
Matthieu Monsch commented on AVRO-1872:
---------------------------------------
TL;DR: This isn't a bug, it'll work if you set the `wrapUnions` flag when
parsing the protocol (e.g. `parse('./Test.avpr', {wrapUnions: true})`).
JavaScript types don't map perfectly to Avro types. For example, we can
distinguish between values of type `int` and values of type `string`, but not
always between `int` and `double`. This can lead to data corruption when
encoding unions (since we might not be able to correctly infer the active
branch). For the majority of unions, this isn't a problem and using the natural
"unwrapped" representation is a good choice. However, in cases where two or
more branches are alike, this representation isn't good enough and parsing the
schema will fail rather than risk corrupting data. The simplest option is then
to use the `wrapUnions` flag to fall back to the "wrapped" representation
(similar to unions' JSON encoding), which is less convenient but always
correct. If you are curious, you can read more about this here -
https://github.com/mtth/avsc/wiki/API#class-unwrappeduniontypeattrs-opts .
> Using union in nodejs, when has several different names in the union's record
> will report the Error at this time: ambiguous unwrapped the union, but the
> same protocol in Java: no problem
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: AVRO-1872
> URL: https://issues.apache.org/jira/browse/AVRO-1872
> Project: Avro
> Issue Type: Bug
> Components: java, javascript, js
> Affects Versions: 1.8.1
> Environment: node.js
> Reporter: Chunfy.Tseng
>
> protocol eg:
> {
> "protocol": "Test",
> "namespace": "test.avro",
> "types": [
> {
> "type": "record",
> "name": "AAA",
> "fields": [
> {
> "name": "a",
> "type": "string"
> }
> ]
> },
> {
> "type": "record",
> "name": "BBB",
> "fields": [
> {
> "name": "b",
> "type": "string"
> },
> {
> "name": "a",
> "type": "AAA"
> }
> ]
> },
> {
> "type": "record",
> "name": "CCC",
> "fields": [
> {
> "name": "c",
> "type": "string"
> },
> {
> "name": "t",
> "type": "int"
> }
> ]
> },
> {
> "type": "record",
> "name": "DDD",
> "fields": [
> {
> "name": "temp",
> "type": "string"
> },
> {
> "name": "d",
> "type": [
> "null",
> "BBB",
> "CCC"
> ]
> }
> ]
> }
> ],
> "messages": {}
> }
> in java use cmd:java -jar D:\workspace-mars2\Avro\lib\avro-tools-1.8.1.jar
> compile protocol D:\new\Test.avpr D:\new is ok.
> but in node.js code:
> var avro = require("D:\\Soft\\nodejs\\node_modules\\avsc");
> var protocol = avro.parse("./Test.avpr");
> Prompt error message as below:
> D:\Soft\nodejs\node_modules\avsc\lib\types.js:918
> throw new Error(f('ambiguous unwrapped union: %j', this));
> Error: ambiguous unwrapped
> union:["null",{"name":"test.avro.BBB","type":"record","fields":[{"name":"b","type":"string","order":"ascending","aliases":[]},{"name":"a","type":{"name":"test.avro.AAA","type":"record","fields":[{"name":"a","type":"string","order":"ascending","aliases":[]}],"aliases":[]},"order":"ascending","aliases":[]}],"aliases":[]},{"name":"test.avro.CCC","type":"record","fields":[{"name":"c","type":"string","order":"ascending","aliases":[]},{"name":"t","type":"int","order":"ascending","aliases":[]}],"aliases":[]}]
> it is bug?
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)