Skip to content

Commit d318752

Browse files
authored
[php][php-nextgen] fix return type if empty and non-empty responses are mixed (#22322)
1 parent 86ff873 commit d318752

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
188188
for (CodegenOperation operation : operations.getOperation()) {
189189
Set<String> phpReturnTypeOptions = new LinkedHashSet<>();
190190
Set<String> docReturnTypeOptions = new LinkedHashSet<>();
191+
boolean hasEmptyResponse = false;
191192

192193
for (CodegenResponse response : operation.responses) {
193194
if (response.dataType != null) {
@@ -200,6 +201,8 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
200201

201202
phpReturnTypeOptions.add(returnType);
202203
docReturnTypeOptions.add(response.dataType);
204+
} else {
205+
hasEmptyResponse = true;
203206
}
204207
}
205208

@@ -208,9 +211,16 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
208211
operation.vendorExtensions.putIfAbsent("x-php-return-type", "void");
209212
operation.vendorExtensions.putIfAbsent("x-php-doc-return-type", "void");
210213
} else {
214+
String phpReturnType = String.join("|", phpReturnTypeOptions);
215+
String docReturnType = String.join("|", docReturnTypeOptions);
216+
if (hasEmptyResponse) {
217+
phpReturnType = "?" + phpReturnType;
218+
docReturnType = docReturnType + "|null";
219+
}
220+
211221
operation.vendorExtensions.putIfAbsent("x-php-return-type-is-void", false);
212-
operation.vendorExtensions.putIfAbsent("x-php-return-type", String.join("|", phpReturnTypeOptions));
213-
operation.vendorExtensions.putIfAbsent("x-php-doc-return-type", String.join("|", docReturnTypeOptions));
222+
operation.vendorExtensions.putIfAbsent("x-php-return-type", phpReturnType);
223+
operation.vendorExtensions.putIfAbsent("x-php-doc-return-type", docReturnType);
214224
}
215225

216226
for (CodegenParameter param : operation.allParams) {

samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Api/FakeApi.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,12 @@ public function fakeBigDecimalMapRequest(
470470
*
471471
* @throws ApiException on non-2xx response or if the response body is not in the expected format
472472
* @throws InvalidArgumentException
473-
* @return \OpenAPI\Client\Model\Error
473+
* @return \OpenAPI\Client\Model\Error|null
474474
*/
475475
public function fakeDeletePet(
476476
string $pet_id,
477477
string $contentType = self::contentTypes['fakeDeletePet'][0]
478-
): \OpenAPI\Client\Model\Error
478+
): ?\OpenAPI\Client\Model\Error
479479
{
480480
list($response) = $this->fakeDeletePetWithHttpInfo($pet_id, $contentType);
481481
return $response;
@@ -3225,12 +3225,12 @@ public function fakeWith400And4xxRangeResponseEndpointRequest(
32253225
*
32263226
* @throws ApiException on non-2xx response or if the response body is not in the expected format
32273227
* @throws InvalidArgumentException
3228-
* @return \OpenAPI\Client\Model\Pet
3228+
* @return \OpenAPI\Client\Model\Pet|null
32293229
*/
32303230
public function fakeWith400And4xxRangeResponseNo4xxDatatypeEndpoint(
32313231
\OpenAPI\Client\Model\Pet $pet,
32323232
string $contentType = self::contentTypes['fakeWith400And4xxRangeResponseNo4xxDatatypeEndpoint'][0]
3233-
): \OpenAPI\Client\Model\Pet
3233+
): ?\OpenAPI\Client\Model\Pet
32343234
{
32353235
list($response) = $this->fakeWith400And4xxRangeResponseNo4xxDatatypeEndpointWithHttpInfo($pet, $contentType);
32363236
return $response;
@@ -4094,12 +4094,12 @@ public function fakeWith4xxRangeResponseEndpointRequest(
40944094
*
40954095
* @throws ApiException on non-2xx response or if the response body is not in the expected format
40964096
* @throws InvalidArgumentException
4097-
* @return \OpenAPI\Client\Model\Pet
4097+
* @return \OpenAPI\Client\Model\Pet|null
40984098
*/
40994099
public function fakeWith4xxRangeResponseNo4xxDatatypeEndpoint(
41004100
\OpenAPI\Client\Model\Pet $pet,
41014101
string $contentType = self::contentTypes['fakeWith4xxRangeResponseNo4xxDatatypeEndpoint'][0]
4102-
): \OpenAPI\Client\Model\Pet
4102+
): ?\OpenAPI\Client\Model\Pet
41034103
{
41044104
list($response) = $this->fakeWith4xxRangeResponseNo4xxDatatypeEndpointWithHttpInfo($pet, $contentType);
41054105
return $response;

samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Api/PetApi.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,12 @@ public function deletePetRequest(
808808
*
809809
* @throws ApiException on non-2xx response or if the response body is not in the expected format
810810
* @throws InvalidArgumentException
811-
* @return \OpenAPI\Client\Model\Pet[]
811+
* @return \OpenAPI\Client\Model\Pet[]|null
812812
*/
813813
public function findPetsByStatus(
814814
array $status,
815815
string $contentType = self::contentTypes['findPetsByStatus'][0]
816-
): array
816+
): ?array
817817
{
818818
list($response) = $this->findPetsByStatusWithHttpInfo($status, $contentType);
819819
return $response;
@@ -1093,13 +1093,13 @@ public function findPetsByStatusRequest(
10931093
*
10941094
* @throws ApiException on non-2xx response or if the response body is not in the expected format
10951095
* @throws InvalidArgumentException
1096-
* @return \OpenAPI\Client\Model\Pet[]
1096+
* @return \OpenAPI\Client\Model\Pet[]|null
10971097
* @deprecated
10981098
*/
10991099
public function findPetsByTags(
11001100
array $tags,
11011101
string $contentType = self::contentTypes['findPetsByTags'][0]
1102-
): array
1102+
): ?array
11031103
{
11041104
list($response) = $this->findPetsByTagsWithHttpInfo($tags, $contentType);
11051105
return $response;
@@ -1383,12 +1383,12 @@ public function findPetsByTagsRequest(
13831383
*
13841384
* @throws ApiException on non-2xx response or if the response body is not in the expected format
13851385
* @throws InvalidArgumentException
1386-
* @return \OpenAPI\Client\Model\Pet
1386+
* @return \OpenAPI\Client\Model\Pet|null
13871387
*/
13881388
public function getPetById(
13891389
int $pet_id,
13901390
string $contentType = self::contentTypes['getPetById'][0]
1391-
): \OpenAPI\Client\Model\Pet
1391+
): ?\OpenAPI\Client\Model\Pet
13921392
{
13931393
list($response) = $this->getPetByIdWithHttpInfo($pet_id, $contentType);
13941394
return $response;

samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Api/StoreApi.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,12 +636,12 @@ public function getInventoryRequest(
636636
*
637637
* @throws ApiException on non-2xx response or if the response body is not in the expected format
638638
* @throws InvalidArgumentException
639-
* @return \OpenAPI\Client\Model\Order
639+
* @return \OpenAPI\Client\Model\Order|null
640640
*/
641641
public function getOrderById(
642642
int $order_id,
643643
string $contentType = self::contentTypes['getOrderById'][0]
644-
): \OpenAPI\Client\Model\Order
644+
): ?\OpenAPI\Client\Model\Order
645645
{
646646
list($response) = $this->getOrderByIdWithHttpInfo($order_id, $contentType);
647647
return $response;
@@ -922,12 +922,12 @@ public function getOrderByIdRequest(
922922
*
923923
* @throws ApiException on non-2xx response or if the response body is not in the expected format
924924
* @throws InvalidArgumentException
925-
* @return \OpenAPI\Client\Model\Order
925+
* @return \OpenAPI\Client\Model\Order|null
926926
*/
927927
public function placeOrder(
928928
\OpenAPI\Client\Model\Order $order,
929929
string $contentType = self::contentTypes['placeOrder'][0]
930-
): \OpenAPI\Client\Model\Order
930+
): ?\OpenAPI\Client\Model\Order
931931
{
932932
list($response) = $this->placeOrderWithHttpInfo($order, $contentType);
933933
return $response;

samples/client/petstore/php-nextgen/OpenAPIClient-php/src/Api/UserApi.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,12 +1081,12 @@ public function deleteUserRequest(
10811081
*
10821082
* @throws ApiException on non-2xx response or if the response body is not in the expected format
10831083
* @throws InvalidArgumentException
1084-
* @return \OpenAPI\Client\Model\User
1084+
* @return \OpenAPI\Client\Model\User|null
10851085
*/
10861086
public function getUserByName(
10871087
string $username,
10881088
string $contentType = self::contentTypes['getUserByName'][0]
1089-
): \OpenAPI\Client\Model\User
1089+
): ?\OpenAPI\Client\Model\User
10901090
{
10911091
list($response) = $this->getUserByNameWithHttpInfo($username, $contentType);
10921092
return $response;
@@ -1362,13 +1362,13 @@ public function getUserByNameRequest(
13621362
*
13631363
* @throws ApiException on non-2xx response or if the response body is not in the expected format
13641364
* @throws InvalidArgumentException
1365-
* @return string
1365+
* @return string|null
13661366
*/
13671367
public function loginUser(
13681368
string $username,
13691369
string $password,
13701370
string $contentType = self::contentTypes['loginUser'][0]
1371-
): string
1371+
): ?string
13721372
{
13731373
list($response) = $this->loginUserWithHttpInfo($username, $password, $contentType);
13741374
return $response;

0 commit comments

Comments
 (0)