Skip to content

[Php] Convert boolean value for query string (additional option)#11880

Closed
ksvirkou-hubspot wants to merge 18 commits intoOpenAPITools:masterfrom
ksvirkou-hubspot:feature/queryParamValues
Closed

[Php] Convert boolean value for query string (additional option)#11880
ksvirkou-hubspot wants to merge 18 commits intoOpenAPITools:masterfrom
ksvirkou-hubspot:feature/queryParamValues

Conversation

@ksvirkou-hubspot
Copy link
Contributor

@ksvirkou-hubspot ksvirkou-hubspot commented Mar 15, 2022

This PR creates an additional property "booleanFormatForQueryString"
User can choose the format for conversation of boolean to the query string
user can choose int (param=1 or param=0) or string (param=true or param=false)
int is default format because guzzle/psr7 converts boolean to int (param=1 or param=0)

it will fix #11683

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@ksvirkou-hubspot
Copy link
Contributor Author

@ksvirkou-hubspot
Copy link
Contributor Author

Hi Everyone
Could anyone check this pr, please?
@jebentier @dkarlovi @mandrean @jfastnacht @ackintosh @ybelenko @renepardon
@wing328

@ksvirkou-hubspot ksvirkou-hubspot changed the title [Php] add an additional property "booleanFormatForQueryString" [Php] Convert boolean value for query string Mar 17, 2022
@ybelenko ybelenko self-requested a review March 18, 2022 01:05
@ybelenko
Copy link
Contributor

Ohh, you're fixing the same part of generator that I do right now... I changed toQueryValue method significantly at #11225 because we need to support many styles of query serialization including deepObject.

/**
* Take query parameter properties and turn it into an array suitable for
* native http_build_query or GuzzleHttp\Psr7\Query::build.
*
* @param mixed $value Parameter value
* @param string $paramName Parameter name
* @param string $openApiType OpenAPIType eg. array or object
* @param string $style Parameter serialization style
* @param bool $explode Parameter explode option
*
* @return array
*/
public static function toQueryValue(
$value,
string $paramName,
string $openApiType = 'string',
string $style = 'form',
bool $explode = true
): array {
// return empty string
if (empty($value)) return ["{$paramName}" => ''];
$query = [];
$value = (in_array($openApiType, ['object', 'array'], true)) ? (array)$value : $value;
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
// need to flatten array first
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
if (!is_array($arr)) return $arr;
foreach ($arr as $k => $v) {
$prop = ($style === 'deepObject') ? $prop = "{$name}[{$k}]" : $k;
if (is_array($v)) {
$flattenArray($v, $prop, $result);
} else {
if ($style !== 'deepObject' && !$explode) {
// push key itself
$result[] = $prop;
}
$result[$prop] = $v;
}
}
return $result;
};
$value = $flattenArray($value, $paramName);
if ($openApiType === 'object' && ($style === 'deepObject' || $explode)) {
return $value;
}
// handle style in serializeCollection
$query[$paramName] = ($explode) ? $value : self::serializeCollection((array)$value, $style);
return $query;
}

We can't merge this PR before #11225. I can integrate your changes into new method later, don't worry. If you want to help, you can write unit tests at samples/client/petstore/php/OpenAPIClient-php/tests/ObjectSerializerTest.php. Wait... seems like you can't because you need to test updated toQueryValue function which you don't have right now. Anyway, this changes will be published in next major release 6.0.0 which scheduled for the end of the March, so hold on, we won't forget your PR.

@ybelenko
Copy link
Contributor

It cannot be merged without at least few unit tests, so please describe some test cases, so we can cover your feature.

@ksvirkou-hubspot
Copy link
Contributor Author

@ybelenko
I'll be waiting until you merge and then I'll update this PR and create some tests like these

