v2.2.3 How to have a check that saves the value of a json field if exists and passes validation even if it does not exist

Beginning in v2.2.2 I notice that my jsonPath checks now are more strict in the way that when a field in a response JSON does not exists or is an empty string and I have a “check” with a “saveAs” for that field I now get the following exception in runtime:

[INFO ] i.g.c.v.package$ - Boon failed to parse into a valid AST: JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is ‘o’ with an int value of 111
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
oauth_problem=token_rejected&oauth_problem_advice=the oauth_token is unacceptable to the Service Provider
^
io.advantageous.boon.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is ‘o’ with an int value of 111
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0

This mostly happens when I get a 401 ‘Unauthorized’ status code response from the API I am calling.

This didn’t happen before the upgrade to v2.2.2 and still occurs on v2.2.3.

I am thinking that if I get a “saveAsIfExists” method that always validates as successful even if the field to be saved does not exist, I could avoid these issues.

If there is no such option now, please help me understand how to implement it. I am browsing the source code and am not really sure what to do.

Thanks in advance.

please help

.check(jsonPath("$…id").optional.saveAs(“testValue”))) ?

But i’d recommend to find a reason of 401 error

Thanks!
I didn’t realize that is that the “optional” function did.

Unfortunately the 401 occurs in low frequency on high loads on those APIs.
Kind of a bug, but we need to be able to support it in our simulations…

Much appreciated!

Isn’t the purpose of tests to identify such bugs and then fix them, instead of adapting the tests so they can fail/retry silently?

Of course, though tests aren’t meant to only find a single bug and then wait till it is fixed until trying to discover additional ones.
At the moment this single bug in that backend API kills our simulation, not allowing us to continue looking for other bugs.
Or at least it did, until Nikolay suggested I use “optional.saveAs”
:slight_smile:

Makes sense then :slight_smile: