Skip to content

Commit

Permalink
fix(i): Panic in object equal filter (sourcenetwork#3143)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves sourcenetwork#3141

## Description

This PR fixes a bug where the object equality check would panic when
comparing with a non object type.

## Tasks

- [x] I made sure the code is well commented, particularly
hard-to-understand areas.
- [x] I made sure the repository-held documentation is changed
accordingly.
- [x] I made sure the pull request title adheres to the conventional
commit style (the subset used in the project can be found in
[tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)).
- [x] I made sure to discuss its limitations such as threats to
validity, vulnerability to mistake and misuse, robustness to
invalidation of assumptions, resource requirements, ...

## How has this been tested?

Added integration test.

Specify the platform(s) on which this was tested:
- MacOS
  • Loading branch information
nasdf authored Oct 17, 2024
1 parent ba2df4a commit f528a6b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
13 changes: 8 additions & 5 deletions internal/connor/connor.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/*
Package connor provides a domain-specific language to express conditions against data.
It is derived from https://github.com/SierraSoftworks/connor.
*/
// Package connor provides a domain-specific language to express conditions against data.
//
// It is derived from https://github.com/SierraSoftworks/connor.
//
// Note to developers:
// Never assume that the data given to an operator is of a certain type.
// Operators should work with any data type so that untyped data
// such as JSON can be filtered as expected.
package connor

const (
Expand Down
4 changes: 2 additions & 2 deletions internal/connor/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func objectsEqual(condition map[string]any, data any) (bool, error) {
if data == nil {
return condition == nil, nil
}
d := data.(map[string]any)
if len(d) != len(condition) {
d, ok := data.(map[string]any)
if !ok || len(d) != len(condition) {
return false, nil
}
for k, v := range d {
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/query/json/with_eq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,65 @@ func TestQueryJSON_WithEqualFilterWithNullValue_ShouldFilter(t *testing.T) {

testUtils.ExecuteTestCase(t, test)
}

func TestQueryJSON_WithEqualFilterWithAllTypes_ShouldFilter(t *testing.T) {
test := testUtils.TestCase{
Description: "Simple query with JSON _eq filter all types",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
Name: String
Custom: JSON
}
`,
},
testUtils.CreateDoc{
Doc: `{
"Name": "Shahzad",
"Custom": "32"
}`,
},
testUtils.CreateDoc{
Doc: `{
"Name": "Andy",
"Custom": [1, 2]
}`,
},
testUtils.CreateDoc{
Doc: `{
"Name": "Fred",
"Custom": {"one": 1}
}`,
},
testUtils.CreateDoc{
Doc: `{
"Name": "John",
"Custom": false
}`,
},
testUtils.CreateDoc{
Doc: `{
"Name": "David",
"Custom": 32
}`,
},
testUtils.Request{
Request: `query {
Users(filter: {Custom: {_eq: {one: 1}}}) {
Name
}
}`,
Results: map[string]any{
"Users": []map[string]any{
{
"Name": "Fred",
},
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

0 comments on commit f528a6b

Please sign in to comment.