question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getOrNull not giving a null value

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
simpleauthoritycommented, Jul 10, 2019

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

1reaction
oharaandrew314commented, Jul 11, 2019

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 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

getOrNull - Kotlin Programming Language
Returns an element at the given index or null if the index is out of bounds of this array.
Read more >
Avoiding NullPointerException in Java - Stack Overflow
The method returns the object passed into it and throws a NullPointerException if the object is null. This means that the returned value...
Read more >
getOrNull bug regression #978 - InsertKoinIO/koin - GitHub
Describe the bug The getOrNull scope method for getting an optional instance now throws an IllegalStateException instead of returning null ...
Read more >
Avoid Check for Null Statement in Java - Baeldung
Learn several strategies for avoiding the all-too-familiar boilerplate conditional statements to check for null values in Java.
Read more >
Item 7: Prefer a nullable or Result result type when the lack of ...
Return a null or Result.failure , thus indicating failure. ... getOrNull method, which returns the value if the result is a success, or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found