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

Feat/blacklist #83

Merged
merged 35 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bd97d3a
feat: is null/is not null
Jan 12, 2024
4299470
Merge branch 'feat/add-between' into feat/is-null
Jan 17, 2024
6af7374
fix: search null for collections
Jan 17, 2024
446fb05
refactor: null not case sensitive
Jan 17, 2024
8f45975
doc: update README.md
Jan 17, 2024
3c118a1
Merge branch 'feat/refactor' into feat/is-null
reifocS Jan 22, 2024
df52d0d
refactor: throw if operand != IS | IS NOT
reifocS Jan 22, 2024
f363a56
Merge branch 'feat/gte-string' into feat/is-null
reifocS Jan 22, 2024
7887789
refactor: throw if calling buildPredicate parent method with empty cl…
reifocS Jan 22, 2024
00615a2
refactor: don't allow search for null collections
reifocS Jan 22, 2024
17b189f
refactor: don't allow search for null collections
reifocS Jan 22, 2024
9f0107f
fix: lint
reifocS Jan 22, 2024
4bbb7a7
test: add test for searching for empty non-collection fields
reifocS Jan 22, 2024
d03db3d
refactor: avoid passing null value, check literal value instead (Sear…
reifocS Jan 23, 2024
3bc14fb
feat: blacklist
reifocS Jan 23, 2024
a68662f
fix: add missing check for "NULL" value in strategies
reifocS Jan 23, 2024
30f704c
Merge branch 'feat/is-null' into feat/blacklist
reifocS Jan 23, 2024
78cd88d
fix: missing arg in constructor
reifocS Jan 23, 2024
4df5299
Merge branch 'master' into feat/is-null
reifocS May 6, 2024
8223e61
fix: update threshold
reifocS May 6, 2024
a9927b2
fix: remove unreachable code
reifocS May 6, 2024
079df6b
doc: add blacklist doc
reifocS May 13, 2024
ae8e7bc
test: add test for UUID null
reifocS May 15, 2024
e6e45c2
test: canGetUsersWithUpdatedDateAtNull
reifocS May 15, 2024
131c7ab
test: LocalDateTime is not null
reifocS May 16, 2024
f145cd5
test: Int Boolean and Date is null
reifocS May 16, 2024
6e8aba2
trigger-ci
reifocS May 16, 2024
e1aace9
trigger-ci
reifocS May 16, 2024
3197797
Merge branch 'master' into feat/is-null
reifocS Jun 20, 2024
31c675e
fix: ktlint
reifocS Jun 20, 2024
dee880f
fix: ktlint
reifocS Jun 20, 2024
8355580
fix: ktlint
reifocS Jun 20, 2024
24c5e25
Merge branch 'feat/is-null' into feat/blacklist
reifocS Jun 20, 2024
cdb879b
fix: test missing arguments
reifocS Jun 20, 2024
4d65211
Merge branch 'master' into feat/blacklist
reifocS Jun 20, 2024
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ Request : `/cars?search=creationyear:2018 AND price<300000 AND (color:Yellow OR
15. Using the BETWEEN operator
Request : `/cars?search=creationyear BETWEEN 2017 AND 2019`

## Blocking the search on a field
```java
@GetMapping
public List<User> getUsers(@SearchSpec(blackListedFields = {"password"}) Specification<User> specs) {
return userRepository.findAll(Specification.where(specs));
}
```

<!-- TROUBLESHOOTING -->
## Troubleshooting

Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/com/sipios/springsearch/QueryVisitorImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.sipios.springsearch.anotation.SearchSpec
import com.sipios.springsearch.grammar.QueryBaseVisitor
import com.sipios.springsearch.grammar.QueryParser
import org.springframework.data.jpa.domain.Specification
import org.springframework.http.HttpStatus
import org.springframework.web.server.ResponseStatusException

class QueryVisitorImpl<T>(private val searchSpecAnnotation: SearchSpec) : QueryBaseVisitor<Specification<T>>() {
private val valueRegExp = Regex(pattern = "^(?<prefix>\\*?)(?<value>.+?)(?<suffix>\\*?)$")
Expand Down Expand Up @@ -32,6 +34,7 @@ class QueryVisitorImpl<T>(private val searchSpecAnnotation: SearchSpec) : QueryB

override fun visitIsCriteria(ctx: QueryParser.IsCriteriaContext): Specification<T> {
val key = ctx.key().text
verifyBlackList(key)
val op = if (ctx.IS() != null) {
SearchOperation.IS
} else {
Expand All @@ -42,6 +45,7 @@ class QueryVisitorImpl<T>(private val searchSpecAnnotation: SearchSpec) : QueryB

override fun visitEqArrayCriteria(ctx: QueryParser.EqArrayCriteriaContext): Specification<T> {
val key = ctx.key().text
verifyBlackList(key)
val op = if (ctx.IN() != null) {
SearchOperation.IN_ARRAY
} else {
Expand All @@ -58,6 +62,7 @@ class QueryVisitorImpl<T>(private val searchSpecAnnotation: SearchSpec) : QueryB

override fun visitBetweenCriteria(ctx: QueryParser.BetweenCriteriaContext): Specification<T> {
val key = ctx.key().text
verifyBlackList(key)
val leftValue = if (ctx.left.STRING() != null) {
clearString(ctx.left.text)
} else {
Expand Down Expand Up @@ -93,6 +98,7 @@ class QueryVisitorImpl<T>(private val searchSpecAnnotation: SearchSpec) : QueryB
} else {
ctx.value().text
}
verifyBlackList(key)
val matchResult = this.valueRegExp.find(value)
val op = SearchOperation.getSimpleOperation(ctx.op().text) ?: throw IllegalArgumentException("Invalid operation")
val criteria = SearchCriteria(
Expand All @@ -106,6 +112,16 @@ class QueryVisitorImpl<T>(private val searchSpecAnnotation: SearchSpec) : QueryB
return SpecificationImpl(criteria, searchSpecAnnotation)
}

private fun verifyBlackList(key: String?) {
val blackList = this.searchSpecAnnotation.blackListedFields
if (blackList.contains(key)) {
throw ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Field $key is blacklisted"
)
}
}

private fun clearString(value: String) = value
.removeSurrounding("'")
.removeSurrounding("\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ annotation class SearchSpec(
/**
* A flag to indicate if the search needs to be case-sensitive or not
*/
val caseSensitiveFlag: Boolean = true
val caseSensitiveFlag: Boolean = true,

/**
* A list of fields that should be excluded from the search
*/
val blackListedFields: Array<String> = []
)
Loading
Loading