swagger-api/swagger-codegen

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

Open

#7327 opened on Jan 5, 2018

View on GitHub
 (2 comments) (0 reactions) (0 assignees)HTML (12,701 stars) (5,474 forks)batch import
Client: PHPIssue: Bughelp wanted

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