Skip to content

Commit

Permalink
fix object serializer, update tests in php nextgen (OpenAPITools#16764)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored and AlanCitrix committed Oct 26, 2023
1 parent 989aa5f commit 7d1fc5a
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ApiException extends Exception
*
* @var mixed
*/
protected mixed $responseObject;
protected mixed $responseObject = null;
/**
* Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/echo_api/php-nextgen/src/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ApiException extends Exception
*
* @var mixed
*/
protected mixed $responseObject;
protected mixed $responseObject = null;

/**
* Constructor
Expand Down
9 changes: 7 additions & 2 deletions samples/client/echo_api/php-nextgen/src/ObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public static function deserialize(mixed $data, string $class, array $httpHeader
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
Expand Down Expand Up @@ -484,7 +484,12 @@ public static function deserialize(mixed $data, string $class, array $httpHeader
}

/** @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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ApiException extends Exception
*
* @var mixed
*/
protected mixed $responseObject;
protected mixed $responseObject = null;

/**
* Constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public static function deserialize(mixed $data, string $class, array $httpHeader
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
Expand Down Expand Up @@ -483,7 +483,12 @@ public static function deserialize(mixed $data, string $class, array $httpHeader
}

/** @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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public function testDateTimeSanitation()

$input = new FormatTest([
'date_time' => $dateTime,
'date' => $dateTime,
'number' => 123.45,
'password' => 'password',
'byte' => '0101'
]);

$data = ObjectSerializer::sanitizeForSerialization($input);
Expand All @@ -30,7 +34,11 @@ public function testDateSanitation()
$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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function testInlineAdditionalProperties()
{
$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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Assert;

require_once __DIR__ . '/FakeHttpClient.php';

class PetApiTest extends TestCase
{

Expand Down Expand Up @@ -117,15 +119,15 @@ public function testGetPetByIdWithHttpInfo()

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
foreach ($response as $pet) {
$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);
}

Expand Down Expand Up @@ -281,14 +283,6 @@ public function testGetPetByIdWithByteArray()
}
*/

// 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()
{
Expand Down

This file was deleted.

0 comments on commit 7d1fc5a

Please sign in to comment.