-
Notifications
You must be signed in to change notification settings - Fork 649
Support polymorphism in response deserialization #746
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
base: master
Are you sure you want to change the base?
Conversation
5cb1c12
to
859e15d
Compare
$data = is_string($data) ? json_decode($data) : $data; | ||
if (is_array($data)) { | ||
$data = (object)$data; | ||
} | ||
|
||
if (is_object($data) && method_exists($class, 'fromAssocArray')) { | ||
return $class::fromAssocArray(self::objectToArray($data)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the model defined in OpenAPI is specified as the target class for deserialization, the process passes through here. The variable $data may contain a JSON-formatted string, an array, or an stdClass object. Therefore, we first standardize it to stdClass at this point (this has already implemented in original template).
In this PR, we recursively convert the stdClass object into an associative array and then deserialize it into $class using the fromAssocArray function, which is also used for webhook deserialization. This function supports polymorphism.
"usageCondition": "Valid at all stores", | ||
"reward": { | ||
"type": "cashback", | ||
"type": "cashBack", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for correcting my typo 😅
"height": 1686 | ||
}, | ||
"action": { | ||
"type": "postback", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: what happens when type: unknown
coms to this library? Client should not be broken even if type:unknown is came.
Releated to #728
Summary
This change introduces polymorphism support in API responses.
⚠️ This includes breaking changes.
As-Is
Polymorphic types in responses were not handled correctly.
As a result, all responses were deserialized into the base class, making it impossible to access subclass-specific properties.
To-Be
Polymorphic types in responses are now properly supported.
Each response is deserialized into the correct subclass, allowing subclass properties to be accessed safely.
This change applies to all API responses, not just this specific endpoint.
If your existing code depends on the base class types, you will need to update it accordingly.
You can now directly access subclass-specific properties when polymorphism is involved.
Implementation Details
The serializers are auto-generated by openapi-generator.
This fix was implemented by customizing the OpenAPI generator templates.
Not Requiring Review
openapi-generator-cli author template -g php -o ./default-templates-php
(identical to the upstream defaults)