forked from loafle/openapi-generator-original
Merge remote-tracking branch 'origin/master' into 6.0.x
This commit is contained in:
@@ -332,18 +332,15 @@ class ObjectSerializer
|
||||
}
|
||||
}
|
||||
|
||||
/** @psalm-suppress ParadoxicalCondition */
|
||||
if (in_array($class, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
|
||||
settype($data, $class);
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ($class === '\SplFileObject') {
|
||||
/** @var \Psr\Http\Message\StreamInterface $data */
|
||||
|
||||
// determine file name
|
||||
if (array_key_exists('Content-Disposition', $httpHeaders) &&
|
||||
preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) {
|
||||
if (
|
||||
is_array($httpHeaders)
|
||||
&& array_key_exists('Content-Disposition', $httpHeaders)
|
||||
&& preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)
|
||||
) {
|
||||
$filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . DIRECTORY_SEPARATOR . self::sanitizeFilename($match[1]);
|
||||
} else {
|
||||
$filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), '');
|
||||
@@ -356,7 +353,16 @@ class ObjectSerializer
|
||||
fclose($file);
|
||||
|
||||
return new \SplFileObject($filename, 'r');
|
||||
} elseif (method_exists($class, 'getAllowableEnumValues')) {
|
||||
}
|
||||
|
||||
/** @psalm-suppress ParadoxicalCondition */
|
||||
if (in_array($class, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
|
||||
settype($data, $class);
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
if (method_exists($class, 'getAllowableEnumValues')) {
|
||||
if (!in_array($data, $class::getAllowableEnumValues(), true)) {
|
||||
$imploded = implode("', '", $class::getAllowableEnumValues());
|
||||
throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace OpenAPI\Client;
|
||||
|
||||
use GuzzleHttp\Psr7\Utils;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
// test object serializer
|
||||
class ObjectSerializerTest extends TestCase
|
||||
@@ -24,4 +26,42 @@ class ObjectSerializerTest extends TestCase
|
||||
$this->assertSame("sun.gif", $s->sanitizeFilename("c:\var\tmp\sun.gif"));
|
||||
$this->assertSame("sun.gif", $s->sanitizeFilename(".\sun.gif"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test SplFileObject class deserialization.
|
||||
*
|
||||
* @see https://github.com/OpenAPITools/openapi-generator/pull/11184
|
||||
* @covers ObjectSerializer::serialize
|
||||
* @dataProvider provideFileStreams
|
||||
*/
|
||||
public function testDeserializeFile(StreamInterface $stream, ?array $httpHeaders = null, ?string $expectedFilename = null): void
|
||||
{
|
||||
$s = new ObjectSerializer();
|
||||
|
||||
/** @var \SplFileObject */
|
||||
$file = $s->deserialize($stream, '\SplFileObject', $httpHeaders);
|
||||
$this->assertInstanceOf(\SplFileObject::class, $file);
|
||||
|
||||
if (is_string($expectedFilename)) {
|
||||
$this->assertEquals($expectedFilename, $file->getFilename());
|
||||
} else {
|
||||
$this->assertNotEquals($expectedFilename, $file->getFilename());
|
||||
}
|
||||
}
|
||||
|
||||
public function provideFileStreams()
|
||||
{
|
||||
return [
|
||||
'File stream without headers' => [
|
||||
Utils::streamFor(\fopen(__FILE__, 'r')),
|
||||
null,
|
||||
null,
|
||||
],
|
||||
'File stream with Content-Disposition header' => [
|
||||
Utils::streamFor(\fopen(__FILE__, 'r')),
|
||||
['Content-Disposition' => 'inline; filename=\'foobar.php\''],
|
||||
'foobar.php',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user