Skip to content

Commit ca9a4a2

Browse files
thomasphansenackintosh
authored andcommitted
PR: Fix error when giving an array as parameter to an endpoint body r… (#1037)
* PR: Fix error when giving an array as parameter to an endpoint body request * PR #1037 - Fix issue with array as parameter to operation * update samples * PHP - ObjectSerializer::sanitizeForSerialization(): manage \stdClass objects properly * update samples * bug fix: missing "use" clause * update samples * Changes after @ackintosh review * update samples
1 parent b7edad5 commit ca9a4a2

File tree

16 files changed

+336
-312
lines changed

16 files changed

+336
-312
lines changed

modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
namespace {{invokerPackage}};
2121

22+
use {{modelPackage}}\ModelInterface;
23+
2224
/**
2325
* ObjectSerializer Class Doc Comment
2426
*
@@ -51,19 +53,25 @@ class ObjectSerializer
5153
return $data;
5254
} elseif (is_object($data)) {
5355
$values = [];
54-
$formats = $data::openAPIFormats();
55-
foreach ($data::openAPITypes() as $property => $openAPIType) {
56-
$getter = $data::getters()[$property];
57-
$value = $data->$getter();
58-
if ($value !== null
59-
&& !in_array($openAPIType, [{{&primitives}}], true)
60-
&& method_exists($openAPIType, 'getAllowableEnumValues')
61-
&& !in_array($value, $openAPIType::getAllowableEnumValues(), true)) {
62-
$imploded = implode("', '", $openAPIType::getAllowableEnumValues());
63-
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
56+
if ($data instanceof ModelInterface) {
57+
$formats = $data::openAPIFormats();
58+
foreach ($data::openAPITypes() as $property => $openAPIType) {
59+
$getter = $data::getters()[$property];
60+
$value = $data->$getter();
61+
if ($value !== null
62+
&& !in_array($openAPIType, [{{&primitives}}], true)
63+
&& method_exists($openAPIType, 'getAllowableEnumValues')
64+
&& !in_array($value, $openAPIType::getAllowableEnumValues(), true)) {
65+
$imploded = implode("', '", $openAPIType::getAllowableEnumValues());
66+
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
67+
}
68+
if ($value !== null) {
69+
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
70+
}
6471
}
65-
if ($value !== null) {
66-
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
72+
} else {
73+
foreach($data as $property => $value) {
74+
$values[$property] = self::sanitizeForSerialization($value);
6775
}
6876
}
6977
return (object)$values;

modules/openapi-generator/src/main/resources/php/api.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,10 @@ use {{invokerPackage}}\ObjectSerializer;
444444
// for model (json/xml)
445445
if (isset($_tempBody)) {
446446
// $_tempBody is the method argument, if present
447-
$httpBody = $_tempBody;
448-
// \stdClass has no __toString(), so we should encode it manually
449-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
450-
$httpBody = \GuzzleHttp\json_encode($httpBody);
447+
if ($headers['Content-Type'] === 'application/json') {
448+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
449+
} else {
450+
$httpBody = $_tempBody;
451451
}
452452
} elseif (count($formParams) > 0) {
453453
if ($multipart) {

samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ protected function call123TestSpecialTagsRequest($client)
307307
// for model (json/xml)
308308
if (isset($_tempBody)) {
309309
// $_tempBody is the method argument, if present
310-
$httpBody = $_tempBody;
311-
// \stdClass has no __toString(), so we should encode it manually
312-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
313-
$httpBody = \GuzzleHttp\json_encode($httpBody);
310+
if ($headers['Content-Type'] === 'application/json') {
311+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
312+
} else {
313+
$httpBody = $_tempBody;
314314
}
315315
} elseif (count($formParams) > 0) {
316316
if ($multipart) {

samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,10 @@ protected function fakeOuterBooleanSerializeRequest($body = null)
297297
// for model (json/xml)
298298
if (isset($_tempBody)) {
299299
// $_tempBody is the method argument, if present
300-
$httpBody = $_tempBody;
301-
// \stdClass has no __toString(), so we should encode it manually
302-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
303-
$httpBody = \GuzzleHttp\json_encode($httpBody);
300+
if ($headers['Content-Type'] === 'application/json') {
301+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
302+
} else {
303+
$httpBody = $_tempBody;
304304
}
305305
} elseif (count($formParams) > 0) {
306306
if ($multipart) {
@@ -554,10 +554,10 @@ protected function fakeOuterCompositeSerializeRequest($outer_composite = null)
554554
// for model (json/xml)
555555
if (isset($_tempBody)) {
556556
// $_tempBody is the method argument, if present
557-
$httpBody = $_tempBody;
558-
// \stdClass has no __toString(), so we should encode it manually
559-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
560-
$httpBody = \GuzzleHttp\json_encode($httpBody);
557+
if ($headers['Content-Type'] === 'application/json') {
558+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
559+
} else {
560+
$httpBody = $_tempBody;
561561
}
562562
} elseif (count($formParams) > 0) {
563563
if ($multipart) {
@@ -811,10 +811,10 @@ protected function fakeOuterNumberSerializeRequest($body = null)
811811
// for model (json/xml)
812812
if (isset($_tempBody)) {
813813
// $_tempBody is the method argument, if present
814-
$httpBody = $_tempBody;
815-
// \stdClass has no __toString(), so we should encode it manually
816-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
817-
$httpBody = \GuzzleHttp\json_encode($httpBody);
814+
if ($headers['Content-Type'] === 'application/json') {
815+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
816+
} else {
817+
$httpBody = $_tempBody;
818818
}
819819
} elseif (count($formParams) > 0) {
820820
if ($multipart) {
@@ -1068,10 +1068,10 @@ protected function fakeOuterStringSerializeRequest($body = null)
10681068
// for model (json/xml)
10691069
if (isset($_tempBody)) {
10701070
// $_tempBody is the method argument, if present
1071-
$httpBody = $_tempBody;
1072-
// \stdClass has no __toString(), so we should encode it manually
1073-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
1074-
$httpBody = \GuzzleHttp\json_encode($httpBody);
1071+
if ($headers['Content-Type'] === 'application/json') {
1072+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
1073+
} else {
1074+
$httpBody = $_tempBody;
10751075
}
10761076
} elseif (count($formParams) > 0) {
10771077
if ($multipart) {
@@ -1283,10 +1283,10 @@ protected function testBodyWithFileSchemaRequest($file_schema_test_class)
12831283
// for model (json/xml)
12841284
if (isset($_tempBody)) {
12851285
// $_tempBody is the method argument, if present
1286-
$httpBody = $_tempBody;
1287-
// \stdClass has no __toString(), so we should encode it manually
1288-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
1289-
$httpBody = \GuzzleHttp\json_encode($httpBody);
1286+
if ($headers['Content-Type'] === 'application/json') {
1287+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
1288+
} else {
1289+
$httpBody = $_tempBody;
12901290
}
12911291
} elseif (count($formParams) > 0) {
12921292
if ($multipart) {
@@ -1513,10 +1513,10 @@ protected function testBodyWithQueryParamsRequest($query, $user)
15131513
// for model (json/xml)
15141514
if (isset($_tempBody)) {
15151515
// $_tempBody is the method argument, if present
1516-
$httpBody = $_tempBody;
1517-
// \stdClass has no __toString(), so we should encode it manually
1518-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
1519-
$httpBody = \GuzzleHttp\json_encode($httpBody);
1516+
if ($headers['Content-Type'] === 'application/json') {
1517+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
1518+
} else {
1519+
$httpBody = $_tempBody;
15201520
}
15211521
} elseif (count($formParams) > 0) {
15221522
if ($multipart) {
@@ -1780,10 +1780,10 @@ protected function testClientModelRequest($client)
17801780
// for model (json/xml)
17811781
if (isset($_tempBody)) {
17821782
// $_tempBody is the method argument, if present
1783-
$httpBody = $_tempBody;
1784-
// \stdClass has no __toString(), so we should encode it manually
1785-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
1786-
$httpBody = \GuzzleHttp\json_encode($httpBody);
1783+
if ($headers['Content-Type'] === 'application/json') {
1784+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
1785+
} else {
1786+
$httpBody = $_tempBody;
17871787
}
17881788
} elseif (count($formParams) > 0) {
17891789
if ($multipart) {
@@ -2183,10 +2183,10 @@ protected function testEndpointParametersRequest($number, $double, $pattern_with
21832183
// for model (json/xml)
21842184
if (isset($_tempBody)) {
21852185
// $_tempBody is the method argument, if present
2186-
$httpBody = $_tempBody;
2187-
// \stdClass has no __toString(), so we should encode it manually
2188-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
2189-
$httpBody = \GuzzleHttp\json_encode($httpBody);
2186+
if ($headers['Content-Type'] === 'application/json') {
2187+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
2188+
} else {
2189+
$httpBody = $_tempBody;
21902190
}
21912191
} elseif (count($formParams) > 0) {
21922192
if ($multipart) {
@@ -2470,10 +2470,10 @@ protected function testEnumParametersRequest($enum_header_string_array = null, $
24702470
// for model (json/xml)
24712471
if (isset($_tempBody)) {
24722472
// $_tempBody is the method argument, if present
2473-
$httpBody = $_tempBody;
2474-
// \stdClass has no __toString(), so we should encode it manually
2475-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
2476-
$httpBody = \GuzzleHttp\json_encode($httpBody);
2473+
if ($headers['Content-Type'] === 'application/json') {
2474+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
2475+
} else {
2476+
$httpBody = $_tempBody;
24772477
}
24782478
} elseif (count($formParams) > 0) {
24792479
if ($multipart) {
@@ -2689,10 +2689,10 @@ protected function testInlineAdditionalPropertiesRequest($request_body)
26892689
// for model (json/xml)
26902690
if (isset($_tempBody)) {
26912691
// $_tempBody is the method argument, if present
2692-
$httpBody = $_tempBody;
2693-
// \stdClass has no __toString(), so we should encode it manually
2694-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
2695-
$httpBody = \GuzzleHttp\json_encode($httpBody);
2692+
if ($headers['Content-Type'] === 'application/json') {
2693+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
2694+
} else {
2695+
$httpBody = $_tempBody;
26962696
}
26972697
} elseif (count($formParams) > 0) {
26982698
if ($multipart) {
@@ -2924,10 +2924,10 @@ protected function testJsonFormDataRequest($param, $param2)
29242924
// for model (json/xml)
29252925
if (isset($_tempBody)) {
29262926
// $_tempBody is the method argument, if present
2927-
$httpBody = $_tempBody;
2928-
// \stdClass has no __toString(), so we should encode it manually
2929-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
2930-
$httpBody = \GuzzleHttp\json_encode($httpBody);
2927+
if ($headers['Content-Type'] === 'application/json') {
2928+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
2929+
} else {
2930+
$httpBody = $_tempBody;
29312931
}
29322932
} elseif (count($formParams) > 0) {
29332933
if ($multipart) {

samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ protected function testClassnameRequest($client)
307307
// for model (json/xml)
308308
if (isset($_tempBody)) {
309309
// $_tempBody is the method argument, if present
310-
$httpBody = $_tempBody;
311-
// \stdClass has no __toString(), so we should encode it manually
312-
if ($httpBody instanceof \stdClass && $headers['Content-Type'] === 'application/json') {
313-
$httpBody = \GuzzleHttp\json_encode($httpBody);
310+
if ($headers['Content-Type'] === 'application/json') {
311+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
312+
} else {
313+
$httpBody = $_tempBody;
314314
}
315315
} elseif (count($formParams) > 0) {
316316
if ($multipart) {

0 commit comments

Comments
 (0)