Repository: hamba/avro (go)

Issue:
`Field.MarshalJSON` doesn't correctly encode null default values in unions
Hello, first thank you for writing and supporting this library!
 
As the title says, there is an issue when json encoding a schema containing unions with a default null value. For example a schema defined like this: 

```json
{
    "type":"record",
    "namespace": "org.hamba.avro",
    "name":"X",
    "fields":[{"name":"value", "type": ["null", "int"], "default": null}]
}
```

When json marshalled, it becomes:

```json
{
    "type":"record",
    "namespace": "org.hamba.avro",
    "name":"X",
    "fields":[{"name":"value", "type": ["null", "int"], "default": {}]
}
```

Note the default value being an empty object instead of `null` data type.

I identified the root cause in how `Field.MarshalJSON` handle fields with default values and prepared a pull request with a fix and updated tests to check this particular use case. I hope it helps.

Pull request description:
fix: null default when json marshalling unions
Fixes `Field.MarshalJSON` handling of default null values. The previous implementation was relying on `omitempty` for the default field and it was not possible to differentiate between a field with no default value and one with `null`. 

Fixes #136 

Patch:
diff --git a/schema.go b/schema.go
index 978d4f5..743a69d 100644
--- a/schema.go
+++ b/schema.go
@@ -523,16 +523,23 @@ func (f *Field) String() string {
 
 // MarshalJSON marshals the schema to json.
 func (f *Field) MarshalJSON() ([]byte, error) {
-	s := struct {
-		Name    string      `json:"name"`
-		Type    Schema      `json:"type"`
-		Default interface{} `json:"default,omitempty"`
-	}{
+	type base struct {
+		Name string `json:"name"`
+		Type Schema `json:"type"`
+	}
+	type ext struct {
+		base
+		Default interface{} `json:"default"`
+	}
+	var s interface{} = base{
 		Name: f.name,
 		Type: f.typ,
 	}
 	if f.hasDef {
-		s.Default = f.def
+		s = ext{
+			base:    s.(base),
+			Default: f.Default(),
+		}
 	}
 	return jsoniter.Marshal(s)
 }


Test patch:
diff --git a/schema_json_test.go b/schema_json_test.go
index 233395f..feda9b9 100644
--- a/schema_json_test.go
+++ b/schema_json_test.go
@@ -258,6 +258,18 @@ func TestSchema_JSON(t *testing.T) {
 			}`,
 			json: `{"name":"org.hamba.avro.X","type":"record","fields":[{"name":"value","type":{"name":"org.hamba.avro.Y","type":"fixed","size":15}}]}`,
 		},
+		{
+			input: `{
+				"type":"record",
+				"namespace": "org.hamba.avro",
+				"name":"X",
+  				"fields":[
+					{"name":"union_no_def","type":["null", "int"]},
+					{"name":"union_with_def","type":["null", "string"],"default": null}
+				]
+			}`,
+			json: `{"name":"org.hamba.avro.X","type":"record","fields":[{"name":"union_no_def","type":["null","int"]},{"name":"union_with_def","type":["null","string"],"default":null}]}`,
+		},
 	}
 
 	for i, test := range tests {