[PHP] Bug deserialize default responses as a Model
Created by: ymilin
Description
I'm generating clients that make use of default
responses for other HTTP codes (implying an error). It's defined as a Model (code, message) in definitions
in the swagger file.
...
responses:
'200':
description: pet response
schema:
type: array
items:
$ref: '#/definitions/Pet'
default:
description: error payload
schema:
$ref: '#/definitions/ErrorModel'
...
After generating the client API with openapi-generator, when catching an ApiException
, the deserialized response object ApiException::responseObject
is empty (it is an an instance of ErrorModel
as expected, but with no content).
openapi-generator version
2.3.0
OpenAPI declaration file content or url
gist: minimal swagger.json example
Command line used for generation
java -jar bin/openapi-generator-cli.jar generate -i swagger.json \
-l php \
-o build/ \
--git-user-id "vendor" \
--git-repo-id "ws-clients" \
-DpackagePath=.,srcBasePath=lib,variableNamingConvention=camelCase,invokerPackage="MyCie\\Service",apiTests=false,apiDocs=false,modelTests=false,modelDocs=false
Steps to reproduce
With the generated client:
- try to make a request for a non-existing pet
- Expect
$e->getResponseObject()
to be a fully hydrated instance of ErrorModel - Got an empty instance of ErrorModel instead
$petApi = new PetApi();
try {
$pet = $api->getPetById(123);
...
} catch (ApiException $e) {
$errorModel = $e->getResponseObject();
print_r($errorModel); // empty ErrorModel
}
Suggest a fix/enhancement
I checked the generated code and the bug seem to be located in the catch
part of the {{operationId}}WithHttpInfo
methods:
try {
...
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = ObjectSerializer::deserialize(
$e->getResponseBody(),
'\MyCie\Service\Model\Pet',
$e->getResponseHeaders()
);
$e->setResponseObject($data);
break;
default:
$data = ObjectSerializer::deserialize(
$e->getResponseBody(), // <-- works with json_decode($e->getResponseBody())
'\MyCie\Service\Model\ErrorModel',
$e->getResponseHeaders()
);
$e->setResponseObject($data);
break;
}
throw $e;
}
We can see that there is a specific test for this in the try
part of same function in the api.mustache
file, line 184
So a possible fix would be doing something similar when deserializing for ApiException
objects.