Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #756
Closed
Open
Issue created Aug 07, 2018 by Administrator@rootContributor

[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

https://github.com/swagger-api/swagger-codegen/blob/616f8c17cfb7c44a3ccf7b6d55fb26ec6280213f/modules/swagger-codegen/src/main/resources/php/api.mustache#L184-188

So a possible fix would be doing something similar when deserializing for ApiException objects.

Assignee
Assign to
Time tracking