From 87f9e436e922a977b6002bb3070f087641001782 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Wed, 27 Oct 2021 11:17:09 -0400 Subject: [PATCH] Fix nil pointer dereference when an invalid import is bound to a model (#1676) * Fixes remaining Name field in singlefile test * Fixes nill pointer dereference when an invalid import is bound to a model * Only return error if we failed to find type * Revert "Fixes remaining Name field in singlefile test" This reverts commit e43ebf7aa80f884afdb3feca90867b1eff593f01. * Undo change of log.Println -> fmt.Println Totally accidental, sorry! Co-authored-by: Giuliano Oliveira --- codegen/config/binder.go | 4 +++- codegen/field.go | 4 ++++ plugin/resolvergen/resolver_test.go | 12 ++++++++++++ .../testdata/invalid_model_path/gqlgen.yml | 6 ++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 plugin/resolvergen/testdata/invalid_model_path/gqlgen.yml diff --git a/codegen/config/binder.go b/codegen/config/binder.go index 0974e44cf91..5d08290a01b 100644 --- a/codegen/config/binder.go +++ b/codegen/config/binder.go @@ -11,6 +11,8 @@ import ( "github.com/vektah/gqlparser/v2/ast" ) +var ErrTypeNotFound = errors.New("unable to find type") + // Binder connects graphql types to golang types using static analysis type Binder struct { pkgs *code.Packages @@ -148,7 +150,7 @@ func (b *Binder) FindObject(pkgName string, typeName string) (types.Object, erro } } - return nil, fmt.Errorf("unable to find type %s\n", fullName) + return nil, fmt.Errorf("%w: %s", ErrTypeNotFound, fullName) } func (b *Binder) PointerTo(ref *TypeReference) *TypeReference { diff --git a/codegen/field.go b/codegen/field.go index 245632aac02..7311b42c00c 100644 --- a/codegen/field.go +++ b/codegen/field.go @@ -1,6 +1,7 @@ package codegen import ( + "errors" "fmt" "go/types" "log" @@ -64,6 +65,9 @@ func (b *builder) buildField(obj *Object, field *ast.FieldDefinition) (*Field, e if err = b.bindField(obj, &f); err != nil { f.IsResolver = true + if errors.Is(err, config.ErrTypeNotFound) { + return nil, err + } log.Println(err.Error()) } diff --git a/plugin/resolvergen/resolver_test.go b/plugin/resolvergen/resolver_test.go index 3c05a62db6c..218d0ff73d7 100644 --- a/plugin/resolvergen/resolver_test.go +++ b/plugin/resolvergen/resolver_test.go @@ -54,6 +54,18 @@ func TestLayoutFollowSchemaWithCustomFilename(t *testing.T) { require.Contains(t, source, "// AUserHelperFunction implementation") } +func TestLayoutInvalidModelPath(t *testing.T) { + + cfg, err := config.LoadConfig("testdata/invalid_model_path/gqlgen.yml") + require.NoError(t, err) + + require.NoError(t, cfg.Init()) + + _, err = codegen.BuildData(cfg) + require.Error(t, err) + +} + func testFollowSchemaPersistence(t *testing.T, dir string) { _ = syscall.Unlink(dir + "/out/resolver.go") diff --git a/plugin/resolvergen/testdata/invalid_model_path/gqlgen.yml b/plugin/resolvergen/testdata/invalid_model_path/gqlgen.yml new file mode 100644 index 00000000000..cc1d06a7630 --- /dev/null +++ b/plugin/resolvergen/testdata/invalid_model_path/gqlgen.yml @@ -0,0 +1,6 @@ +schema: + - "testdata/schema.graphql" + +models: + Resolver: + model: github.com/99designs/invalid/invalid/invalid/nope.Resolver