From 4a17c22905175e137f234960fb923c76d8194a8b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 10 Oct 2023 11:31:38 +0800 Subject: [PATCH] fix object serializer, update tests in php nextgen (#16764) --- .../php-nextgen/ApiException.mustache | 2 +- .../php-nextgen/ObjectSerializer.mustache | 9 +++-- .../echo_api/php-nextgen/src/ApiException.php | 2 +- .../php-nextgen/src/ObjectSerializer.php | 9 +++-- .../OpenAPIClient-php/src/ApiException.php | 2 +- .../src/ObjectSerializer.php | 9 +++-- .../test/DateTimeSerializerTest.php | 8 +++++ .../OpenAPIClient-php/test/HeadersTest.php | 33 ------------------- .../OpenAPIClient-php/test/ParametersTest.php | 4 +-- .../OpenAPIClient-php/test/PetApiTest.php | 14 +++----- .../OpenAPIClient-php/test/PetTest.php | 19 ----------- 11 files changed, 38 insertions(+), 73 deletions(-) delete mode 100644 samples/client/petstore/php-nextgen/OpenAPIClient-php/test/HeadersTest.php delete mode 100644 samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetTest.php diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/ApiException.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/ApiException.mustache index a4cf50fdd92..2cdb18b83b2 100644 --- a/modules/openapi-generator/src/main/resources/php-nextgen/ApiException.mustache +++ b/modules/openapi-generator/src/main/resources/php-nextgen/ApiException.mustache @@ -50,7 +50,7 @@ class ApiException extends Exception * * @var mixed */ - protected mixed $responseObject; + protected mixed $responseObject = null; /** * Constructor diff --git a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache index 23367d5ca07..10c2203227d 100644 --- a/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php-nextgen/ObjectSerializer.mustache @@ -427,7 +427,7 @@ class ObjectSerializer return $data; } - if ($class === 'DateTime') { + if ($class === '\DateTime') { // Some APIs return an invalid, empty string as a // date-time property. DateTime::__construct() will return // the current time for empty input which is probably not @@ -474,7 +474,12 @@ class ObjectSerializer } /** @psalm-suppress ParadoxicalCondition */ - if (in_array($class, [{{&primitives}}], true)) { + // handle primitive types + if (in_array($class, ['\DateTime', '\SplFileObject'], true)) { + return $data; + } elseif (in_array($class, ['array', 'bool', 'boolean', 'float', 'double', 'int', 'integer', 'object', 'string', 'null'], true)) { + // type ref: https://www.php.net/manual/en/function.settype.php + // byte, mixed, void in the old php client were removed settype($data, $class); return $data; } diff --git a/samples/client/echo_api/php-nextgen/src/ApiException.php b/samples/client/echo_api/php-nextgen/src/ApiException.php index 886493dd047..9749438fb08 100644 --- a/samples/client/echo_api/php-nextgen/src/ApiException.php +++ b/samples/client/echo_api/php-nextgen/src/ApiException.php @@ -60,7 +60,7 @@ class ApiException extends Exception * * @var mixed */ - protected mixed $responseObject; + protected mixed $responseObject = null; /** * Constructor diff --git a/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php b/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php index edf8a5c60b4..2b5ae4aefe8 100644 --- a/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php +++ b/samples/client/echo_api/php-nextgen/src/ObjectSerializer.php @@ -437,7 +437,7 @@ class ObjectSerializer return $data; } - if ($class === 'DateTime') { + if ($class === '\DateTime') { // Some APIs return an invalid, empty string as a // date-time property. DateTime::__construct() will return // the current time for empty input which is probably not @@ -484,7 +484,12 @@ class ObjectSerializer } /** @psalm-suppress ParadoxicalCondition */ - if (in_array($class, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { + // handle primitive types + if (in_array($class, ['\DateTime', '\SplFileObject'], true)) { + return $data; + } elseif (in_array($class, ['array', 'bool', 'boolean', 'float', 'double', 'int', 'integer', 'object', 'string', 'null'], true)) { + // type ref: https://www.php.net/manual/en/function.settype.php + // byte, mixed, void in the old php client were removed settype($data, $class); return $data; } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ApiException.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ApiException.php index 7b3bb441592..88c1b37d8c9 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ApiException.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ApiException.php @@ -59,7 +59,7 @@ class ApiException extends Exception * * @var mixed */ - protected mixed $responseObject; + protected mixed $responseObject = null; /** * Constructor diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php index 72733e8b5d2..47bedf3a541 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/src/ObjectSerializer.php @@ -436,7 +436,7 @@ class ObjectSerializer return $data; } - if ($class === 'DateTime') { + if ($class === '\DateTime') { // Some APIs return an invalid, empty string as a // date-time property. DateTime::__construct() will return // the current time for empty input which is probably not @@ -483,7 +483,12 @@ class ObjectSerializer } /** @psalm-suppress ParadoxicalCondition */ - if (in_array($class, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { + // handle primitive types + if (in_array($class, ['\DateTime', '\SplFileObject'], true)) { + return $data; + } elseif (in_array($class, ['array', 'bool', 'boolean', 'float', 'double', 'int', 'integer', 'object', 'string', 'null'], true)) { + // type ref: https://www.php.net/manual/en/function.settype.php + // byte, mixed, void in the old php client were removed settype($data, $class); return $data; } diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/DateTimeSerializerTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/DateTimeSerializerTest.php index 78547afd2f4..5aee91c5a90 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/DateTimeSerializerTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/DateTimeSerializerTest.php @@ -13,6 +13,10 @@ class DateTimeSerializerTest extends TestCase $input = new FormatTest([ 'date_time' => $dateTime, + 'date' => $dateTime, + 'number' => 123.45, + 'password' => 'password', + 'byte' => '0101' ]); $data = ObjectSerializer::sanitizeForSerialization($input); @@ -30,7 +34,11 @@ class DateTimeSerializerTest extends TestCase $dateTime = new \DateTime('April 30, 1973 17:05 CEST'); $input = new FormatTest([ + 'date_time' => $dateTime, 'date' => $dateTime, + 'number' => 123.45, + 'password' => 'password', + 'byte' => '0101' ]); $data = ObjectSerializer::sanitizeForSerialization($input); diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/HeadersTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/HeadersTest.php deleted file mode 100644 index 989516cc0c6..00000000000 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/HeadersTest.php +++ /dev/null @@ -1,33 +0,0 @@ -fakeHttpClient = new FakeHttpClient(); - } - - public function testUserAgent() - { - $config = new Configuration(); - $config->setUserAgent('value'); - $api = new Api\PetApi($this->fakeHttpClient, $config); - - $api->getPetById(3); - - $request = $this->fakeHttpClient->getLastRequest(); - $headers = $request->getHeaders(); - - $this->assertArrayHasKey('User-Agent', $headers); - $this->assertEquals(['value'], $headers['User-Agent']); - } -} diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ParametersTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ParametersTest.php index f5a00efc6b8..9bda987f53d 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ParametersTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/ParametersTest.php @@ -50,10 +50,10 @@ class ParametersTest extends TestCase { $param = new \stdClass(); $param->foo = 'bar'; - $this->fakeApi->testInlineAdditionalProperties($param); + $this->fakeApi->testInlineAdditionalProperties(array($param)); $request = $this->fakeHttpClient->getLastRequest(); - $this->assertSame('{"foo":"bar"}', $request->getBody()->getContents()); + $this->assertSame('[{"foo":"bar"}]', $request->getBody()->getContents()); } /** diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php index 15202763d20..758dfda0d11 100644 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php +++ b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetApiTest.php @@ -8,6 +8,8 @@ use OpenAPI\Client\Model\Pet; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Assert; +require_once __DIR__ . '/FakeHttpClient.php'; + class PetApiTest extends TestCase { @@ -117,7 +119,7 @@ class PetApiTest extends TestCase public function testFindPetByStatus() { - $response = $this->api->findPetsByStatus('available'); + $response = $this->api->findPetsByStatus(array('available')); $this->assertGreaterThan(0, count($response)); // at least one object returned $this->assertInstanceOf(Pet::class, $response[0]); // verify the object is Pet @@ -125,7 +127,7 @@ class PetApiTest extends TestCase $this->assertSame('available', $pet->getStatus()); } - $response = $this->api->findPetsByStatus('unknown_and_incorrect_status'); + $response = $this->api->findPetsByStatus(array('unknown_and_incorrect_status')); $this->assertCount(0, $response); } @@ -281,14 +283,6 @@ class PetApiTest extends TestCase } */ - // test empty object serialization - public function testEmptyPetSerialization() - { - $new_pet = new Model\Pet; - // the empty object should be serialised to {} - $this->assertSame("{}", "$new_pet"); - } - // test inheritance in the model public function testInheritance() { diff --git a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetTest.php b/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetTest.php deleted file mode 100644 index c3c4ee8950e..00000000000 --- a/samples/client/petstore/php-nextgen/OpenAPIClient-php/test/PetTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertSame("{}", "$new_pet"); - } -}