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.

[BUG] PHP sends boolean params as "1" or "" instead of "true" or "false"

See original GitHub issue

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What’s the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What’s the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When params are of type “boolean”, they should be sent as “true” or “false”, but the PHP generator sends them as “1” or “”. But it correctly documents the parameters as @param bool. So if you send boolean parameters, it ends up doing something like (string) true, which evaluates to 1.

openapi-generator version

4.0.0-SNAPSHOT

OpenAPI declaration file content or url

https://gist.github.com/jacobweber/e83874fbf1c0ee6ede79100a7507e11f

Command line used for generation

java -jar openapi-generator.jar generate -i <url> -g php -o /path/to/output

Steps to reproduce
  1. Build the above spec.
  2. Run composer install from the output dir.
  3. Run the “Getting Started” example code in README.md:
$admin = True; // bool | Include admin users?
try {
    $result = $apiInstance->usersGet($admin);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->usersGet: ', $e->getMessage(), PHP_EOL;
}
  1. It calls GET http://localhost/api/users?admin=1 instead of GET http://localhost/api/users?admin=true.
Related issues/PRs

N/A

Suggest a fix

Detect booleans and convert to “true” or “false”.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
etremblaycommented, Sep 21, 2020

@wing328 The fix introduced by #2257 has been removed by #3984.

ObjectSerializer::toQueryValue() has been removed, I guess to improve collection serialization, but the boolean case is still broken.

1reaction
wing328commented, Mar 31, 2019

@jacobweber @ackintosh you both have valid points.

To move this forward, I suggest we go with https://github.com/OpenAPITools/openapi-generator/pull/2257 and if there are demands to send boolean values as 1/0 in the query string or other parameters, then we can make an option to cater both cases.

(I did work on 1 or 2 issues before in which the users are expecting the client to send true/false in the query string)

Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP - Get bool to echo false when false - Stack Overflow
If the second argument is not true, it will output the result directly. ... This will print out boolean value as it is,...
Read more >
pg_query_params Sets Boolean False to Blank String
Description: ------------ pg_query_params somehow converts (boolean)false (as a parameter) to a blank string before sending it to postgres.
Read more >
Avoid Passing Booleans to Functions - Alex Kondov
So a common approach is to pass a boolean parameter, also known as a flag. While this may be initially convenient it is...
Read more >
Is it wrong to use a boolean parameter to determine behavior?
I think this question consists of two parts: 1. using a boolean variable to a method and 2. using a variable (boolean or...
Read more >
10 Most Common Mistakes That PHP Developers Make - Toptal
Despite its name, isset() not only returns false if an item does not exist, but also returns false for null values. This behavior...
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