Skip to content

Commit eceef28

Browse files
authored
[PHP][php-nextgen] Improve method parameter typing (#20361)
* Fix #20354 - Nullable mixed type is not allowed * Add parameter types for request and asyncWithHttpInfo functions
1 parent 806b99e commit eceef28

File tree

18 files changed

+384
-380
lines changed

18 files changed

+384
-380
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
214214
if (param.isArray || param.isMap) {
215215
param.vendorExtensions.putIfAbsent("x-php-param-type", "array");
216216
} else {
217-
param.vendorExtensions.putIfAbsent("x-php-param-type", param.dataType);
217+
String paramType = param.dataType;
218+
if ((!param.required || param.isNullable) && !paramType.equals("mixed")) { // optional or nullable but not mixed
219+
paramType = "?" + paramType;
220+
}
221+
param.vendorExtensions.putIfAbsent("x-php-param-type", paramType);
218222
}
219223
}
220224
}

modules/openapi-generator/src/main/resources/php-nextgen/api.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ use {{invokerPackage}}\ObjectSerializer;
172172
public function {{operationId}}(
173173
{{^vendorExtensions.x-group-parameters}}
174174
{{#allParams}}
175-
{{^required}}?{{/required}}{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
175+
{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
176176
{{/allParams}}
177177
{{#servers}}
178178
{{#-first}}
@@ -247,7 +247,7 @@ use {{invokerPackage}}\ObjectSerializer;
247247
public function {{operationId}}WithHttpInfo(
248248
{{^vendorExtensions.x-group-parameters}}
249249
{{#allParams}}
250-
{{^required}}?{{/required}}{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
250+
{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
251251
{{/allParams}}
252252
{{#servers}}
253253
{{#-first}}
@@ -446,7 +446,7 @@ use {{invokerPackage}}\ObjectSerializer;
446446
public function {{operationId}}Async(
447447
{{^vendorExtensions.x-group-parameters}}
448448
{{#allParams}}
449-
{{^required}}?{{/required}}{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
449+
{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
450450
{{/allParams}}
451451
{{#servers}}
452452
{{#-first}}
@@ -524,7 +524,7 @@ use {{invokerPackage}}\ObjectSerializer;
524524
public function {{operationId}}AsyncWithHttpInfo(
525525
{{^vendorExtensions.x-group-parameters}}
526526
{{#allParams}}
527-
${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
527+
{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
528528
{{/allParams}}
529529
{{#servers}}
530530
{{#-first}}
@@ -630,7 +630,7 @@ use {{invokerPackage}}\ObjectSerializer;
630630
public function {{operationId}}Request(
631631
{{^vendorExtensions.x-group-parameters}}
632632
{{#allParams}}
633-
${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
633+
{{vendorExtensions.x-php-param-type}} ${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}},
634634
{{/allParams}}
635635
{{#servers}}
636636
{{#-first}}

samples/client/echo_api/php-nextgen-streaming/src/Api/BodyApi.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ function ($response) {
633633
* @return PromiseInterface
634634
*/
635635
public function testBodyApplicationOctetstreamBinaryAsyncWithHttpInfo(
636-
$body = null,
636+
?\Psr\Http\Message\StreamInterface $body = null,
637637
string $contentType = self::contentTypes['testBodyApplicationOctetstreamBinary'][0]
638638
): PromiseInterface
639639
{
@@ -686,7 +686,7 @@ function ($exception) {
686686
* @return \GuzzleHttp\Psr7\Request
687687
*/
688688
public function testBodyApplicationOctetstreamBinaryRequest(
689-
$body = null,
689+
?\Psr\Http\Message\StreamInterface $body = null,
690690
string $contentType = self::contentTypes['testBodyApplicationOctetstreamBinary'][0]
691691
): Request
692692
{
@@ -949,7 +949,7 @@ function ($response) {
949949
* @return PromiseInterface
950950
*/
951951
public function testBodyMultipartFormdataArrayOfBinaryAsyncWithHttpInfo(
952-
$files,
952+
array $files,
953953
string $contentType = self::contentTypes['testBodyMultipartFormdataArrayOfBinary'][0]
954954
): PromiseInterface
955955
{
@@ -1002,7 +1002,7 @@ function ($exception) {
10021002
* @return \GuzzleHttp\Psr7\Request
10031003
*/
10041004
public function testBodyMultipartFormdataArrayOfBinaryRequest(
1005-
$files,
1005+
array $files,
10061006
string $contentType = self::contentTypes['testBodyMultipartFormdataArrayOfBinary'][0]
10071007
): Request
10081008
{
@@ -1278,7 +1278,7 @@ function ($response) {
12781278
* @return PromiseInterface
12791279
*/
12801280
public function testBodyMultipartFormdataSingleBinaryAsyncWithHttpInfo(
1281-
$my_file = null,
1281+
?\Psr\Http\Message\StreamInterface $my_file = null,
12821282
string $contentType = self::contentTypes['testBodyMultipartFormdataSingleBinary'][0]
12831283
): PromiseInterface
12841284
{
@@ -1331,7 +1331,7 @@ function ($exception) {
13311331
* @return \GuzzleHttp\Psr7\Request
13321332
*/
13331333
public function testBodyMultipartFormdataSingleBinaryRequest(
1334-
$my_file = null,
1334+
?\Psr\Http\Message\StreamInterface $my_file = null,
13351335
string $contentType = self::contentTypes['testBodyMultipartFormdataSingleBinary'][0]
13361336
): Request
13371337
{
@@ -1601,7 +1601,7 @@ function ($response) {
16011601
* @return PromiseInterface
16021602
*/
16031603
public function testEchoBodyAllOfPetAsyncWithHttpInfo(
1604-
$pet = null,
1604+
?\OpenAPI\Client\Model\Pet $pet = null,
16051605
string $contentType = self::contentTypes['testEchoBodyAllOfPet'][0]
16061606
): PromiseInterface
16071607
{
@@ -1654,7 +1654,7 @@ function ($exception) {
16541654
* @return \GuzzleHttp\Psr7\Request
16551655
*/
16561656
public function testEchoBodyAllOfPetRequest(
1657-
$pet = null,
1657+
?\OpenAPI\Client\Model\Pet $pet = null,
16581658
string $contentType = self::contentTypes['testEchoBodyAllOfPet'][0]
16591659
): Request
16601660
{
@@ -1745,7 +1745,7 @@ public function testEchoBodyAllOfPetRequest(
17451745
* @return string
17461746
*/
17471747
public function testEchoBodyFreeFormObjectResponseString(
1748-
?array $body = null,
1748+
array $body = null,
17491749
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
17501750
): string
17511751
{
@@ -1766,7 +1766,7 @@ public function testEchoBodyFreeFormObjectResponseString(
17661766
* @return array of string, HTTP status code, HTTP response headers (array of strings)
17671767
*/
17681768
public function testEchoBodyFreeFormObjectResponseStringWithHttpInfo(
1769-
?array $body = null,
1769+
array $body = null,
17701770
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
17711771
): array
17721772
{
@@ -1893,7 +1893,7 @@ public function testEchoBodyFreeFormObjectResponseStringWithHttpInfo(
18931893
* @return PromiseInterface
18941894
*/
18951895
public function testEchoBodyFreeFormObjectResponseStringAsync(
1896-
?array $body = null,
1896+
array $body = null,
18971897
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
18981898
): PromiseInterface
18991899
{
@@ -1917,7 +1917,7 @@ function ($response) {
19171917
* @return PromiseInterface
19181918
*/
19191919
public function testEchoBodyFreeFormObjectResponseStringAsyncWithHttpInfo(
1920-
$body = null,
1920+
array $body = null,
19211921
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
19221922
): PromiseInterface
19231923
{
@@ -1970,7 +1970,7 @@ function ($exception) {
19701970
* @return \GuzzleHttp\Psr7\Request
19711971
*/
19721972
public function testEchoBodyFreeFormObjectResponseStringRequest(
1973-
$body = null,
1973+
array $body = null,
19741974
string $contentType = self::contentTypes['testEchoBodyFreeFormObjectResponseString'][0]
19751975
): Request
19761976
{
@@ -2233,7 +2233,7 @@ function ($response) {
22332233
* @return PromiseInterface
22342234
*/
22352235
public function testEchoBodyPetAsyncWithHttpInfo(
2236-
$pet = null,
2236+
?\OpenAPI\Client\Model\Pet $pet = null,
22372237
string $contentType = self::contentTypes['testEchoBodyPet'][0]
22382238
): PromiseInterface
22392239
{
@@ -2286,7 +2286,7 @@ function ($exception) {
22862286
* @return \GuzzleHttp\Psr7\Request
22872287
*/
22882288
public function testEchoBodyPetRequest(
2289-
$pet = null,
2289+
?\OpenAPI\Client\Model\Pet $pet = null,
22902290
string $contentType = self::contentTypes['testEchoBodyPet'][0]
22912291
): Request
22922292
{
@@ -2549,7 +2549,7 @@ function ($response) {
25492549
* @return PromiseInterface
25502550
*/
25512551
public function testEchoBodyPetResponseStringAsyncWithHttpInfo(
2552-
$pet = null,
2552+
?\OpenAPI\Client\Model\Pet $pet = null,
25532553
string $contentType = self::contentTypes['testEchoBodyPetResponseString'][0]
25542554
): PromiseInterface
25552555
{
@@ -2602,7 +2602,7 @@ function ($exception) {
26022602
* @return \GuzzleHttp\Psr7\Request
26032603
*/
26042604
public function testEchoBodyPetResponseStringRequest(
2605-
$pet = null,
2605+
?\OpenAPI\Client\Model\Pet $pet = null,
26062606
string $contentType = self::contentTypes['testEchoBodyPetResponseString'][0]
26072607
): Request
26082608
{
@@ -2865,7 +2865,7 @@ function ($response) {
28652865
* @return PromiseInterface
28662866
*/
28672867
public function testEchoBodyStringEnumAsyncWithHttpInfo(
2868-
$body = null,
2868+
?string $body = null,
28692869
string $contentType = self::contentTypes['testEchoBodyStringEnum'][0]
28702870
): PromiseInterface
28712871
{
@@ -2918,7 +2918,7 @@ function ($exception) {
29182918
* @return \GuzzleHttp\Psr7\Request
29192919
*/
29202920
public function testEchoBodyStringEnumRequest(
2921-
$body = null,
2921+
?string $body = null,
29222922
string $contentType = self::contentTypes['testEchoBodyStringEnum'][0]
29232923
): Request
29242924
{
@@ -3181,7 +3181,7 @@ function ($response) {
31813181
* @return PromiseInterface
31823182
*/
31833183
public function testEchoBodyTagResponseStringAsyncWithHttpInfo(
3184-
$tag = null,
3184+
?\OpenAPI\Client\Model\Tag $tag = null,
31853185
string $contentType = self::contentTypes['testEchoBodyTagResponseString'][0]
31863186
): PromiseInterface
31873187
{
@@ -3234,7 +3234,7 @@ function ($exception) {
32343234
* @return \GuzzleHttp\Psr7\Request
32353235
*/
32363236
public function testEchoBodyTagResponseStringRequest(
3237-
$tag = null,
3237+
?\OpenAPI\Client\Model\Tag $tag = null,
32383238
string $contentType = self::contentTypes['testEchoBodyTagResponseString'][0]
32393239
): Request
32403240
{

samples/client/echo_api/php-nextgen-streaming/src/Api/FormApi.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ function ($response) {
328328
* @return PromiseInterface
329329
*/
330330
public function testFormIntegerBooleanStringAsyncWithHttpInfo(
331-
$integer_form = null,
332-
$boolean_form = null,
333-
$string_form = null,
331+
?int $integer_form = null,
332+
?bool $boolean_form = null,
333+
?string $string_form = null,
334334
string $contentType = self::contentTypes['testFormIntegerBooleanString'][0]
335335
): PromiseInterface
336336
{
@@ -385,9 +385,9 @@ function ($exception) {
385385
* @return \GuzzleHttp\Psr7\Request
386386
*/
387387
public function testFormIntegerBooleanStringRequest(
388-
$integer_form = null,
389-
$boolean_form = null,
390-
$string_form = null,
388+
?int $integer_form = null,
389+
?bool $boolean_form = null,
390+
?string $string_form = null,
391391
string $contentType = self::contentTypes['testFormIntegerBooleanString'][0]
392392
): Request
393393
{
@@ -657,7 +657,7 @@ function ($response) {
657657
* @return PromiseInterface
658658
*/
659659
public function testFormObjectMultipartAsyncWithHttpInfo(
660-
$marker,
660+
\OpenAPI\Client\Model\TestFormObjectMultipartRequestMarker $marker,
661661
string $contentType = self::contentTypes['testFormObjectMultipart'][0]
662662
): PromiseInterface
663663
{
@@ -710,7 +710,7 @@ function ($exception) {
710710
* @return \GuzzleHttp\Psr7\Request
711711
*/
712712
public function testFormObjectMultipartRequest(
713-
$marker,
713+
\OpenAPI\Client\Model\TestFormObjectMultipartRequestMarker $marker,
714714
string $contentType = self::contentTypes['testFormObjectMultipart'][0]
715715
): Request
716716
{
@@ -1011,12 +1011,12 @@ function ($response) {
10111011
* @return PromiseInterface
10121012
*/
10131013
public function testFormOneofAsyncWithHttpInfo(
1014-
$form1 = null,
1015-
$form2 = null,
1016-
$form3 = null,
1017-
$form4 = null,
1018-
$id = null,
1019-
$name = null,
1014+
?string $form1 = null,
1015+
?int $form2 = null,
1016+
?string $form3 = null,
1017+
?bool $form4 = null,
1018+
?int $id = null,
1019+
?string $name = null,
10201020
string $contentType = self::contentTypes['testFormOneof'][0]
10211021
): PromiseInterface
10221022
{
@@ -1074,12 +1074,12 @@ function ($exception) {
10741074
* @return \GuzzleHttp\Psr7\Request
10751075
*/
10761076
public function testFormOneofRequest(
1077-
$form1 = null,
1078-
$form2 = null,
1079-
$form3 = null,
1080-
$form4 = null,
1081-
$id = null,
1082-
$name = null,
1077+
?string $form1 = null,
1078+
?int $form2 = null,
1079+
?string $form3 = null,
1080+
?bool $form4 = null,
1081+
?int $id = null,
1082+
?string $name = null,
10831083
string $contentType = self::contentTypes['testFormOneof'][0]
10841084
): Request
10851085
{

samples/client/echo_api/php-nextgen-streaming/src/Api/HeaderApi.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ function ($response) {
336336
* @return PromiseInterface
337337
*/
338338
public function testHeaderIntegerBooleanStringEnumsAsyncWithHttpInfo(
339-
$integer_header = null,
340-
$boolean_header = null,
341-
$string_header = null,
342-
$enum_nonref_string_header = null,
343-
$enum_ref_string_header = null,
339+
?int $integer_header = null,
340+
?bool $boolean_header = null,
341+
?string $string_header = null,
342+
?string $enum_nonref_string_header = null,
343+
?\OpenAPI\Client\Model\StringEnumRef $enum_ref_string_header = null,
344344
string $contentType = self::contentTypes['testHeaderIntegerBooleanStringEnums'][0]
345345
): PromiseInterface
346346
{
@@ -397,11 +397,11 @@ function ($exception) {
397397
* @return \GuzzleHttp\Psr7\Request
398398
*/
399399
public function testHeaderIntegerBooleanStringEnumsRequest(
400-
$integer_header = null,
401-
$boolean_header = null,
402-
$string_header = null,
403-
$enum_nonref_string_header = null,
404-
$enum_ref_string_header = null,
400+
?int $integer_header = null,
401+
?bool $boolean_header = null,
402+
?string $string_header = null,
403+
?string $enum_nonref_string_header = null,
404+
?\OpenAPI\Client\Model\StringEnumRef $enum_ref_string_header = null,
405405
string $contentType = self::contentTypes['testHeaderIntegerBooleanStringEnums'][0]
406406
): Request
407407
{

samples/client/echo_api/php-nextgen-streaming/src/Api/PathApi.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ function ($response) {
329329
* @return PromiseInterface
330330
*/
331331
public function testsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathAsyncWithHttpInfo(
332-
$path_string,
333-
$path_integer,
334-
$enum_nonref_string_path,
335-
$enum_ref_string_path,
332+
string $path_string,
333+
int $path_integer,
334+
string $enum_nonref_string_path,
335+
\OpenAPI\Client\Model\StringEnumRef $enum_ref_string_path,
336336
string $contentType = self::contentTypes['testsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPath'][0]
337337
): PromiseInterface
338338
{
@@ -388,10 +388,10 @@ function ($exception) {
388388
* @return \GuzzleHttp\Psr7\Request
389389
*/
390390
public function testsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPathRequest(
391-
$path_string,
392-
$path_integer,
393-
$enum_nonref_string_path,
394-
$enum_ref_string_path,
391+
string $path_string,
392+
int $path_integer,
393+
string $enum_nonref_string_path,
394+
\OpenAPI\Client\Model\StringEnumRef $enum_ref_string_path,
395395
string $contentType = self::contentTypes['testsPathStringPathStringIntegerPathIntegerEnumNonrefStringPathEnumRefStringPath'][0]
396396
): Request
397397
{

0 commit comments

Comments
 (0)