fix object serializer, update tests in php nextgen (#16764)

This commit is contained in:
William Cheng 2023-10-10 11:31:38 +08:00 committed by GitHub
parent 3e9dba01ee
commit 4a17c22905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 38 additions and 73 deletions

View File

@ -50,7 +50,7 @@ class ApiException extends Exception
*
* @var mixed
*/
protected mixed $responseObject;
protected mixed $responseObject = null;
/**
* Constructor

View File

@ -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;
}

View File

@ -60,7 +60,7 @@ class ApiException extends Exception
*
* @var mixed
*/
protected mixed $responseObject;
protected mixed $responseObject = null;
/**
* Constructor

View File

@ -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;
}

View File

@ -59,7 +59,7 @@ class ApiException extends Exception
*
* @var mixed
*/
protected mixed $responseObject;
protected mixed $responseObject = null;
/**
* Constructor

View File

@ -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;
}

View File

@ -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);

View File

@ -1,33 +0,0 @@
<?php
namespace OpenAPI\Client;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/FakeHttpClient.php';
class HeadersTest extends TestCase
{
/** @var FakeHttpClient */
private $fakeHttpClient;
public function setUp(): void
{
$this->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']);
}
}

View File

@ -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());
}
/**

View File

@ -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()
{

View File

@ -1,19 +0,0 @@
<?php
namespace OpenAPI\Client;
use OpenAPI\Client\Model\Pet;
use PHPUnit\Framework\TestCase;
class PetTest extends TestCase
{
/**
* test empty object serialization
*/
public function testEmptyPetSerialization()
{
$new_pet = new Pet;
// the empty object should be serialised to {}
$this->assertSame("{}", "$new_pet");
}
}