Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php-nextgen] fix object serializer, update tests #16764

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading