Skip to content

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

Open
@Souvent22

Description

@Souvent22
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions