swagger-api / swagger-api/swagger-codegen

[PHP] Object deserialization returns array instead of object.

Open
#9,346 0 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

The "object" response type actually returns an array in PHP.

This can mostly be worked around, with the exception that static analyzers break because the docblock return type (object) does not match the actual return type (array).

I believe it is due to these lines:
https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache#L240-L241

        } elseif ($class === 'object') {
            settype($data, 'array');

The generated code has this in the docblock:

* @return object

But the resulting return type is an array.

Swagger-codegen version

2.4.2

Related issues/PRs

This is essentially a duplicate of #5553, but I wanted to add that static analyzers fail when ran against the generated code.

Suggest a fix/enhancement

Returning a stdClass seems ideal here (IMO), but altering the docblock to indicate the array return type would also fix the static analysis issue.

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 in modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache at the referenced deserialization branch. Compare the generated return value with its @return object declaration and review related issue #5553 before choosing the intended behavior. Done means generated PHP code returns a value consistent with its documented type and no longer breaks static analysis.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.