Skip to content

Commit 7e9fa4c

Browse files
committed
feat: allow empty namespaces
1 parent 4917ec2 commit 7e9fa4c

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

protocol.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func parseProtocol(m map[string]any, seen seenCache, cache *SchemaCache) (*Proto
252252
return nil, fmt.Errorf("avro: error decoding protocol: %w", err)
253253
}
254254

255-
if err := checkParsedName(p.Protocol, p.Namespace, hasKey(meta.Keys, "namespace")); err != nil {
255+
if err := checkParsedName(p.Protocol); err != nil {
256256
return nil, err
257257
}
258258

protocol_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func TestParseProtocol(t *testing.T) {
5353
schema: `{"protocol":"test", "namespace": "org.hamba.avro", "doc": "docs"}`,
5454
wantErr: assert.NoError,
5555
},
56+
{
57+
name: "Empty Namespace",
58+
schema: `{"protocol":"test", "namespace": ""}`,
59+
wantErr: assert.NoError,
60+
},
5661
{
5762
name: "Invalid Json",
5863
schema: `{`,
@@ -83,11 +88,6 @@ func TestParseProtocol(t *testing.T) {
8388
schema: `{"protocol":"test", "namespace": "org.hamba.avro+"}`,
8489
wantErr: assert.Error,
8590
},
86-
{
87-
name: "Empty Namespace",
88-
schema: `{"protocol":"test", "namespace": ""}`,
89-
wantErr: assert.Error,
90-
},
9191
{
9292
name: "Invalid Type Schema",
9393
schema: `{"protocol":"test", "namespace": "org.hamba.avro", "types":["test"]}`,

schema_parse.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func parseRecord(typ Type, namespace string, m map[string]any, seen seenCache, c
224224
return nil, fmt.Errorf("avro: error decoding record: %w", err)
225225
}
226226

227-
if err := checkParsedName(r.Name, r.Namespace, hasKey(meta.Keys, "namespace")); err != nil {
227+
if err := checkParsedName(r.Name); err != nil {
228228
return nil, err
229229
}
230230
if r.Namespace == "" {
@@ -294,7 +294,7 @@ func parseField(namespace string, m map[string]any, seen seenCache, cache *Schem
294294
return nil, fmt.Errorf("avro: error decoding field: %w", err)
295295
}
296296

297-
if err := checkParsedName(f.Name, "", false); err != nil {
297+
if err := checkParsedName(f.Name); err != nil {
298298
return nil, err
299299
}
300300

@@ -340,7 +340,7 @@ func parseEnum(namespace string, m map[string]any, seen seenCache, cache *Schema
340340
return nil, fmt.Errorf("avro: error decoding enum: %w", err)
341341
}
342342

343-
if err := checkParsedName(e.Name, e.Namespace, hasKey(meta.Keys, "namespace")); err != nil {
343+
if err := checkParsedName(e.Name); err != nil {
344344
return nil, err
345345
}
346346
if e.Namespace == "" {
@@ -451,7 +451,7 @@ func parseFixed(namespace string, m map[string]any, seen seenCache, cache *Schem
451451
return nil, fmt.Errorf("avro: error decoding fixed: %w", err)
452452
}
453453

454-
if err := checkParsedName(f.Name, f.Namespace, hasKey(meta.Keys, "namespace")); err != nil {
454+
if err := checkParsedName(f.Name); err != nil {
455455
return nil, err
456456
}
457457
if f.Namespace == "" {
@@ -529,13 +529,10 @@ func fullName(namespace, name string) string {
529529
return namespace + "." + name
530530
}
531531

532-
func checkParsedName(name, ns string, hasNS bool) error {
532+
func checkParsedName(name string) error {
533533
if name == "" {
534534
return errors.New("avro: non-empty name key required")
535535
}
536-
if hasNS && ns == "" {
537-
return errors.New("avro: namespace key must be non-empty or omitted")
538-
}
539536
return nil
540537
}
541538

schema_test.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ func TestRecordSchema(t *testing.T) {
191191
schema: `{"type":"record", "name":"test", "namespace": "org.hamba.avro", "doc": "docs", "fields":[{"name": "field", "type": "int"}]}`,
192192
wantErr: require.NoError,
193193
},
194+
{
195+
name: "Empty Namespace",
196+
schema: `{"type":"record", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
197+
wantErr: require.NoError,
198+
},
194199
{
195200
name: "Invalid Name First Char",
196201
schema: `{"type":"record", "name":"0test", "namespace": "org.hamba.avro", "fields":[{"name": "field", "type": "int"}]}`,
@@ -216,11 +221,6 @@ func TestRecordSchema(t *testing.T) {
216221
schema: `{"type":"record", "name":"test", "namespace": "org.hamba.avro+", "fields":[{"name": "field", "type": "int"}]}`,
217222
wantErr: require.Error,
218223
},
219-
{
220-
name: "Empty Namespace",
221-
schema: `{"type":"record", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
222-
wantErr: require.Error,
223-
},
224224
{
225225
name: "No Fields",
226226
schema: `{"type":"record", "name":"test", "namespace": "org.hamba.avro"}`,
@@ -286,6 +286,11 @@ func TestErrorRecordSchema(t *testing.T) {
286286
wantSchema: true,
287287
wantErr: require.NoError,
288288
},
289+
{
290+
name: "Empty Namespace",
291+
schema: `{"type":"error", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
292+
wantErr: require.NoError,
293+
},
289294
{
290295
name: "Invalid Name First Char",
291296
schema: `{"type":"error", "name":"0test", "namespace": "org.hamba.avro", "fields":[{"name": "field", "type": "int"}]}`,
@@ -311,11 +316,6 @@ func TestErrorRecordSchema(t *testing.T) {
311316
schema: `{"type":"error", "name":"test", "namespace": "org.hamba.avro+", "fields":[{"name": "field", "type": "int"}]}`,
312317
wantErr: require.Error,
313318
},
314-
{
315-
name: "Empty Namespace",
316-
schema: `{"type":"error", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
317-
wantErr: require.Error,
318-
},
319319
}
320320

321321
for _, test := range tests {
@@ -640,6 +640,11 @@ func TestEnumSchema(t *testing.T) {
640640
wantDefault: "TEST",
641641
wantErr: require.NoError,
642642
},
643+
{
644+
name: "Empty Namespace",
645+
schema: `{"type":"enum", "name":"test", "namespace": "", "symbols":["TEST"]}`,
646+
wantErr: require.NoError,
647+
},
643648
{
644649
name: "Invalid Name",
645650
schema: `{"type":"enum", "name":"test+", "namespace": "org.hamba.avro", "symbols":["TEST"]}`,
@@ -660,11 +665,6 @@ func TestEnumSchema(t *testing.T) {
660665
schema: `{"type":"enum", "name":"test", "namespace": "org.hamba.avro+", "symbols":["TEST"]}`,
661666
wantErr: require.Error,
662667
},
663-
{
664-
name: "Empty Namespace",
665-
schema: `{"type":"enum", "name":"test", "namespace": "", "symbols":["TEST"]}`,
666-
wantErr: require.Error,
667-
},
668668
{
669669
name: "No Symbols",
670670
schema: `{"type":"enum", "name":"test", "namespace": "org.hamba.avro"}`,
@@ -931,6 +931,11 @@ func TestFixedSchema(t *testing.T) {
931931
wantFingerprint: [32]uint8{0x8c, 0x9e, 0xcb, 0x4, 0x83, 0x2f, 0x3b, 0xa7, 0x58, 0x85, 0x9, 0x99, 0x41, 0xe, 0xbf, 0xd4, 0x7, 0xc7, 0x87, 0x4f, 0x8a, 0x12, 0xf4, 0xd0, 0x7f, 0x45, 0xdd, 0xaa, 0x10, 0x6b, 0x2f, 0xb3},
932932
wantErr: require.NoError,
933933
},
934+
{
935+
name: "Empty Namespace",
936+
schema: `{"type":"fixed", "name":"test", "namespace": "", "size": 12}`,
937+
wantErr: require.NoError,
938+
},
934939
{
935940
name: "Invalid Name",
936941
schema: `{"type":"fixed", "name":"test+", "namespace": "org.hamba.avro", "size": 12}`,
@@ -951,11 +956,6 @@ func TestFixedSchema(t *testing.T) {
951956
schema: `{"type":"fixed", "name":"test", "namespace": "org.hamba.avro+", "size": 12}`,
952957
wantErr: require.Error,
953958
},
954-
{
955-
name: "Empty Namespace",
956-
schema: `{"type":"fixed", "name":"test", "namespace": "", "size": 12}`,
957-
wantErr: require.Error,
958-
},
959959
{
960960
name: "No Size",
961961
schema: `{"type":"fixed", "name":"test", "namespace": "org.hamba.avro"}`,

0 commit comments

Comments
 (0)