swagger-api / swagger-api/swagger-codegen

[PHP] Bug deserialize default responses as a Model

Open
#7,544 3 comments 0 reactions 0 assignees View on GitHub

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

https://github.com/swagger-api/swagger-codegen/blob/36f69a034d2c6503b3e3cee00d10ab8944cc5be6/modules/swagger-codegen/src/main/resources/php/api.mustache#L163-L166

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.