Skip to content

Commit

Permalink
feat: allow empty namespaces (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Sep 25, 2024
1 parent 4917ec2 commit 45e7071
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
2 changes: 1 addition & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func parseProtocol(m map[string]any, seen seenCache, cache *SchemaCache) (*Proto
return nil, fmt.Errorf("avro: error decoding protocol: %w", err)
}

if err := checkParsedName(p.Protocol, p.Namespace, hasKey(meta.Keys, "namespace")); err != nil {
if err := checkParsedName(p.Protocol); err != nil {
return nil, err
}

Expand Down
10 changes: 5 additions & 5 deletions protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func TestParseProtocol(t *testing.T) {
schema: `{"protocol":"test", "namespace": "org.hamba.avro", "doc": "docs"}`,
wantErr: assert.NoError,
},
{
name: "Empty Namespace",
schema: `{"protocol":"test", "namespace": ""}`,
wantErr: assert.NoError,
},
{
name: "Invalid Json",
schema: `{`,
Expand Down Expand Up @@ -83,11 +88,6 @@ func TestParseProtocol(t *testing.T) {
schema: `{"protocol":"test", "namespace": "org.hamba.avro+"}`,
wantErr: assert.Error,
},
{
name: "Empty Namespace",
schema: `{"protocol":"test", "namespace": ""}`,
wantErr: assert.Error,
},
{
name: "Invalid Type Schema",
schema: `{"protocol":"test", "namespace": "org.hamba.avro", "types":["test"]}`,
Expand Down
13 changes: 5 additions & 8 deletions schema_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func parseRecord(typ Type, namespace string, m map[string]any, seen seenCache, c
return nil, fmt.Errorf("avro: error decoding record: %w", err)
}

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

if err := checkParsedName(f.Name, "", false); err != nil {
if err := checkParsedName(f.Name); err != nil {
return nil, err
}

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

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

if err := checkParsedName(f.Name, f.Namespace, hasKey(meta.Keys, "namespace")); err != nil {
if err := checkParsedName(f.Name); err != nil {
return nil, err
}
if f.Namespace == "" {
Expand Down Expand Up @@ -529,13 +529,10 @@ func fullName(namespace, name string) string {
return namespace + "." + name
}

func checkParsedName(name, ns string, hasNS bool) error {
func checkParsedName(name string) error {
if name == "" {
return errors.New("avro: non-empty name key required")
}
if hasNS && ns == "" {
return errors.New("avro: namespace key must be non-empty or omitted")
}
return nil
}

Expand Down
40 changes: 20 additions & 20 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func TestRecordSchema(t *testing.T) {
schema: `{"type":"record", "name":"test", "namespace": "org.hamba.avro", "doc": "docs", "fields":[{"name": "field", "type": "int"}]}`,
wantErr: require.NoError,
},
{
name: "Empty Namespace",
schema: `{"type":"record", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
wantErr: require.NoError,
},
{
name: "Invalid Name First Char",
schema: `{"type":"record", "name":"0test", "namespace": "org.hamba.avro", "fields":[{"name": "field", "type": "int"}]}`,
Expand All @@ -216,11 +221,6 @@ func TestRecordSchema(t *testing.T) {
schema: `{"type":"record", "name":"test", "namespace": "org.hamba.avro+", "fields":[{"name": "field", "type": "int"}]}`,
wantErr: require.Error,
},
{
name: "Empty Namespace",
schema: `{"type":"record", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
wantErr: require.Error,
},
{
name: "No Fields",
schema: `{"type":"record", "name":"test", "namespace": "org.hamba.avro"}`,
Expand Down Expand Up @@ -286,6 +286,11 @@ func TestErrorRecordSchema(t *testing.T) {
wantSchema: true,
wantErr: require.NoError,
},
{
name: "Empty Namespace",
schema: `{"type":"error", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
wantErr: require.NoError,
},
{
name: "Invalid Name First Char",
schema: `{"type":"error", "name":"0test", "namespace": "org.hamba.avro", "fields":[{"name": "field", "type": "int"}]}`,
Expand All @@ -311,11 +316,6 @@ func TestErrorRecordSchema(t *testing.T) {
schema: `{"type":"error", "name":"test", "namespace": "org.hamba.avro+", "fields":[{"name": "field", "type": "int"}]}`,
wantErr: require.Error,
},
{
name: "Empty Namespace",
schema: `{"type":"error", "name":"test", "namespace": "", "fields":[{"name": "intField", "type": "int"}]}`,
wantErr: require.Error,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -640,6 +640,11 @@ func TestEnumSchema(t *testing.T) {
wantDefault: "TEST",
wantErr: require.NoError,
},
{
name: "Empty Namespace",
schema: `{"type":"enum", "name":"test", "namespace": "", "symbols":["TEST"]}`,
wantErr: require.NoError,
},
{
name: "Invalid Name",
schema: `{"type":"enum", "name":"test+", "namespace": "org.hamba.avro", "symbols":["TEST"]}`,
Expand All @@ -660,11 +665,6 @@ func TestEnumSchema(t *testing.T) {
schema: `{"type":"enum", "name":"test", "namespace": "org.hamba.avro+", "symbols":["TEST"]}`,
wantErr: require.Error,
},
{
name: "Empty Namespace",
schema: `{"type":"enum", "name":"test", "namespace": "", "symbols":["TEST"]}`,
wantErr: require.Error,
},
{
name: "No Symbols",
schema: `{"type":"enum", "name":"test", "namespace": "org.hamba.avro"}`,
Expand Down Expand Up @@ -931,6 +931,11 @@ func TestFixedSchema(t *testing.T) {
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},
wantErr: require.NoError,
},
{
name: "Empty Namespace",
schema: `{"type":"fixed", "name":"test", "namespace": "", "size": 12}`,
wantErr: require.NoError,
},
{
name: "Invalid Name",
schema: `{"type":"fixed", "name":"test+", "namespace": "org.hamba.avro", "size": 12}`,
Expand All @@ -951,11 +956,6 @@ func TestFixedSchema(t *testing.T) {
schema: `{"type":"fixed", "name":"test", "namespace": "org.hamba.avro+", "size": 12}`,
wantErr: require.Error,
},
{
name: "Empty Namespace",
schema: `{"type":"fixed", "name":"test", "namespace": "", "size": 12}`,
wantErr: require.Error,
},
{
name: "No Size",
schema: `{"type":"fixed", "name":"test", "namespace": "org.hamba.avro"}`,
Expand Down

0 comments on commit 45e7071

Please sign in to comment.