Skip to content

Commit

Permalink
fix: Handle missing type in an SDL (sourcenetwork#3023)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves sourcenetwork#3022

## Description

Handles missing type in an SDL instead of panicing.
  • Loading branch information
AndrewSisley authored Sep 17, 2024
1 parent ea668a4 commit b477313
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
14 changes: 10 additions & 4 deletions internal/request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func fieldsFromAST(
cTypeByFieldNameByObjName map[string]map[string]client.CType,
schemaOnly bool,
) ([]client.SchemaFieldDescription, []client.CollectionFieldDescription, error) {
kind, err := astTypeToKind(field.Type)
kind, err := astTypeToKind(hostObjectName, field)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -618,8 +618,11 @@ func setCRDTType(field *ast.FieldDefinition, kind client.FieldKind) (client.CTyp
return defaultCRDTForFieldKind[kind], nil
}

func astTypeToKind(t ast.Type) (client.FieldKind, error) {
switch astTypeVal := t.(type) {
func astTypeToKind(
hostObjectName string,
field *ast.FieldDefinition,
) (client.FieldKind, error) {
switch astTypeVal := field.Type.(type) {
case *ast.List:
switch innerAstTypeVal := astTypeVal.Type.(type) {
case *ast.NonNull:
Expand Down Expand Up @@ -677,7 +680,10 @@ func astTypeToKind(t ast.Type) (client.FieldKind, error) {
return client.FieldKind_None, ErrNonNullNotSupported

default:
return client.FieldKind_None, NewErrTypeNotFound(t.String())
if field.Type == nil {
return client.FieldKind_None, NewErrFieldTypeNotSpecified(hostObjectName, field.Name.Value)
}
return client.FieldKind_None, NewErrTypeNotFound(field.Type.String())
}
}

Expand Down
10 changes: 10 additions & 0 deletions internal/request/graphql/schema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
errPolicyInvalidResourceProp string = "policy directive with invalid resource property"
errDefaultValueInvalid string = "default value type must match field type"
errDefaultValueNotAllowed string = "default value is not allowed for this field type"
errFieldTypeNotSpecified string = "field type not specified"
)

var (
Expand All @@ -58,6 +59,7 @@ var (
ErrPolicyWithUnknownArg = errors.New(errPolicyUnknownArgument)
ErrPolicyInvalidIDProp = errors.New(errPolicyInvalidIDProp)
ErrPolicyInvalidResourceProp = errors.New(errPolicyInvalidResourceProp)
ErrFieldTypeNotSpecified = errors.New(errFieldTypeNotSpecified)
)

func NewErrDuplicateField(objectName, fieldName string) error {
Expand Down Expand Up @@ -155,3 +157,11 @@ func NewErrDefaultValueNotAllowed(fieldName, fieldType string) error {
errors.NewKV("Type", fieldType),
)
}

func NewErrFieldTypeNotSpecified(objectName, fieldName string) error {
return errors.New(
errFieldTypeNotSpecified,
errors.NewKV("Object", objectName),
errors.NewKV("Field", fieldName),
)
}
34 changes: 34 additions & 0 deletions tests/integration/schema/nil_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package schema

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestSchema_WithMissingType_Errors(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type User {
name:
}
`,
ExpectedError: "field type not specified. Object: User, Field: name",
},
},
}

testUtils.ExecuteTestCase(t, test)
}

0 comments on commit b477313

Please sign in to comment.