linkedin / linkedin/goavro

Can't decode nested field which parent field could be null or record

Open
#273 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
1.1k
Forks
232
PR merge metrics
No merged PRs in 30d

Description

I have next code:

```go
package main

import (
"log"

"github.com/linkedin/goavro/v2"
)

const schema = `{
"type": "record",
"name": "Payload",
"fields": [
{
"name": "image",
"default": "null",
"type": [
"null",
{
"type": "record",
"name": "Image",
"fields": [
{
"name": "url",
"type": "string"
},
{
"name": "caption",
"type": "string",
"default": "undefined"
}
]
}
]
}
]
}

`

func main() {
codec, err := goavro.NewCodec(schema)
if err != nil {
println("schema is wrong")
log.Fatalln(err)
}

str := `{"image": {"caption": "asd"}}`

a, b, err := codec.NativeFromTextual([]byte(str))

if err != nil {
println(err.Error())
} else {
println(string(b))
println(a)
}

}
```

Image is optional field: it could be null or contain some fields (url and caption).

Instead of successful parsing of JSON i receving next error message: `cannot decode textual record "SendPayload": cannot decode textual union: cannot decode textual map: cannot determine codec: "caption" for key: "image"`.

Problem is that avro schema contains `image.caption` field. I tried sending image with caption, url and both, but problem persists.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided schema and reproduction, then trace codec.NativeFromTextual while it decodes the optional image union and nested record. Confirm the behavior for image as null, with url, with caption, and with both; done means the valid JSON forms decode successfully without the reported union/map error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.