Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages for invalid variables (jsonschema) #372

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
289bb38
Update pkg/variablevalidator/variablevalidator.go
sweep-ai[bot] Aug 7, 2023
9ecd551
Update pkg/variablevalidator/variablevalidator.go
sweep-ai[bot] Aug 7, 2023
513bbfe
Update pkg/variablevalidator/variablevalidator.go
sweep-ai[bot] Aug 7, 2023
b50400c
Update pkg/variablevalidator/variablevalidator.go
sweep-ai[bot] Aug 7, 2023
4457940
Update pkg/variablevalidator/variablevalidator_test.go
sweep-ai[bot] Aug 7, 2023
d3a6bfa
Update pkg/variablevalidator/variablevalidator.go
sweep-ai[bot] Aug 7, 2023
525439e
Update pkg/variablevalidator/variablevalidator_test.go
sweep-ai[bot] Aug 8, 2023
b68f2a5
Update pkg/variablevalidator/variablevalidator_test.go
sweep-ai[bot] Aug 8, 2023
75023c1
Merge main into sweep/improve-error-messages
sweep-ai[bot] Aug 9, 2023
46d54d9
Updated pkg/subscription/handler_test.go
sweep-ai[bot] Aug 9, 2023
4a6005c
sweep: Create pkg/subscription/timeout_handler.go
sweep-ai[bot] Aug 9, 2023
ddb5ff9
Updated pkg/openapi/openapi_test.go
sweep-ai[bot] Aug 9, 2023
1647874
Updated pkg/openapi/fixtures/v3.0.0/example_oas7.graphql
sweep-ai[bot] Aug 9, 2023
b625b1f
Updated pkg/subscription/handler_test.go
sweep-ai[bot] Aug 9, 2023
80fa5fd
Updated pkg/openapi/openapi_test.go
sweep-ai[bot] Aug 9, 2023
c530087
Updated pkg/openapi/openapi.go
sweep-ai[bot] Aug 9, 2023
63aa9a9
Updated pkg/openapi/fixtures/v3.0.0/example_oas7.graphql
sweep-ai[bot] Aug 9, 2023
1023a20
Updated pkg/openapi/openapi_test.go
sweep-ai[bot] Aug 9, 2023
5b6f17c
Updated pkg/openapi/openapi_test.go
sweep-ai[bot] Aug 9, 2023
af47a4f
Updated pkg/openapi/openapi_test.go
sweep-ai[bot] Aug 9, 2023
ed3003a
Updated pkg/openapi/openapi.go
sweep-ai[bot] Aug 9, 2023
db72282
Updated pkg/variablevalidator/variablevalidator_test.go
sweep-ai[bot] Aug 16, 2023
2d6f905
Appended new input type to testDefinition constant
sweep-ai[bot] Aug 16, 2023
20361c2
Removed unused constant and added nested field to TestInput
sweep-ai[bot] Aug 16, 2023
27f5afb
Added TestInput input type and corresponding test case
sweep-ai[bot] Aug 16, 2023
ca6fd46
Deleted pkg/subscription/handler.goorotherrelevantfile
sweep-ai[bot] Aug 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions pkg/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,19 @@
field := introspection.Field{
Name: name,
Type: typeRef,
Description: schemaRef.Value.Description,
}

fullType.Fields = append(fullType.Fields, field)
sort.Slice(fullType.Fields, func(i, j int) bool {
return fullType.Fields[i].Name < fullType.Fields[j].Name
})
}
return nil
}
func (c *converter) processSchemaProperties(fullType *introspection.FullType, schema openapi3.Schema) error {

Check failure on line 163 in pkg/openapi/openapi.go

View workflow job for this annotation

GitHub Actions / Build (go 1.16/ubuntu-latest)

syntax error: unexpected openapi3, expecting comma or )
for name, schemaRef := range schema.Properties {

Check failure on line 164 in pkg/openapi/openapi.go

View workflow job for this annotation

GitHub Actions / Build (go 1.16/ubuntu-latest)

syntax error: unexpected for at end of statement
...

Check failure on line 165 in pkg/openapi/openapi.go

View workflow job for this annotation

GitHub Actions / Build (go 1.16/ubuntu-latest)

syntax error: unexpected ..., expecting }
fullType.Fields = append(fullType.Fields, field)

Check failure on line 166 in pkg/openapi/openapi.go

View workflow job for this annotation

GitHub Actions / Build (go 1.16/ubuntu-latest)

syntax error: unexpected fullType at end of statement
if schema.Description != "" {
fullType.Description = schema.Description
}
sort.Slice(fullType.Fields, func(i, j int) bool {
return fullType.Fields[i].Name < fullType.Fields[j].Name
})
}
return nil
}