ksvirkou-hubspot and others added 4 commits March 22, 2022 19:34
# Conflicts:
#	modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
#	modules/openapi-generator/src/main/resources/php/api.mustache
#	samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php
#	samples/client/petstore/php/OpenAPIClient-php/lib/Api/PetApi.php
#	samples/client/petstore/php/OpenAPIClient-php/lib/Api/UserApi.php
#	samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php
|allowUnicodeIdentifiers|boolean, toggles whether unicode identifiers are allowed in names or not, default is false| |false|
|apiPackage|package for generated api classes| |null|
|artifactVersion|The version to use in the composer package version field. e.g. 1.2.3| |null|
|booleanFormatForQueryString|Specifies the format for converting boolean to query string. The default is 'int'.(e.g. int param=1 or param=0 string param=true or param=false)|<dl><dt>**int**</dt><dd>int</dd><dt>**string**</dt><dd>string</dd></dl>|int|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional codegen option, really? Are you sure there are many users who need this?
Beside why not to add that option to modules/openapi-generator/src/main/resources/php/Configuration.mustache instead? That way you can change bool serialization without regeneration of client SDK.

Copy link
Contributor Author

@ksvirkou-hubspot ksvirkou-hubspot Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional option is better then option in Configuration.mustache
Because no necessary for an user to set up it because it will be common behavior for all requests

no, I think it is necessary for a small number of users (one more reason for using Additional option)

In my opinion it is better to have an Additional option then have option in Configuration.mustache because it will be like “excessive option in Configuration.mustache
but If for you better option in Configuration.mustache you can say and I’ll rewrite it
What do you think?
@ybelenko

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 As far as I remember you also against adding dozens of options to codegen. In current situation I think Configuration php class is better place to put this option.

@ksvirkou-hubspot Your suggested codegen option changes one single line of code, I understand that it's convinient to you, but client SDK users always ask to consider their use case as one and only important. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok
it was only thought
waiting decision and will do it

@ybelenko
Copy link
Contributor

ybelenko commented May 3, 2022

@ksvirkou-hubspot I don't clearly remember did you add that fix in #12120 or not. You should rebase to latest master and solve conflicts if you need this merged.

# Conflicts:
#	modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
#	samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php
#	samples/client/petstore/php/OpenAPIClient-php/tests/ObjectSerializerTest.php
@ksvirkou-hubspot
Copy link
Contributor Author

ksvirkou-hubspot commented May 4, 2022

@ksvirkou-hubspot I don't clearly remember did you add that fix in #12120 or not. You should rebase to latest master and solve conflicts if you need this merged.

I ve merged master
Short information
I create an additional codegen option "booleanFormatForQueryString"
for setting format for converting boolean for query string
int (param=1 or param=0) or string (param=true or param=false)
we discussed it above
Could you check it one more time, please?
I am ready to rewrite or update something
CC @ybelenko

@ybelenko
Copy link
Contributor

ybelenko commented May 6, 2022

As I said I would vote against codegen option in favour to config option. I think you can wait for somebody who share your vision to approve. Otherwise you can rewrite it a bit to use config option and I'll be pleased to merge.

@ksvirkou-hubspot
Copy link
Contributor Author

ksvirkou-hubspot commented May 17, 2022

This PR creates an additional property "booleanFormatForQueryString" User can choose the format for conversation of boolean to the query string user can choose int (param=1 or param=0) or string (param=true or param=false) int is default format because guzzle/psr7 converts boolean to int (param=1 or param=0)

it will fix #11683

HI @ybelenko
I ve created new PR
Could you check it, please?

@ksvirkou-hubspot ksvirkou-hubspot changed the title [Php] Convert boolean value for query string [Php] Convert boolean value for query string (additional option) May 23, 2022
@ksvirkou-hubspot
Copy link
Contributor Author

Any updates?
@ybelenko @wing328

@ybelenko
Copy link
Contributor

I would close this in favour of #12385

@wing328 wing328 closed this May 25, 2022
@ksvirkou-hubspot ksvirkou-hubspot deleted the feature/queryParamValues branch May 25, 2022 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REQ] [Php] Convert boolean value for query string

3 participants