-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When you have a path that either returns a 204 response (without any body) or any other response code with a body, a type error is thrown due to the return type hint not being nullable.
openapi-generator version
7.13.0
OpenAPI declaration file content or url
openapi: 3.0.0
paths:
"/pet":
delete:
responses:
"204":
description: Pet deleted
"404":
description: Pet not found
content:
application/json:
schema:
type: object
properties:
error:
type: stringGeneration Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:v7.13.0 generate \
-i https://gist.githubusercontent.com/imba28/cc57b1589b084a689958869fc5b3a34f/raw/90aacb27985765c35daf3118c2720859756b74a6/spec.yaml \
-g php-nextgen \
-o /local/out/php
Steps to reproduce
use OpenAPI\Client\Api\DefaultApi;
use OpenAPI\Client\Configuration;
require 'vendor/autoload.php';
$config = new Configuration();
$config->setHost('https://httpstat.us/204');
$api = new DefaultApi(config: $config);
$api->petDelete();Expected output
I would expect the script to finish without any errors since the API spec is valid.
Actual output
Fatal error: Uncaught TypeError: OpenAPI\Client\Api\DefaultApi::petDelete(): Return value must be of type OpenAPI\Client\Model\PetDelete404Response, null returned in /var/www/html/src/Api/DefaultApi.php:142
Stack trace:
#0 /var/www/html/test.php(13): OpenAPI\Client\Api\DefaultApi->petDelete()
#1 {main}
thrown in /var/www/html/src/Api/DefaultApi.php on line 142
The cause of the type error seems to lie within the petDeleteWithHttpInfo method. The first element of its return value is null when dealing with 204 responses. The type hint of petDelete doesn't reflect that yet.
public function petDelete(
string $contentType = self::contentTypes['petDelete'][0]
): \OpenAPI\Client\Model\PetDelete404Response
{
list($response) = $this->petDeleteWithHttpInfo($contentType);
return $response;
}
public function petDeleteWithHttpInfo(
string $contentType = self::contentTypes['petDelete'][0]
): array
{
$request = $this->petDeleteRequest($contentType);
try {
// code omitted...
return [null, $statusCode, $response->getHeaders()]; // HTTP 204 => body is null
} catch (ApiException $e) {
switch ($e->getCode()) {
case 404:
$data = ObjectSerializer::deserialize(
$e->getResponseBody(),
'OpenAPI\Client\Model\PetDelete404Response',
$e->getResponseHeaders()
);
$e->setResponseObject($data);
throw $e;
}
throw $e;
}
}Related issues/PRs
Suggest a fix
HTTP 204 responses seem to be an edge case which https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpNextgenClientCodegen.java#L193 doesn't handle yet.
Maybe the code generator could include null as a return type if response.dataType != null || operation.responses.size() > 1 . Furthermore, it might be sufficient to only consider a subset of the responses since any non-2xx status code triggers an exception.