swagger-api / swagger-api/swagger-codegen

[PHP] - Do not treat \stdClass as a Model.

Open
#7,327 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: PHP help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When using a generic type: "object" in my swagger file for a property and then setting that property such as:

$my_data = new \stdClass();
$my_data->hi = "world";
$client->setMyField($my_data);

The generator throws an error saying "no function ::getFormatters" because it is attempting to run that static function on the generic PHP standard class, which does not exist.

Swagger-codegen version

Version 2.3

Swagger declaration file content or url
{
  "definitions": {
    "Example": {
      "description": "This is an example for nested objects.",
      "properties": {
        "mapping_data": {
          "type": "object",
          "description": "Mapping data as an object"
        }
      }
    }
  }
}
Command line used for generation

java -jar swagger-codegen.jar generate -l php -o output-dir -i swagger.json

Steps to reproduce

Just run the swagger generator for php

Suggest a fix/enhancement

In ObjectSerialization#sanitizeForSerialization add this section above is_object check.

// SNIP!
elseif ($data instanceof \stdClass) {
			foreach ($data as $property => $value) {
				$data->$property = self::sanitizeForSerialization($value);
			}
			return $data;
		} 

I was going to create a patch when I had a chance.
We should also include a guard against circular references; at the same time, maybe we just let the stack overflow and leave that issue to the developer.

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 at ObjectSerialization#sanitizeForSerialization in the generated PHP client and reproduce the failure with the provided Swagger definition and java generation command. Confirm that a generic stdClass property can be serialized without the missing getFormatters error, while considering the noted circular-reference concern.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.