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
  • #8394
Closed
Open
Issue created Jan 09, 2021 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][php] json_decode missing when response is object

Created by: ybelenko

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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 I call API via generated PHP client I got encoded json string instead of decoded object. Response looks like:

["{\"foobar\":\"foobaz\"}"]
openapi-generator version

5.0.0-beta

OpenAPI declaration file content or url
paths: 
  '/objects/{id}':
    parameters:
      - name: id
        in: path
        required: true
    get:
      operationId: getSingleObjectById
      responses:
        '200':
          description: ok
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FreeFormObject'
components:
  schemas:
    FreeFormObject:
      type: object
Generation Details
generate -i \"spec.yaml\" -g php -o \"api-client-sdk\"
Steps to reproduce

Send request via generated SDK to staged server /objects/1 where response is correct JSON item. The response will be json encoded string instead of object.

Related issues/PRs

#6566 Seems close but not really. I'm ok with response as object instead of model. It's just json_decode execution missed.

Suggest a fix
diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
index 58200ac713..42fd2658d1 100644
--- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
+++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache
@@ -294,6 +294,7 @@ class ObjectSerializer
         }
 
         if ($class === 'object') {
+            $data = is_string($data) ? json_decode($data) : $data;
             settype($data, 'array');
             return $data;
         }

I don't know why json_decode executes based on string check and doesn't check Content-Type header but it's a quick fix.

Assignee
Assign to
Time tracking