swagger-api / swagger-api/swagger-codegen
[PHP] Bug deserialize default responses as a Model
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
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 swagger-codegen, 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).
Swagger-codegen version
2.3.0
Swagger declaration file content or url
gist: minimal swagger.json example
Command line used for generation
java -jar bin/swagger-codegen-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 164
So a possible fix would be doing something similar when deserializing for ApiException objects.
I can work on a PR for this, as I really need it, but I have no experience with mustache templates and will not be able to test edge cases.
So I would need help for testing my PR.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/swagger-codegen/src/main/resources/php/api.mustache, comparing the response deserialization in the try path around line 164 with the ApiException catch path. Reproduce the generated PHP client using the linked minimal swagger.json example and verify that a default error response hydrates ErrorModel in getResponseObject(), including the generated client behavior shown in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100