Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ref types error #261

Closed
solerf opened this issue Jun 4, 2023 · 3 comments · Fixed by #262
Closed

Ref types error #261

solerf opened this issue Jun 4, 2023 · 3 comments · Fixed by #262

Comments

@solerf
Copy link

solerf commented Jun 4, 2023

I've got a case that fails validating the default when reading an avro schema with ref types and it has a default:
avro: invalid default for field X. <nil> not a <ref>

fails at isValidDefault from schema.go:1268 since Ref is not handled.

Simple test to reproduce:

package main

import (
	"fmt"
	"github.com/hamba/avro/v2"
)

type MyType struct {
	a int64  `avro:"a"`
	b string `avro:"b"`
}

type TheAvro struct {
	fieldA MyType `avro:fieldA`
	fieldB MyType `avro:fieldB`
}

func main() {
	s, err := avro.Parse(`{
				  "type": "record",
				  "name": "TheAvro",
				  "namespace": "org.testing",
				  "fields": [
					{
					  "name": "fieldA",
					  "type": {
						"type": "record",
						"name": "MyType",
						"fields": [
						  {
							"name": "a",
							"type": "int",
							"default": 0
						  },
						  {
							"name": "b",
							"type": "string",
							"default": "the_default"
						  }
						]
					  },
					  "default": {
						"a": 0,
						"b": "the_default"
					  }
					},
					{
					  "name": "fieldB",
					  "type": "MyType",
					  "default": {
						"a": 0,
						"b": "the_default"
					  }
					}
				  ]
				}
	`)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println(s) // nil
}
@nrwiersma
Copy link
Member

Fixed in v2.10.0

@solerf
Copy link
Author

solerf commented Jun 4, 2023

I was trying a fix at schema.go:595 to pass the actual when validating (appeared to work) but you were fast! appreciated!

@nrwiersma
Copy link
Member

A ref can be any named schema, so it is better to re-parse the actual rather than assume it is a Record (which it mostly is).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants