Skip to content

Commit

Permalink
Merge pull request #60 from fossology/fix/search/fuzzy
Browse files Browse the repository at this point in the history
fix(search): check column name before searching

Reviewed-by: [email protected]
  • Loading branch information
GMishx authored May 20, 2024
2 parents 66b83ac + 2681632 commit 9f23523
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,19 @@ func SearchInLicense(c *gin.Context) {
query := db.DB.Model(&license)

if input.Search == "fuzzy" {
query = query.Where(fmt.Sprintf("%s ILIKE ?", input.Field), fmt.Sprintf("%%%s%%", input.SearchTerm))
if !db.DB.Migrator().HasColumn(&models.LicenseDB{}, input.Field) {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: fmt.Sprintf("invalid field name '%s'", input.Field),
Error: "field does not exist in the database",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
return
}
query = query.Where(fmt.Sprintf("%s ILIKE ?", input.Field),
fmt.Sprintf("%%%s%%", input.SearchTerm))
} else if input.Search == "" || input.Search == "full_text_search" {
query = query.Where(input.Field+" @@ plainto_tsquery(?)", input.SearchTerm)
} else {
Expand Down

0 comments on commit 9f23523

Please sign in to comment.