Javalin: getOrNull not giving a null value

Created on 10 Jul 2019  Â·  6Comments  Â·  Source: tipsy/javalin

Actual 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.

All 6 comments

This function was recently added by @oharaandrew314, maybe he knows what's up?

@simpleauthority, I was able to reproduce the error you describe, but I'd like to confirm that the causes are the same.

Was your validator created for a query param with at least one check added to it?

@oharaandrew314 Form param, but yes at least one check was added.

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:

@oharaandrew314 https://github.com/oharaandrew314 Form param, but yes
at least one check was added.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/tipsy/javalin/issues/655?email_source=notifications&email_token=AAG2JSD7YJQ7UL5WDXI3KCDP6ZW4ZA5CNFSM4H7NDTKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZVBPBA#issuecomment-510269316,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAG2JSE7QPWYNTZILAJMWQDP6ZW4ZANCNFSM4H7NDTKA
.

Thank you for your prompt work on this @oharaandrew314. It is appreciated.

To be thorough, it may also be wise to add a test for path params that can be omitted (null) with checks added.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonerer picture jonerer  Â·  4Comments

mikexliu picture mikexliu  Â·  3Comments

JosefEvAlloc picture JosefEvAlloc  Â·  5Comments

MFernstrom picture MFernstrom  Â·  3Comments

accron-1 picture accron-1  Â·  4Comments