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.

Numeric value for a boolean value is accepted.

See original GitHub issue

Describe the bug A numeric value for a boolean value is accepted. Any numeric value besides 0 ends up in the boolean value getting set to true. 0 will set it to false. Using the openapi-generator to generate the code where there is a boolean property in the request body. Note this is for requests where the value is not in quotes. If the value is in quotes only true or false is accepted.

Version information jackson-databind 2.11.2 openapi-generator 4.2.3

To Reproduce Body of rest request. { "booleanProperty": 9 }

Outcome

<html><body>
Value Boolean value set to
0 false
1 true
9 true
-1 true
</body> </html>

Expected behavior Only 0 or 1 should be accepted as valid options besides true or false. A config option should be available so that only true or false are valid options.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
GedMarccommented, Jul 22, 2021

@johnreilly100 godo stuff man glad it’s sorted out -

(-1) is true in boolean cause of JavaScript, COBOL, BASIC, and any other linear language 😉

By programming rules - quote - “All values are truthy unless they are defined as falsy (i.e., except for false, 0, “”, null, undefined, and NaN).” https://stackoverflow.com/questions/34726070/in-javascript-is-1-true-or-false https://www.tutorialspoint.com/cobol/cobol_condition_statements.htm

I know what you say sounds right, and to a normal human it would make sense - but, not in our world 😃

1reaction
cowtowncodercommented, Jul 19, 2021

As per @GedMarc, this is a feature, not bug. A few coercions are accepted: originally this particular one was due to languages like C and Perl not having native boolean types.

But it is possible to prevent this acceptance using CoercionConfig, explained ff.ex here:

https://cowtowncoder.medium.com/jackson-2-12-most-wanted-4-5-cbc91c00bcd2

and the example case:

mapper.coercionConfigFor(LogicalType.Boolean)
   .setCoercion(CoercionInputShape.Integer, CoercionAction.Fail);

which specifically makes this coercion fail.

Existing default can not really be changed for Jackson 2.x for backwards compatibility.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Boolean values - IBM
A Boolean value represents a truth value; that is, TRUE or FALSE. A Boolean expression or predicate can result in a value of...
Read more >
Boolean or truth values - Analytica Wiki
A Boolean or truth value can be True and False , or, equivalently, the number 1 or 0. For example: ... It actually...
Read more >
Boolean data type - Wikipedia
In computer science, the Boolean (sometimes shortened to Bool) is a data type that has one of two possible values (usually denoted true...
Read more >
In Javascript, do integer values of 0/1 evaluate the same as ...
In Javascript, do if/while/do-while statements evaluate a 0 or 1 as they would a Boolean variable (true/false)? In the introduction code to Dragon...
Read more >
Boolean Data Type - Visual Basic | Microsoft Learn
When Visual Basic converts numeric data type values to Boolean , 0 becomes False and all other values become True . When Visual...
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