Skip to content

Commit

Permalink
Merge pull request #80 from jensneuse/bug/empty-list
Browse files Browse the repository at this point in the history
Failing empty list bug
  • Loading branch information
jensneuse authored May 20, 2019
2 parents 5c6d04a + 795d9e4 commit f0e4400
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 31 deletions.
33 changes: 33 additions & 0 deletions pkg/middleware/context_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ func TestContextMiddleware(t *testing.T) {
}
want := testhelper.UglifyRequestString(privateQueryWithEscapedQuote)

if want != got {
panic(fmt.Errorf("\nwant:\n%s\ngot:\n%s", want, got))
}
})
t.Run("empty list as a parameter should work", func(t *testing.T) {

// it's important to quote the value so the lexer will recognize it's a string value
// we might push this including checks into the implementation
ctx := context.WithValue(context.Background(), "user", "jsmith@example.org")

got, err := InvokeMiddleware(&ContextMiddleware{}, ctx, publicSchema, emptyListQuery)
if err != nil {
t.Fatal(err)
}

want := testhelper.UglifyRequestString(emptyListQuery)

if want != got {
panic(fmt.Errorf("\nwant:\n%s\ngot:\n%s", want, got))
}
Expand All @@ -113,12 +130,18 @@ schema {
type Query {
documents: [Document] @addArgumentFromContext(name: "user",contextKey: "user")
Document: [Document]
Drawer: [Drawer]
}
type Document implements Node {
owner: String
sensitiveInformation: String
}
type Drawer {
documents(owners: [String]!): [Document]
}
`

/*
Expand Down Expand Up @@ -162,3 +185,13 @@ query myDocuments {
}
}
`

const emptyListQuery = `
query emptyList {
Drawer {
documents(owners: []){
sensitiveInformation
}
}
}
`
4 changes: 2 additions & 2 deletions pkg/middleware/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

type GraphQLRequest struct {
OperationName string `json:"operationName,omitempty"`
Query string `json:"query"`
OperationName string `json:"operationName,omitempty"`
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/parser/list_value_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ import (
"github.com/jensneuse/graphql-go-tools/pkg/lexing/keyword"
)

func (p *Parser) parsePeekedListValue(index *int) error {
func (p *Parser) parsePeekedListValue() (ref int, err error) {

p.l.Read()

listValue := p.makeListValue(index)
listValue := p.IndexPoolGet()

for {

peeked := p.l.Peek(true)

if peeked == keyword.SQUAREBRACKETCLOSE {
p.l.Read()
p.putListValue(listValue, index)
return nil
return p.putListValue(listValue), nil

} else {

var next int
next, err := p.parseValue()
if err != nil {
return err
return -1, err
}

listValue = append(listValue, next)
Expand Down
17 changes: 4 additions & 13 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,6 @@ func (p *Parser) initExecutableDefinition() {
p.ParsedDefinitions.ExecutableDefinition.FragmentDefinitions = p.IndexPoolGet()
}

func (p *Parser) makeListValue(index *int) document.ListValue {
value := p.IndexPoolGet()
p.ParsedDefinitions.ListValues = append(p.ParsedDefinitions.ListValues, value)
*index = len(p.ParsedDefinitions.ListValues) - 1
return value
}

func (p *Parser) makeObjectValue(index *int) document.ObjectValue {
value := p.IndexPoolGet()
p.ParsedDefinitions.ObjectValues = append(p.ParsedDefinitions.ObjectValues, value)
Expand Down Expand Up @@ -731,16 +724,14 @@ func (p *Parser) putFloat(float float32) int {
return len(p.ParsedDefinitions.Floats) - 1
}

func (p *Parser) putListValue(value document.ListValue, index *int) {
func (p *Parser) putListValue(value document.ListValue) (ref int) {
for i, known := range p.ParsedDefinitions.ListValues {
if p.integersContainSameValues(value, known) {
p.ParsedDefinitions.ListValues = // delete the dupe
append(p.ParsedDefinitions.ListValues[:*index], p.ParsedDefinitions.ListValues[*index+1:]...)
*index = i
return
return i
}
}
p.ParsedDefinitions.ListValues[*index] = value
p.ParsedDefinitions.ListValues = append(p.ParsedDefinitions.ListValues, value)
return len(p.ParsedDefinitions.ListValues) - 1
}

func (p *Parser) putObjectValue(value document.ObjectValue, index *int) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func TestParser_putListValue(t *testing.T) {
var listValueIndex int
var listValueIndex2 int

listValue := parser.makeListValue(&listValueIndex)
listValue2 := parser.makeListValue(&listValueIndex2)
listValue := parser.IndexPoolGet()
listValue2 := parser.IndexPoolGet()
listValue = append(listValue, valueIndex)
listValue2 = append(listValue2, valueIndex)

parser.putListValue(listValue, &listValueIndex)
parser.putListValue(listValue2, &listValueIndex2)
listValueIndex = parser.putListValue(listValue)
listValueIndex2 = parser.putListValue(listValue2)

if listValueIndex != listValueIndex2 {
panic("expect lists to be merged")
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/value_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *Parser) parseValue() (ref int, err error) {
p.parsePeekedByteSlice(&value)
case keyword.SQUAREBRACKETOPEN:
value.ValueType = document.ValueTypeList
err = p.parsePeekedListValue(&value.Reference)
value.Reference, err = p.parsePeekedListValue()
case keyword.CURLYBRACKETOPEN:
value.ValueType = document.ValueTypeObject
err = p.parsePeekedObjectValue(&value.Reference)
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func (pr *ProxyRequest) AcceptRequest(buff *bytes.Buffer) error {

func (pr *ProxyRequest) DispatchRequest(buff *bytes.Buffer) (io.ReadCloser, error) {
req := middleware.GraphQLRequest{
Query: buff.String(),
Query: buff.String(),
OperationName: pr.GraphQLRequest.OperationName,
Variables: pr.GraphQLRequest.Variables,
Variables: pr.GraphQLRequest.Variables,
}

out := bytes.Buffer{}
Expand Down
8 changes: 4 additions & 4 deletions pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type Proxy struct {
}

type Request struct {
Config *RequestConfig
RequestURL url.URL
Body io.Reader
Context context.Context
Config *RequestConfig
RequestURL url.URL
Body io.Reader
Context context.Context
GraphQLRequest middleware.GraphQLRequest
}

Expand Down

0 comments on commit f0e4400

Please sign in to comment.