Skip to content

Commit b4b857c

Browse files
authored
Support polymorphism in response deserialization (#746)
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. ```php $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. When the discriminator is unknown, it is deserialized into the parent class, and no error occurs. ```php $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
1 parent c6d2dc0 commit b4b857c

16 files changed

+1325
-22
lines changed

docs/classes/LINE-Clients-ChannelAccessToken-ObjectSerializer.html

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ <h4 id="toc-methods">
342342
</dt>
343343
<dd>Checks if a value is empty, based on its OpenAPI type.</dd>
344344

345+
<dt class="phpdocumentor-table-of-contents__entry -method -private">
346+
<a class="" href="classes/LINE-Clients-ChannelAccessToken-ObjectSerializer.html#method_objectToArray">objectToArray()</a>
347+
<span>
348+
&nbsp;: mixed </span>
349+
</dt>
350+
345351
</dl>
346352

347353

@@ -415,7 +421,7 @@ <h4 class="phpdocumentor-element__name" id="method_buildQuery">
415421
<aside class="phpdocumentor-element-found-in">
416422
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/channel-access-token/lib/ObjectSerializer.php"><a href="files/src-clients-channel-access-token-lib-objectserializer.html"><abbr title="src/clients/channel-access-token/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
417423
:
418-
<span class="phpdocumentor-element-found-in__line">586</span>
424+
<span class="phpdocumentor-element-found-in__line">590</span>
419425

420426
</aside>
421427

@@ -1283,6 +1289,50 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
12831289

12841290
</section>
12851291

1292+
</article>
1293+
<article
1294+
class="phpdocumentor-element
1295+
-method
1296+
-private
1297+
-static "
1298+
>
1299+
<h4 class="phpdocumentor-element__name" id="method_objectToArray">
1300+
objectToArray()
1301+
<a href="classes/LINE-Clients-ChannelAccessToken-ObjectSerializer.html#method_objectToArray" class="headerlink"><i class="fas fa-link"></i></a>
1302+
1303+
</h4>
1304+
<aside class="phpdocumentor-element-found-in">
1305+
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/channel-access-token/lib/ObjectSerializer.php"><a href="files/src-clients-channel-access-token-lib-objectserializer.html"><abbr title="src/clients/channel-access-token/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
1306+
:
1307+
<span class="phpdocumentor-element-found-in__line">638</span>
1308+
1309+
</aside>
1310+
1311+
1312+
<code class="phpdocumentor-code phpdocumentor-signature ">
1313+
<span class="phpdocumentor-signature__visibility">private</span>
1314+
<span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__name">objectToArray</span><span>(</span><span class="phpdocumentor-signature__argument"><span class="phpdocumentor-signature__argument__return-type">mixed&nbsp;</span><span class="phpdocumentor-signature__argument__name">$obj</span></span><span>)</span><span> : </span><span class="phpdocumentor-signature__response_type">mixed</span></code>
1315+
1316+
<div class="phpdocumentor-label-line">
1317+
</div>
1318+
1319+
1320+
<h5 class="phpdocumentor-argument-list__heading">Parameters</h5>
1321+
<dl class="phpdocumentor-argument-list">
1322+
<dt class="phpdocumentor-argument-list__entry">
1323+
<span class="phpdocumentor-signature__argument__name">$obj</span>
1324+
: <span class="phpdocumentor-signature__argument__return-type">mixed</span>
1325+
</dt>
1326+
<dd class="phpdocumentor-argument-list__definition">
1327+
1328+
</dd>
1329+
</dl>
1330+
1331+
1332+
1333+
1334+
1335+
12861336
</article>
12871337
</section>
12881338

@@ -1420,6 +1470,7 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
14201470
<li class=""><a href="classes/LINE-Clients-ChannelAccessToken-ObjectSerializer.html#method_toQueryValue">toQueryValue()</a></li>
14211471
<li class=""><a href="classes/LINE-Clients-ChannelAccessToken-ObjectSerializer.html#method_toString">toString()</a></li>
14221472
<li class=""><a href="classes/LINE-Clients-ChannelAccessToken-ObjectSerializer.html#method_isEmptyValue">isEmptyValue()</a></li>
1473+
<li class=""><a href="classes/LINE-Clients-ChannelAccessToken-ObjectSerializer.html#method_objectToArray">objectToArray()</a></li>
14231474
</ul>
14241475
</li>
14251476
</ul>

docs/classes/LINE-Clients-Insight-ObjectSerializer.html

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ <h4 id="toc-methods">
342342
</dt>
343343
<dd>Checks if a value is empty, based on its OpenAPI type.</dd>
344344

345+
<dt class="phpdocumentor-table-of-contents__entry -method -private">
346+
<a class="" href="classes/LINE-Clients-Insight-ObjectSerializer.html#method_objectToArray">objectToArray()</a>
347+
<span>
348+
&nbsp;: mixed </span>
349+
</dt>
350+
345351
</dl>
346352

347353

@@ -415,7 +421,7 @@ <h4 class="phpdocumentor-element__name" id="method_buildQuery">
415421
<aside class="phpdocumentor-element-found-in">
416422
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/insight/lib/ObjectSerializer.php"><a href="files/src-clients-insight-lib-objectserializer.html"><abbr title="src/clients/insight/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
417423
:
418-
<span class="phpdocumentor-element-found-in__line">586</span>
424+
<span class="phpdocumentor-element-found-in__line">590</span>
419425

420426
</aside>
421427

@@ -1283,6 +1289,50 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
12831289

12841290
</section>
12851291

1292+
</article>
1293+
<article
1294+
class="phpdocumentor-element
1295+
-method
1296+
-private
1297+
-static "
1298+
>
1299+
<h4 class="phpdocumentor-element__name" id="method_objectToArray">
1300+
objectToArray()
1301+
<a href="classes/LINE-Clients-Insight-ObjectSerializer.html#method_objectToArray" class="headerlink"><i class="fas fa-link"></i></a>
1302+
1303+
</h4>
1304+
<aside class="phpdocumentor-element-found-in">
1305+
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/insight/lib/ObjectSerializer.php"><a href="files/src-clients-insight-lib-objectserializer.html"><abbr title="src/clients/insight/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
1306+
:
1307+
<span class="phpdocumentor-element-found-in__line">638</span>
1308+
1309+
</aside>
1310+
1311+
1312+
<code class="phpdocumentor-code phpdocumentor-signature ">
1313+
<span class="phpdocumentor-signature__visibility">private</span>
1314+
<span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__name">objectToArray</span><span>(</span><span class="phpdocumentor-signature__argument"><span class="phpdocumentor-signature__argument__return-type">mixed&nbsp;</span><span class="phpdocumentor-signature__argument__name">$obj</span></span><span>)</span><span> : </span><span class="phpdocumentor-signature__response_type">mixed</span></code>
1315+
1316+
<div class="phpdocumentor-label-line">
1317+
</div>
1318+
1319+
1320+
<h5 class="phpdocumentor-argument-list__heading">Parameters</h5>
1321+
<dl class="phpdocumentor-argument-list">
1322+
<dt class="phpdocumentor-argument-list__entry">
1323+
<span class="phpdocumentor-signature__argument__name">$obj</span>
1324+
: <span class="phpdocumentor-signature__argument__return-type">mixed</span>
1325+
</dt>
1326+
<dd class="phpdocumentor-argument-list__definition">
1327+
1328+
</dd>
1329+
</dl>
1330+
1331+
1332+
1333+
1334+
1335+
12861336
</article>
12871337
</section>
12881338

@@ -1420,6 +1470,7 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
14201470
<li class=""><a href="classes/LINE-Clients-Insight-ObjectSerializer.html#method_toQueryValue">toQueryValue()</a></li>
14211471
<li class=""><a href="classes/LINE-Clients-Insight-ObjectSerializer.html#method_toString">toString()</a></li>
14221472
<li class=""><a href="classes/LINE-Clients-Insight-ObjectSerializer.html#method_isEmptyValue">isEmptyValue()</a></li>
1473+
<li class=""><a href="classes/LINE-Clients-Insight-ObjectSerializer.html#method_objectToArray">objectToArray()</a></li>
14231474
</ul>
14241475
</li>
14251476
</ul>

docs/classes/LINE-Clients-Liff-ObjectSerializer.html

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ <h4 id="toc-methods">
342342
</dt>
343343
<dd>Checks if a value is empty, based on its OpenAPI type.</dd>
344344

345+
<dt class="phpdocumentor-table-of-contents__entry -method -private">
346+
<a class="" href="classes/LINE-Clients-Liff-ObjectSerializer.html#method_objectToArray">objectToArray()</a>
347+
<span>
348+
&nbsp;: mixed </span>
349+
</dt>
350+
345351
</dl>
346352

347353

@@ -415,7 +421,7 @@ <h4 class="phpdocumentor-element__name" id="method_buildQuery">
415421
<aside class="phpdocumentor-element-found-in">
416422
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/liff/lib/ObjectSerializer.php"><a href="files/src-clients-liff-lib-objectserializer.html"><abbr title="src/clients/liff/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
417423
:
418-
<span class="phpdocumentor-element-found-in__line">586</span>
424+
<span class="phpdocumentor-element-found-in__line">590</span>
419425

420426
</aside>
421427

@@ -1283,6 +1289,50 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
12831289

12841290
</section>
12851291

1292+
</article>
1293+
<article
1294+
class="phpdocumentor-element
1295+
-method
1296+
-private
1297+
-static "
1298+
>
1299+
<h4 class="phpdocumentor-element__name" id="method_objectToArray">
1300+
objectToArray()
1301+
<a href="classes/LINE-Clients-Liff-ObjectSerializer.html#method_objectToArray" class="headerlink"><i class="fas fa-link"></i></a>
1302+
1303+
</h4>
1304+
<aside class="phpdocumentor-element-found-in">
1305+
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/liff/lib/ObjectSerializer.php"><a href="files/src-clients-liff-lib-objectserializer.html"><abbr title="src/clients/liff/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
1306+
:
1307+
<span class="phpdocumentor-element-found-in__line">638</span>
1308+
1309+
</aside>
1310+
1311+
1312+
<code class="phpdocumentor-code phpdocumentor-signature ">
1313+
<span class="phpdocumentor-signature__visibility">private</span>
1314+
<span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__name">objectToArray</span><span>(</span><span class="phpdocumentor-signature__argument"><span class="phpdocumentor-signature__argument__return-type">mixed&nbsp;</span><span class="phpdocumentor-signature__argument__name">$obj</span></span><span>)</span><span> : </span><span class="phpdocumentor-signature__response_type">mixed</span></code>
1315+
1316+
<div class="phpdocumentor-label-line">
1317+
</div>
1318+
1319+
1320+
<h5 class="phpdocumentor-argument-list__heading">Parameters</h5>
1321+
<dl class="phpdocumentor-argument-list">
1322+
<dt class="phpdocumentor-argument-list__entry">
1323+
<span class="phpdocumentor-signature__argument__name">$obj</span>
1324+
: <span class="phpdocumentor-signature__argument__return-type">mixed</span>
1325+
</dt>
1326+
<dd class="phpdocumentor-argument-list__definition">
1327+
1328+
</dd>
1329+
</dl>
1330+
1331+
1332+
1333+
1334+
1335+
12861336
</article>
12871337
</section>
12881338

@@ -1420,6 +1470,7 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
14201470
<li class=""><a href="classes/LINE-Clients-Liff-ObjectSerializer.html#method_toQueryValue">toQueryValue()</a></li>
14211471
<li class=""><a href="classes/LINE-Clients-Liff-ObjectSerializer.html#method_toString">toString()</a></li>
14221472
<li class=""><a href="classes/LINE-Clients-Liff-ObjectSerializer.html#method_isEmptyValue">isEmptyValue()</a></li>
1473+
<li class=""><a href="classes/LINE-Clients-Liff-ObjectSerializer.html#method_objectToArray">objectToArray()</a></li>
14231474
</ul>
14241475
</li>
14251476
</ul>

docs/classes/LINE-Clients-ManageAudience-ObjectSerializer.html

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ <h4 id="toc-methods">
342342
</dt>
343343
<dd>Checks if a value is empty, based on its OpenAPI type.</dd>
344344

345+
<dt class="phpdocumentor-table-of-contents__entry -method -private">
346+
<a class="" href="classes/LINE-Clients-ManageAudience-ObjectSerializer.html#method_objectToArray">objectToArray()</a>
347+
<span>
348+
&nbsp;: mixed </span>
349+
</dt>
350+
345351
</dl>
346352

347353

@@ -415,7 +421,7 @@ <h4 class="phpdocumentor-element__name" id="method_buildQuery">
415421
<aside class="phpdocumentor-element-found-in">
416422
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/manage-audience/lib/ObjectSerializer.php"><a href="files/src-clients-manage-audience-lib-objectserializer.html"><abbr title="src/clients/manage-audience/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
417423
:
418-
<span class="phpdocumentor-element-found-in__line">586</span>
424+
<span class="phpdocumentor-element-found-in__line">590</span>
419425

420426
</aside>
421427

@@ -1283,6 +1289,50 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
12831289

12841290
</section>
12851291

1292+
</article>
1293+
<article
1294+
class="phpdocumentor-element
1295+
-method
1296+
-private
1297+
-static "
1298+
>
1299+
<h4 class="phpdocumentor-element__name" id="method_objectToArray">
1300+
objectToArray()
1301+
<a href="classes/LINE-Clients-ManageAudience-ObjectSerializer.html#method_objectToArray" class="headerlink"><i class="fas fa-link"></i></a>
1302+
1303+
</h4>
1304+
<aside class="phpdocumentor-element-found-in">
1305+
<abbr class="phpdocumentor-element-found-in__file" title="src/clients/manage-audience/lib/ObjectSerializer.php"><a href="files/src-clients-manage-audience-lib-objectserializer.html"><abbr title="src/clients/manage-audience/lib/ObjectSerializer.php">ObjectSerializer.php</abbr></a></abbr>
1306+
:
1307+
<span class="phpdocumentor-element-found-in__line">638</span>
1308+
1309+
</aside>
1310+
1311+
1312+
<code class="phpdocumentor-code phpdocumentor-signature ">
1313+
<span class="phpdocumentor-signature__visibility">private</span>
1314+
<span class="phpdocumentor-signature__static">static</span> <span class="phpdocumentor-signature__name">objectToArray</span><span>(</span><span class="phpdocumentor-signature__argument"><span class="phpdocumentor-signature__argument__return-type">mixed&nbsp;</span><span class="phpdocumentor-signature__argument__name">$obj</span></span><span>)</span><span> : </span><span class="phpdocumentor-signature__response_type">mixed</span></code>
1315+
1316+
<div class="phpdocumentor-label-line">
1317+
</div>
1318+
1319+
1320+
<h5 class="phpdocumentor-argument-list__heading">Parameters</h5>
1321+
<dl class="phpdocumentor-argument-list">
1322+
<dt class="phpdocumentor-argument-list__entry">
1323+
<span class="phpdocumentor-signature__argument__name">$obj</span>
1324+
: <span class="phpdocumentor-signature__argument__return-type">mixed</span>
1325+
</dt>
1326+
<dd class="phpdocumentor-argument-list__definition">
1327+
1328+
</dd>
1329+
</dl>
1330+
1331+
1332+
1333+
1334+
1335+
12861336
</article>
12871337
</section>
12881338

@@ -1420,6 +1470,7 @@ <h5 class="phpdocumentor-return-value__heading">Return values</h5>
14201470
<li class=""><a href="classes/LINE-Clients-ManageAudience-ObjectSerializer.html#method_toQueryValue">toQueryValue()</a></li>
14211471
<li class=""><a href="classes/LINE-Clients-ManageAudience-ObjectSerializer.html#method_toString">toString()</a></li>
14221472
<li class=""><a href="classes/LINE-Clients-ManageAudience-ObjectSerializer.html#method_isEmptyValue">isEmptyValue()</a></li>
1473+
<li class=""><a href="classes/LINE-Clients-ManageAudience-ObjectSerializer.html#method_objectToArray">objectToArray()</a></li>
14231474
</ul>
14241475
</li>
14251476
</ul>

0 commit comments

Comments
 (0)