You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Ryan Skraba (Jira)" <ji...@apache.org> on 2021/09/29 18:13:00 UTC

[jira] [Comment Edited] (AVRO-3210) how the Avro Schema with Union type can accept the ‘normal JSON’

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

Ryan Skraba edited comment on AVRO-3210 at 9/29/21, 6:12 PM:
-------------------------------------------------------------

Hello! I understand your issue – this is a frequent area of confusion in Avro, but not necessarily a bug.

Unfortunately, the Avro specification wraps unions inside an object with the type it refers to like  *{{{"middle_name": {"string": "chang"}}}}* even when it's obvious to the person reading the JSON what it means, and we could unambiguously infer it from the value.

We could change the spec, or even just the Java SDK to _accept_ the short version *{{ {"middle_name": "chang"} }}*.  This would only be permitted for unambiguous JSON values; if middle_name were a union of FIXED and STRING for example (which both look like a JSON string), there's no way to tell which one to use.

Changing the spec (or even just the Java SDK) to _produce_ the short version would probably make some people depending on interoperability very unhappy.  I think this is why it hasn't been implemented yet – it would be unexpected for the round-trip of JSON->Avro->JSON to modify the JSON object.

If anybody can think of a better solution, I'd love to hear of it!


was (Author: ryanskraba):
Hello! I understand your issue – this is a frequent area of confusion in Avro, but not necessarily a bug.

Unfortunately, the Avro specification wraps unions inside an object with the type it refers to like  *{{{"middle_name": {"string": "chang"}}}}* even when it's obvious to the person reading the JSON what it means, and we could unambiguously infer it from the value.

We could change the spec, or even just the Java SDK to _accept_ the short version *{{{"middle_name":"chang"}}}*.  This would only be permitted for unambiguous JSON values; if middle_name were a union of FIXED and STRING for example (which both look like a JSON string), there's no way to tell which one to use.

Changing the spec (or even just the Java SDK) to _produce_ the short version would probably make some people depending on interoperability very unhappy.  I think this is why it hasn't been implemented yet – it would be unexpected for the round-trip of JSON->Avro->JSON to modify the JSON object.

If anybody can think of a better solution, I'd love to hear of it!

> how the Avro Schema with Union type can accept the ‘normal JSON’ 
> -----------------------------------------------------------------
>
>                 Key: AVRO-3210
>                 URL: https://issues.apache.org/jira/browse/AVRO-3210
>             Project: Apache Avro
>          Issue Type: Task
>            Reporter: Ning Chang
>            Priority: Critical
>         Attachments: test2.avsc
>
>
> how the Avro Schema with Union type can accept the ‘normal JSON’
> Avro Schema; 
>                         {
>                             "name": "middle_name",
>                             "type": [
>                                 "null",
>                                 "string"
>                             ],
>                             "default": null
>                         }
>  
> how to accept the Normal json payload like:   "middle_name": "chang" ,  not the one:  
>             "middle_name": {
>                 "string": "chang"
>             }
> Thanks In advance.
>   



--
This message was sent by Atlassian Jira
(v8.3.4#803005)