getOrNull not giving a null value
See original GitHub issueActual behavior (the bug)
I have optional/required parameters for a specific route. I use getOrNull
by default to either get the value or get null in its place. Then, I check if optional
is true and if value
is null
. If so, I just return null from my handler. However, my route execution is short-circuited by getOrNull
and Postman has:
{
"title": "Form parameter 'name removed' with value 'null' invalid - Failed check",
"status": 400,
"type": "https://javalin.io/documentation#badrequestresponse",
"details": []
}
Expected behavior
I expect to be able to work with null
.
To Reproduce
Call getOrNull
with a parameter that is supposed to be allowed to be null
.
Additional context Code I’m using:
String resolved = validator.getOrNull(); // I am short-circuited here if it is null and I do not reach the if.
if (optional && resolved == null) {
return null;
} else if (resolved != null) {
return parser.parse(resolved);
}
I’m not very proficient in Kotlin, though I do use it on occasion. This doesn’t look right to me, but I could be wrong.
fun getOrNull(): T? = rules
.find { value == null || !it.test.invoke(value) }
?.let { throw BadRequestResponse(it.invalidMessage) }
?: value
It seems that if the value is null, the ?.let {}
will run and throw a 400 Bad Request
. The value is only returned if it isn’t null. (Edit: verified with debugger)
I am not sure of anything else, but if you need more please let me know and I will get the information for you.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Thank you for your prompt work on this @oharaandrew314. It is appreciated.
I’ll add a test for that to make sure it’s fixed.
On Wed., Jul. 10, 2019, 7:35 p.m. Jacob Andersen, notifications@github.com wrote: