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 Client] Nested models do not deserialize correctly #635

Closed
who opened this issue Apr 14, 2015 · 6 comments
Closed

[PHP Client] Nested models do not deserialize correctly #635

who opened this issue Apr 14, 2015 · 6 comments
Milestone

Comments

@who
Copy link
Contributor

who commented Apr 14, 2015

If you have nested models in your swagger spec, the nested object does not deserialize correctly.

Given this Swagger 2.0 spec snippet:

...
  "definitions": {
    "BookAuthor": {
      "properties": {
        "name": {
          "type": "string"
        }
      }
    },
    "Book": {
      "properties": {
        "name": {
          "type": "string"
        },
        "bookAuthor": {
          "$ref": "#/definitions/BookAuthor"
        }
      }
    }
  }
...

The PHP client will yield this JSON when returning Book responses.

{"name":"My Book Name","book_author":null}

The fix for this appears to be in the deserialize() method of APIClient.Mustache

Here's the apparent fix:

Change this:

...
      foreach ($instance::$swaggerTypes as $property => $type) {
        if (isset($data->$property)) {
          $original_property_name = $instance::$attributeMap[$property];
          $instance->$property = self::deserialize($data->$original_property_name, $type);
        }
      }
...

To this:

...
      foreach ($instance::$swaggerTypes as $property => $type) {
        $original_property_name = $instance::$attributeMap[$property];
        if (isset($original_property_name)) {
          $instance->$property = self::deserialize($data->$original_property_name, $type);
        }
      }
...
@fehguy
Copy link
Contributor

fehguy commented Apr 14, 2015

Thanks. Yes, @who, there is currently no support for nested schemas right now. It will be added, but for now please consider using $ref syntax to flatten them out.

@fehguy fehguy added this to the v2.1.0 milestone Apr 14, 2015
@who
Copy link
Contributor Author

who commented Apr 14, 2015

@fehguy

Using the code that I provided, I am able to deserialize nested models (at least 2 levels deep) correctly.

Would you be willing to take this as a PR ?

@fehguy
Copy link
Contributor

fehguy commented Apr 14, 2015

@who i stand corrected, the snipped you put is valid. I read it as there was a schema in a property which is not currently supported in the codegen project.

So yes, please do send a PR. Apologies for the misidentification of the issue.

@who
Copy link
Contributor Author

who commented Apr 14, 2015

@fehguy

I've added it to my existing PHP PR where I've fixed another issue as well.

See PR #630

@fehguy
Copy link
Contributor

fehguy commented Apr 14, 2015

great, thank you.

@who
Copy link
Contributor Author

who commented Apr 22, 2015

Closing this, as it's fixed now.

@who who closed this as completed Apr 22, 2015
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

2 participants