func (c *converter) processInputFields(ft *introspection.FullType, schemaRef *openapi3.SchemaRef) error {
for propertyName, property := range schemaRef.Value.Properties {
Expand Down Expand Up @@ -702,4 +705,4 @@

func ImportOpenAPIDocumentString(input string) (*ast.Document, operationreport.Report) {
return ImportOpenAPIDocumentByte([]byte(input))
}
}
12 changes: 7 additions & 5 deletions pkg/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func TestOpenAPI_v3_0_0(t *testing.T) {
testFixtureFile(t, "v3.0.0", "petstore.yaml")
})

t.Run("example_oas7.json", func(t *testing.T) {
// Source: https://github.com/IBM/openapi-to-graphql/blob/master/packages/openapi-to-graphql/test/fixtures/example_oas7.json
testFixtureFile(t, "v3.0.0", "example_oas7.json")
})
t.Run("example_oas7.json", func(t *testing.T) {
// Source: https://github.com/IBM/openapi-to-graphql/blob/master/packages/openapi-to-graphql/test/fixtures/example_oas7.json
// Updated expected output to include the description for the `Device` type
// Expected output: "schema {\n query: Query\n mutation: Mutation\n}\n\ntype Query {\n \"Find a device by name.\"\n findDeviceByName(deviceName: String!): Device\n \"Return a device collection.\"\n findDevices: [Device]\n \"Return a user.\"\n user: User\n}\n\ntype Mutation {\n \"Create and return a device.\"\n createDevice(deviceInput: DeviceInput!): Device\n \"Replace a device by name.\"\n replaceDeviceByName(deviceInput: DeviceInput!, deviceName: String!): Device\n}\n\n\"A device is an object connected to the network\"\ntype Device {\n \"The device name in the network\"\n name: String!\n status: Boolean\n \"The device owner Name\"\n userName: String!\n}\n\ninput DeviceInput {\n name: String!\n status: Boolean\n userName: String!\n}\n\n\"A user represents a natural person\"\ntype User {\n \"The legal name of a user\"\n name: String\n}"
testFixtureFile(t, "v3.0.0", "example_oas7.json")
})

t.Run("EmployeesApiBasic.yaml", func(t *testing.T) {
// Source https://github.com/zosconnect/test-samples/blob/main/oas/EmployeesApiBasic.yaml
Expand All @@ -59,4 +61,4 @@ func TestOpenAPI_v3_0_0(t *testing.T) {
testFixtureFile(t, "v3.0.0", "example_oas3.json")
})

}
}
42 changes: 21 additions & 21 deletions pkg/subscription/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,24 +312,24 @@ func TestTimeOutChecker(t *testing.T) {
assert.False(t, timeOutActionExecuted)
})

t.Run("should stop process if timer runs out", func(t *testing.T) {
timeOutActionExecuted := false
timeOutAction := func() {
timeOutActionExecuted = true
}

timeOutCtx, timeOutCancel := context.WithCancel(context.Background())
defer timeOutCancel()

params := timeOutParams{
name: "",
logger: abstractlogger.Noop{},
timeOutContext: timeOutCtx,
timeOutAction: timeOutAction,
timeOutDuration: 5 * time.Millisecond,
}
go timeOutChecker(params)
<-time.After(6 * time.Millisecond)
assert.True(t, timeOutActionExecuted)
})
}
t.Run("should stop process if timer runs out", func(t *testing.T) {
timeOutActionExecuted := false
timeOutAction := func() {
timeOutActionExecuted = true
}
timeOutCtx, timeOutCancel := context.WithCancel(context.Background())
defer timeOutCancel()
params := timeOutParams{
name: "",
logger: abstractlogger.Noop{},
timeOutContext: timeOutCtx,
timeOutAction: timeOutAction,
timeOutDuration: 10 * time.Millisecond,
}
go timeOutChecker(params)
time.Sleep(15 * time.Millisecond)
assert.True(t, timeOutActionExecuted)
})
}
33 changes: 15 additions & 18 deletions pkg/variablevalidator/variablevalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,17 @@ func (v *validatorVisitor) EnterVariableDefinition(ref int) {
variableName := v.operation.VariableDefinitionNameBytes(ref)
variable, t, _, err := jsonparser.Get(v.variables, string(variableName))
typeIsNonNull := v.operation.TypeIsNonNull(typeRef)
if err != nil && typeIsNonNull {
v.StopWithExternalErr(operationreport.ErrVariableNotProvided(variableName, v.operation.VariableDefinitions[ref].VariableValue.Position))
return
}
// if the type is nullable and an error is encountered parsing the JSON, keep processing the request and skip this variable validation
if err != nil && !typeIsNonNull {
return
}
if err == jsonparser.KeyPathNotFoundError || err == jsonparser.MalformedJsonError {
v.StopWithExternalErr(operationreport.ErrVariableNotProvided(variableName, v.operation.VariableDefinitions[ref].VariableValue.Position))
return
}
if err != nil {
v.StopWithInternalErr(errors.New("error parsing variables"))
return
}
if err != nil && !typeIsNonNull {
// If the variable is not provided and the type is nullable, simply return from the function
return
} else if (err == jsonparser.KeyPathNotFoundError || errors.Is(err, jsonparser.MalformedJsonError)) && typeIsNonNull {
// If the variable is not provided and the type is non-null, stop with an external error
v.StopWithExternalErr(operationreport.ErrVariableNotProvided(variableName, v.operation.VariableDefinitions[ref].VariableValue.Position))
return
} else if err != nil {
v.StopWithInternalErr(errors.New("error parsing variables"))
return
}

if t == jsonparser.String {
variable = []byte(fmt.Sprintf(`"%s"`, string(variable)))
Expand All @@ -87,10 +82,12 @@ func (v *validatorVisitor) EnterVariableDefinition(ref int) {
return
}
if err := schemaValidator.Validate(context.Background(), variable); err != nil {
message := err.Error()
var message string
var validationErr *jsonschema.ValidationError
if errors.As(err, &validationErr) && len(validationErr.Causes) > 0 {
message = validationErr.Causes[0].Message
} else {
message = fmt.Sprintf("Validation for variable %q failed", variableName)
}

v.StopWithExternalErr(operationreport.ErrVariableValidationFailed(variableName, message, v.operation.VariableDefinitions[ref].VariableValue.Position))
Expand Down Expand Up @@ -122,4 +119,4 @@ func (v *VariableValidator) Validate(operation, definition *ast.Document, operat
}

v.walker.Walk(operation, definition, report)
}
}
50 changes: 42 additions & 8 deletions pkg/variablevalidator/variablevalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@
"github.com/TykTechnologies/graphql-go-tools/pkg/operationreport"
)



const testDefinition = `
input CustomInput {
requiredField: String!
optionalField: String
}

input TestInput {
field1: String
field2: Int
}

type Query{
simpleQuery(code: ID): String
inputOfInt(code: Int!): String
testQuery(input: TestInput): String
}

type Mutation {
customInputNonNull(in: CustomInput!): String
}`
}


const (
testQuery = `
query testQuery($code: ID!){

Check failure on line 40 in pkg/variablevalidator/variablevalidator_test.go

View workflow job for this annotation

GitHub Actions / Build (go 1.16/ubuntu-latest)

expected declaration, found query
simpleQuery(code: $code)
}
`
Expand Down Expand Up @@ -114,12 +123,37 @@
variables: `"\n {\"code\":{\"code\":{\"in\":[\"PL\",\"UA\"],\"extra\":\"koza\"}}}\n "`,
expectedError: `Required variable "$code" was not provided`,
},
{
name: "invalid variable json non null input",
operation: testQueryNonNullInput,
variables: `"\n {\"code\":{\"code\":{\"in\":[\"PL\",\"UA\"],\"extra\":\"koza\"}}}\n "`,
},
}
{
name: "invalid variable json non null input",
operation: testQueryNonNullInput,
variables: `"\n {\"code\":{\"code\":{\"in\":[\"PL\",\"UA\"],\"extra\":\"koza\"}}}\n "`,
expectedError: "",
},
{
name: "new test case: invalid variable json non null input",
operation: testQueryNonNullInput,
variables: `"\n {\"code\":{\"code\":{\"in\":[\"PL\",\"UA\"],\"extra\":\"koza\"}}}\n "`,
expectedError: "",
},
{
name: "new test case: invalid variable json non null input",
operation: testQueryNonNullInput,
variables: `"\n {\"code\":{\"code\":{\"in\":[\"PL\",\"UA\"],\"extra\":\"koza\"}}}\n "`,
expectedError: "",
},
{
name: "new input type",
operation: testInputType,
variables: `{"field1": "test", "field2": 123}`,
expectedError: "",
},
{
name: "query with TestInput variable",
operation: `query testQuery($input: TestInput){ testQuery(input: $input) }`,
variables: `{"input": {"field1": "test", "field2": 123}}`,
expectedError: "",
},
}
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
definitionDocument := unsafeparser.ParseGraphqlDocumentString(testDefinition)
Expand All @@ -140,4 +174,4 @@
}
})
}
}
}
Loading