{"action":"created","issue":{"url":"https://api.github.com/repos/mtth/avsc/issues/20","labels_url":"https://api.github.com/repos/mtth/avsc/issues/20/labels{/name}","comments_url":"https://api.github.com/repos/mtth/avsc/issues/20/comments","events_url":"https://api.github.com/repos/mtth/avsc/issues/20/events","html_url":"https://github.com/mtth/avsc/issues/20","id":120275846,"number":20,"title":"union type error","user":{"login":"asarubbi","id":16142280,"avatar_url":"https://avatars.githubusercontent.com/u/16142280?v=3","gravatar_id":"","url":"https://api.github.com/users/asarubbi","html_url":"https://github.com/asarubbi","followers_url":"https://api.github.com/users/asarubbi/followers","following_url":"https://api.github.com/users/asarubbi/following{/other_user}","gists_url":"https://api.github.com/users/asarubbi/gists{/gist_id}","starred_url":"https://api.github.com/users/asarubbi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asarubbi/subscriptions","organizations_url":"https://api.github.com/users/asarubbi/orgs","repos_url":"https://api.github.com/users/asarubbi/repos","events_url":"https://api.github.com/users/asarubbi/events{/privacy}","received_events_url":"https://api.github.com/users/asarubbi/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-12-03T21:38:35Z","updated_at":"2015-12-03T23:26:36Z","closed_at":"2015-12-03T22:06:19Z","body":"given this AVSC:\r\n\r\n```\r\n{\r\n  \"type\" : \"record\",\r\n  \"name\" : \"event_root\",\r\n  \"namespace\" : \"com.millicom.digital.events.schema\",\r\n  \"fields\" : [ {\r\n    \"name\" : \"apiproxy\",\r\n    \"type\" : {\r\n      \"type\" : \"record\",\r\n      \"name\" : \"apiproxy_type\",\r\n      \"fields\" : [ {\r\n        \"name\" : \"name\",\r\n        \"type\" : \"string\"\r\n      }, {\r\n        \"name\" : \"revision\",\r\n        \"type\" : \"string\"\r\n      } ]\r\n    }\r\n  }, {\r\n    \"name\" : \"client\",\r\n    \"type\" : {\r\n      \"type\" : \"record\",\r\n      \"name\" : \"client_type\",\r\n      \"fields\" : [ {\r\n        \"name\" : \"cn\",\r\n        \"type\" : [ \"null\", \"string\" ]\r\n      }, {\r\n        \"name\" : \"country\",\r\n        \"type\" : [ \"null\", \"string\" ]\r\n      } ]\r\n    }\r\n  } ]\r\n}\r\n```\r\nand this JSON:\r\n```\r\n{\r\n  \"schema\": \"tigo_mobile_gt_upselling_v1_Subscriber_Balances\",\r\n  \"apiproxy\": {\r\n    \"name\": \"tigo_mobile_gt_upselling_v1\",\r\n    \"revision\": \"4\"\r\n  },\r\n  \"application\": {\r\n    \"basepath\": null\r\n  },\r\n  \"client\": {\r\n    \"cn\": null,\r\n    \"country\": null\r\n  }\r\n}\r\n```\r\n\r\nand using permutations between  \r\n* tag `country` in the JSON\r\n* data type for `country` in the AVSC\r\n\r\nAVSC Type | JSON Value | Result | Status | Comment\r\n----------------|-------------------|----------|--------- | -------------\r\n\"type\" : [ \"null\", \"string\" ] | null | parsing ok | expected | \r\n\"type\" : [ \"null\", \"string\" ] | \"US\" | **parsing error** (see below) |**unexpected**|the union type is supposed to allow either null or an string value\r\n\"type\" : \"string\" | \"US\" | parsing ok | expected | \r\n\"type\" : \"string\" | null | parsing error | expected | \r\n\r\n** Parsing Error **\r\n```\r\n/home/asarubbi/Development/repo/avrotest/node_modules/avsc/lib/schemas.js:809\r\n    keys = Object.keys(val);\r\n                  ^\r\nTypeError: Object.keys called on non-object\r\n    at Function.keys (native)\r\n    at UnionType._write (/home/asarubbi/Development/repo/avrotest/node_modules/avsc/lib/schemas.js:809:19)\r\n    at RecordType.writeclient_type [as _write] (eval at <anonymous> (/home/asarubbi/Development/repo/avrotest/node_modules/avsc/lib/schemas.js:1620:10), <anonymous>:4:6)\r\n    at RecordType.writeevent_root [as _write] (eval at <anonymous> (/home/asarubbi/Development/repo/avrotest/node_modules/avsc/lib/schemas.js:1620:10), <anonymous>:4:6)\r\n    at RecordType.Type.toBuffer (/home/asarubbi/Development/repo/avrotest/node_modules/avsc/lib/schemas.js:247:8)\r\n    at Object.<anonymous> (/home/asarubbi/Development/repo/avrotest/index.js:9:18)\r\n    at Module._compile (module.js:456:26)\r\n    at Object.Module._extensions..js (module.js:474:10)\r\n    at Module.load (module.js:356:32)\r\n    at Function.Module._load (module.js:312:12)\r\n```\r\n\r\n\r\nmy code:\r\n``` javascript\r\nvar avsc = require('avsc');\r\n\r\nvar parser = avsc.parse(\"./schemas/event_root.avsc\");\r\n\r\nvar evt = require(\"./input.json\");\r\n\r\nvar buf = parser.toBuffer(evt);\r\n\r\nconsole.log(buf);\r\n\r\nvar obj = parser.fromBuffer(buf);\r\n\r\nconsole.log(obj);\r\n```"},"comment":{"url":"https://api.github.com/repos/mtth/avsc/issues/comments/161822401","html_url":"https://github.com/mtth/avsc/issues/20#issuecomment-161822401","issue_url":"https://api.github.com/repos/mtth/avsc/issues/20","id":161822401,"user":{"login":"mtth","id":1216372,"avatar_url":"https://avatars.githubusercontent.com/u/1216372?v=3","gravatar_id":"","url":"https://api.github.com/users/mtth","html_url":"https://github.com/mtth","followers_url":"https://api.github.com/users/mtth/followers","following_url":"https://api.github.com/users/mtth/following{/other_user}","gists_url":"https://api.github.com/users/mtth/gists{/gist_id}","starred_url":"https://api.github.com/users/mtth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mtth/subscriptions","organizations_url":"https://api.github.com/users/mtth/orgs","repos_url":"https://api.github.com/users/mtth/repos","events_url":"https://api.github.com/users/mtth/events{/privacy}","received_events_url":"https://api.github.com/users/mtth/received_events","type":"User","site_admin":false},"created_at":"2015-12-03T23:26:36Z","updated_at":"2015-12-03T23:26:36Z","body":"> my AVSC is VALID\r\n\r\nRight.\r\n\r\n> my JSON is VALID exactly as is.\r\n\r\nIt depends what you mean. It's valid JSON but it *isn't* a valid JSON encoding of a record according for your schema (see Avro's [JSON encoding specification](https://avro.apache.org/docs/current/spec.html#json_encoding)). For example, `{\"foo\": 2}` is valid JSON object, but isn't a valid encoding of  a record with schema `{\"type\": \"record\", \"fields\": [{\"name\": \"foo\", \"type\": \"string\"}], /* ... */}`.\r\n\r\n> the library cannot parse a perfectly valid json with a perfectly valid schema because it would slow down the parser and will break the purpose of the whole library.\r\n\r\nNo. See above.\r\n\r\nFinally, even if the data is generated out of your control, you can still conveniently convert it into valid records by using [`clone`](https://github.com/mtth/avsc/wiki/API#typecloneval-opts)'s `wrapUnions` option (example [here](http://stackoverflow.com/a/33622239/1062617)).\r\n\r\nHope this makes sense."}}