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

fix: WEOS-1492 Can't filter on property that is camel cased #170

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
100 changes: 91 additions & 9 deletions controllers/rest/fixtures/blog-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ x-weos-config:
driver: sqlite3
database: integration.db
database:
database: "%s"
driver: "%s"
host: "%s"
password: "%s"
username: "%s"
port: %d
database: "%s"
driver: "%s"
host: "%s"
password: "%s"
username: "%s"
port: %d
rest:
middleware:
- RequestID
Expand All @@ -36,6 +36,10 @@ components:
type: string
description:
type: string
blogs:
type: array
items:
$ref: "#/components/schemas/Blog"
required:
- title
x-identifier:
Expand Down Expand Up @@ -86,11 +90,11 @@ components:
categories:
type: array
items:
$ref: "#/components/schemas/Post"
$ref: "#/components/schemas/Category"
posts:
type: array
items:
$ref: "#/components/schemas/Category"
$ref: "#/components/schemas/Post"
lastUpdated:
type: string
format: date-time
Expand Down Expand Up @@ -426,4 +430,82 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/Author"
$ref: "#/components/schemas/Author"
get:
operationId: Get Authors
summary: Get List of Authors
parameters:
- in: query
name: page
schema:
type: integer
- in: query
name: limit
schema:
type: integer
- in: query
name: _filters
schema:
type: array
items:
type: object
properties:
field:
type: string
operator:
type: string
values:
type: array
items:
type: string
required: false
description: query string
responses:
200:
description: List of Authors
content:
application/json:
schema:
type: object
properties:
total:
type: integer
page:
type: integer
items:
type: array
items:
$ref: "#/components/schemas/Author"
/authors/batch:
post:
operationId: Add Authors
requestBody:
description: List of Authors to add
required: true
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Author"
application/x-www-form-urlencoded:
schema:
type: array
items:
$ref: "#/components/schemas/Author"
application/xml:
schema:
type: array
items:
$ref: "#/components/schemas/Author"
responses:
201:
description: Added Authors to Aggregator
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Author"
400:
description: Invalid author submitted
78 changes: 78 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,81 @@ func TestIntegration_ManyToOneRelationship(t *testing.T) {

dropDB()
}

func TestIntegration_FilteringByCamelCase(t *testing.T) {
content, err := ioutil.ReadFile("./controllers/rest/fixtures/blog-integration.yaml")
if err != nil {
t.Fatal(err)
}
contentString := string(content)
contentString = fmt.Sprintf(contentString, dbconfig.Database, dbconfig.Driver, dbconfig.Host, dbconfig.Password, dbconfig.User, dbconfig.Port)

tapi, err := api.New(contentString)
if err != nil {
t.Fatalf("unexpected error loading spec '%s'", err)
}
err = tapi.Initialize(context.TODO())
if err != nil {
t.Fatalf("unexpected error loading spec '%s'", err)
}

e := tapi.EchoInstance()

//create bach authors for tests
authors := []map[string]interface{}{
{
"firstName": "first",
"lastName": "first",
},
{
"firstName": "second",
"lastName": "second",
},
{
"firstName": "third",
"lastName": "third",
},
}
reqBytes, err := json.Marshal(authors)
if err != nil {
t.Fatalf("error setting up request %s", err)
}
body := bytes.NewReader(reqBytes)
resp := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/authors/batch", body)
header = http.Header{}
header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header = header
req.Close = true
e.ServeHTTP(resp, req)

if resp.Result().StatusCode != http.StatusCreated {
t.Fatalf("expected to get status %d creating fixtures, got %d", http.StatusCreated, resp.Result().StatusCode)
}

t.Run("filtering by using the name in the spec file", func(t *testing.T) {
queryString := "_filters[firstName][eq]=first"
resp := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/authors?"+queryString, nil)
req.Close = true
e.ServeHTTP(resp, req)

if resp.Result().StatusCode != http.StatusOK {
t.Fatalf("expected to get status %d getting item, got %d", http.StatusOK, resp.Result().StatusCode)
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var resultAuthor api.ListApiResponse
err = json.Unmarshal(bodyBytes, &resultAuthor)
if err != nil {
t.Errorf("unexpected error : got error unmarshalling response body, %s", err)
}
if len(resultAuthor.Items) != 1 {
t.Errorf("expected number of items to be %d got %d ", 1, len(resultAuthor.Items))
}

})
}
6 changes: 3 additions & 3 deletions projections/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,13 @@ func NewProjection(ctx context.Context, db *gorm.DB, logger weos.Log) (*GORMDB,

if len(filter.Values) == 0 {
if filter.Operator == "like" {
db.Where(filter.Field+" "+operator+" ?", "%"+filter.Value.(string)+"%")
db.Where(utils.SnakeCase(filter.Field)+" "+operator+" ?", "%"+filter.Value.(string)+"%")
} else {
db.Where(filter.Field+" "+operator+" ?", filter.Value)
db.Where(utils.SnakeCase(filter.Field)+" "+operator+" ?", filter.Value)
}

} else {
db.Where(filter.Field+" "+operator+" ?", filter.Values)
db.Where(utils.SnakeCase(filter.Field)+" "+operator+" ?", filter.Values)
}

}
Expand Down