Repository: hamba/avro
Pull request #411 (2024-06-21 04:56:55)

allow multi-line doc, and custom fields
Avro doc could have multi-line documentation. Schema is not properly JSON encoded 


Patch:
diff --git a/schema.go b/schema.go
index 6c0018f..084283c 100644
--- a/schema.go
+++ b/schema.go
@@ -377,7 +377,13 @@ func (p properties) marshalPropertiesToJSON(buf *bytes.Buffer) error {
 		if err != nil {
 			return err
 		}
-		buf.WriteString(`,"` + k + `":`)
+		kk, err := jsoniter.Marshal(k)
+		if err != nil {
+			return err
+		}
+		buf.WriteString(`,`)
+		buf.Write(kk)
+		buf.WriteString(`:`)
 		buf.Write(vv)
 	}
 	return nil
@@ -626,7 +632,12 @@ func (s *RecordSchema) MarshalJSON() ([]byte, error) {
 		buf.Write(aliasesJSON)
 	}
 	if s.doc != "" {
-		buf.WriteString(`,"doc":"` + s.doc + `"`)
+		docJSON, err := jsoniter.Marshal(s.doc)
+		if err != nil {
+			return nil, err
+		}
+		buf.WriteString(`,"doc":`)
+		buf.Write(docJSON)
 	}
 	if s.isError {
 		buf.WriteString(`,"type":"error"`)
@@ -818,7 +829,12 @@ func (f *Field) MarshalJSON() ([]byte, error) {
 		buf.Write(aliasesJSON)
 	}
 	if f.doc != "" {
-		buf.WriteString(`,"doc":"` + f.doc + `"`)
+		docJSON, err := jsoniter.Marshal(f.doc)
+		if err != nil {
+			return nil, err
+		}
+		buf.WriteString(`,"doc":`)
+		buf.Write(docJSON)
 	}
 	typeJSON, err := jsoniter.Marshal(f.typ)
 	if err != nil {
@@ -983,7 +999,12 @@ func (s *EnumSchema) MarshalJSON() ([]byte, error) {
 		buf.Write(aliasesJSON)
 	}
 	if s.doc != "" {
-		buf.WriteString(`,"doc":"` + s.doc + `"`)
+		docJSON, err := jsoniter.Marshal(s.doc)
+		if err != nil {
+			return nil, err
+		}
+		buf.WriteString(`,"doc":`)
+		buf.Write(docJSON)
 	}
 	buf.WriteString(`,"type":"enum"`)
 	symbolsJSON, err := jsoniter.Marshal(s.symbols)


Test patch:
diff --git a/schema_json_test.go b/schema_json_test.go
index cecf52d..2db4ac0 100644
--- a/schema_json_test.go
+++ b/schema_json_test.go
@@ -151,6 +151,10 @@ func TestSchema_JSON(t *testing.T) {
 			input: `{"fields":[], "type":"record", "name":"foo", "doc":"Useful info"}`,
 			json:  `{"name":"foo","doc":"Useful info","type":"record","fields":[]}`,
 		},
+		{
+			input: `{"fields":[], "type":"record", "name":"foo", "doc":"Useful info\n on multiple \n lines"}`,
+			json:  `{"name":"foo","doc":"Useful info\n on multiple \n lines","type":"record","fields":[]}`,
+		},
 		{
 			input: `{"fields":[], "type":"record", "name":"foo", "aliases":["foo","bar"]}`,
 			json:  `{"name":"foo","aliases":["foo","bar"],"type":"record","fields":[]}`,
@@ -163,6 +167,10 @@ func TestSchema_JSON(t *testing.T) {
 			input: `{"fields":[], "property-foo": "value-bar", "type":"record", "name":"foo", "doc":"foo", "aliases":["foo","bar"]}`,
 			json:  `{"name":"foo","aliases":["foo","bar"],"doc":"foo","type":"record","fields":[],"property-foo":"value-bar"}`,
 		},
+		{
+			input: `{"fields":[], "property\"foo": "value-bar", "type":"record", "name":"foo", "doc":"foo", "aliases":["foo","bar"]}`,
+			json:  `{"name":"foo","aliases":["foo","bar"],"doc":"foo","type":"record","fields":[],"property\"foo":"value-bar"}`,
+		},
 		{
 			input: `{"fields":[{"type":{"type":"boolean"}, "name":"f1"}], "type":"record", "name":"foo"}`,
 			json:  `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"}]}`,