Skip to content

Conversation

eucyt
Copy link
Contributor

@eucyt eucyt commented Oct 9, 2025

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.

$api = new MessagingApiApi($client);
$richMenuListResponse = $api->getRichMenuList();

// Even if type=postback, it becomes an instance of Action, not PostbackAction
$action = $richMenuListResponse->getRichmenus()[0]->getAreas()[0]->getAction();
// Error: Call to undefined method LINE\Clients\MessagingApi\Model\Action::getData()
$data = $action->getData();
// or simply null
$data = $action["data"];

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.

$api = new MessagingApiApi($client);
$richMenuListResponse = $api->getRichMenuList();

// If type=postback, it will now become an instance of PostbackAction
$action = $richMenuListResponse->getRichmenus()[0]->getAreas()[0]->getAction();
// You can now access subclass-specific properties
$data = $action->getData();
$data = $action["data"];

Implementation Details

The serializers are auto-generated by openapi-generator.
This fix was implemented by customizing the OpenAPI generator templates.

  • 216a987 : Modified generator templates
  • b174605 : Updated related tests

Not Requiring Review

  • 43abd86 : Added the default template files generated by
    openapi-generator-cli author template -g php -o ./default-templates-php
    (identical to the upstream defaults)
  • 48e38a6 , 859e15d : Updated auto-generated files

@eucyt eucyt force-pushed the feature/use-polymorphism branch from 5cb1c12 to 859e15d Compare October 9, 2025 06:33
Comment on lines +506 to +514
$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));
}
Copy link
Contributor Author

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.

@eucyt eucyt self-assigned this Oct 9, 2025
@eucyt eucyt requested a review from a team October 9, 2025 07:41
@eucyt eucyt marked this pull request as ready for review October 9, 2025 07:41
"usageCondition": "Valid at all stores",
"reward": {
"type": "cashback",
"type": "cashBack",
Copy link
Contributor

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",
Copy link
Contributor

@Yang-33 Yang-33 Oct 10, 2025

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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants