Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Souvent22 opened this issue Jan 5, 2018 · 2 comments
Open

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

Souvent22 opened this issue Jan 5, 2018 · 2 comments

Comments

@Souvent22
Copy link

Souvent22 commented Jan 5, 2018

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.

@wing328
Copy link
Contributor

wing328 commented Jan 7, 2018

@Souvent22 thanks for reporting the issue. Looking forward to the PR.

@leberknecht
Copy link

Site note, maybe someone finds this useful:
I have a similar problem where serializing a member that has an stdClass on it results in the error

Call to undefined method stdClass::swaggerFormats()

As a workaround i fixed it by adding this snippet in the beginning of sanitizeForSerialization

if ($data instanceof \stdClass) {
    $data = get_object_vars($data);
}

I think thats more or less doing the same like your solution. Im struggeling a bit with understanding in what cases swagger-codegen will create assoc-arrays and when it will create stdClasses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants