From 6c769aa576699413ce95930eaaf9444d5927b502 Mon Sep 17 00:00:00 2001 From: vvakame Date: Tue, 27 Nov 2018 15:38:00 +0900 Subject: [PATCH] fix infinit loop in OverlappingFieldsCanBeMerged rule --- validator/imported_test.go | 16 ++++++++-- .../rules/overlapping_fields_can_be_merged.go | 4 +++ validator/spec/Fuzz.spec.yml | 29 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/validator/imported_test.go b/validator/imported_test.go index dc2934ac..8139304f 100644 --- a/validator/imported_test.go +++ b/validator/imported_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "regexp" "sort" + "strconv" "strings" "testing" @@ -20,7 +21,7 @@ import ( type Spec struct { Name string Rule string - Schema int + Schema string Query string Errors gqlerror.List } @@ -85,7 +86,18 @@ func runSpec(t *testing.T, schemas []*ast.Schema, deviations []*Deviation, filen } } - _, err := gqlparser.LoadQuery(schemas[spec.Schema], spec.Query) + // idx := spec.Schema + var schema *ast.Schema + if idx, err := strconv.Atoi(spec.Schema); err != nil { + var gqlErr *gqlerror.Error + schema, gqlErr = gqlparser.LoadSchema(&ast.Source{Input: spec.Schema, Name: spec.Name}) + if gqlErr != nil { + t.Fatal(err) + } + } else { + schema = schemas[idx] + } + _, err := gqlparser.LoadQuery(schema, spec.Query) var finalErrors gqlerror.List for _, err := range err { // ignore errors from other rules diff --git a/validator/rules/overlapping_fields_can_be_merged.go b/validator/rules/overlapping_fields_can_be_merged.go index 52eab3a2..bb2f1831 100644 --- a/validator/rules/overlapping_fields_can_be_merged.go +++ b/validator/rules/overlapping_fields_can_be_merged.go @@ -292,7 +292,11 @@ func (m *overlappingFieldsCanBeMergedManager) collectConflictsBetweenFieldsAndFr // (E) Then collect any conflicts between the provided collection of fields // and any fragment names found in the given fragment. + baseFragmentSpread := fragmentSpread for _, fragmentSpread := range fragmentSpreads { + if fragmentSpread.Name == baseFragmentSpread.Name { + continue + } m.collectConflictsBetweenFieldsAndFragment(conflicts, areMutuallyExclusive, fieldsMap, fragmentSpread) } } diff --git a/validator/spec/Fuzz.spec.yml b/validator/spec/Fuzz.spec.yml index a502a614..80c6851d 100644 --- a/validator/spec/Fuzz.spec.yml +++ b/validator/spec/Fuzz.spec.yml @@ -3,3 +3,32 @@ query: '{r{__typename(s:0)}}' errors: - message: Cannot query field "r" on type "QueryRoot". + +- name: 02 - infinit loop occured in OverlappingFieldsCanBeMerged rule + schema: | + type Query { + fieldA: A + } + type A { + s: String + } + query: | + { ...F } + fragment F on Query { + fieldA { s } + ...{ + ...{ + ...F + } + fieldA { + ...notExists + } + } + } + errors: + # from KnownFragmentNames rule + - message: Unknown fragment "notExists". + - message: Unknown fragment "notExists". + - message: Unknown fragment "notExists". + # from NoFragmentCycles rule + - message: Cannot spread fragment "F" within itself.