[PHP] Fix date format serialization (#5754)

* [PHP] Honor Swagger/OpenAPI 'date' format

Per spec
(https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types),
DateTime instances defined as 'date' datatype need to be serialized as
defined by full-date - RFC3339, which has the format:

full-date       = date-fullyear "-" date-month "-" date-mday

ref:
https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14

see #5531
fixes #5607

* [PHP] Add `date` and `date-time` serializer tests

See #5531
See #5607

* [PHP] Improve codestyle of generated code

* [PHP] Regenerate PHP Petstore sample

* [PHP] Regenerate PHP Security sample
This commit is contained in:
Arne Jørgensen 2017-06-04 18:47:56 +02:00 committed by wing328
parent 00f2dc422d
commit d5b3cc0534
54 changed files with 636 additions and 53 deletions

View File

@ -32,16 +32,18 @@ class ObjectSerializer
/** /**
* Serialize data * Serialize data
* *
* @param mixed $data the data to serialize * @param mixed $data the data to serialize
* @param string $type the SwaggerType of the data
* @param string $format the format of the Swagger type of the data
* *
* @return string|object serialized form of $data * @return string|object serialized form of $data
*/ */
public static function sanitizeForSerialization($data) public static function sanitizeForSerialization($data, $type = null, $format = null)
{ {
if (is_scalar($data) || null === $data) { if (is_scalar($data) || null === $data) {
return $data; return $data;
} elseif ($data instanceof \DateTime) { } elseif ($data instanceof \DateTime) {
return $data->format(\DateTime::ATOM); return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
} elseif (is_array($data)) { } elseif (is_array($data)) {
foreach ($data as $property => $value) { foreach ($data as $property => $value) {
$data[$property] = self::sanitizeForSerialization($value); $data[$property] = self::sanitizeForSerialization($value);
@ -49,6 +51,7 @@ class ObjectSerializer
return $data; return $data;
} elseif (is_object($data)) { } elseif (is_object($data)) {
$values = []; $values = [];
$formats = $data::swaggerFormats();
foreach ($data::swaggerTypes() as $property => $swaggerType) { foreach ($data::swaggerTypes() as $property => $swaggerType) {
$getter = $data::getters()[$property]; $getter = $data::getters()[$property];
$value = $data->$getter(); $value = $data->$getter();
@ -58,7 +61,7 @@ class ObjectSerializer
throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'"); throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
} }
if ($value !== null) { if ($value !== null) {
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value); $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $swaggerType, $formats[$property]);
} }
} }
return (object)$values; return (object)$values;

View File

@ -80,8 +80,10 @@ use \{{invokerPackage}}\ObjectSerializer;
/** /**
* Operation {{{operationId}}} * Operation {{{operationId}}}
{{#summary}}
* *
* {{{summary}}} * {{{summary}}}
{{/summary}}
* *
{{#description}} {{#description}}
* {{.}} * {{.}}
@ -101,8 +103,10 @@ use \{{invokerPackage}}\ObjectSerializer;
/** /**
* Operation {{{operationId}}}WithHttpInfo * Operation {{{operationId}}}WithHttpInfo
{{#summary}}
* *
* {{{summary}}} * {{{summary}}}
{{/summary}}
* *
{{#description}} {{#description}}
* {{.}} * {{.}}

View File

@ -39,7 +39,6 @@ use \{{invokerPackage}}\ObjectSerializer;
*/ */
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
} }
/** /**
@ -47,7 +46,6 @@ use \{{invokerPackage}}\ObjectSerializer;
*/ */
public function setUp() public function setUp()
{ {
} }
/** /**
@ -55,7 +53,6 @@ use \{{invokerPackage}}\ObjectSerializer;
*/ */
public function tearDown() public function tearDown()
{ {
} }
/** /**
@ -63,10 +60,9 @@ use \{{invokerPackage}}\ObjectSerializer;
*/ */
public static function tearDownAfterClass() public static function tearDownAfterClass()
{ {
} }
{{#operation}} {{#operation}}
/** /**
* Test case for {{{operationId}}} * Test case for {{{operationId}}}
* *
@ -75,9 +71,7 @@ use \{{invokerPackage}}\ObjectSerializer;
*/ */
public function test{{vendorExtensions.x-testOperationId}}() public function test{{vendorExtensions.x-testOperationId}}()
{ {
} }
{{/operation}} {{/operation}}
} }
{{/operations}} {{/operations}}

View File

@ -1,4 +1,5 @@
class {{classname}} { class {{classname}}
{
/** /**
* Possible values of this enum * Possible values of this enum
*/ */

View File

@ -17,11 +17,25 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{/hasMore}}{{/vars}} {{/hasMore}}{{/vars}}
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
{{#vars}}'{{name}}' => {{#dataFormat}}'{{{dataFormat}}}'{{/dataFormat}}{{^dataFormat}}null{{/dataFormat}}{{#hasMore}},
{{/hasMore}}{{/vars}}
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes{{#parentSchema}} + parent::swaggerTypes(){{/parentSchema}}; return self::$swaggerTypes{{#parentSchema}} + parent::swaggerTypes(){{/parentSchema}};
} }
public static function swaggerFormats()
{
return self::$swaggerFormats{{#parentSchema}} + parent::swaggerFormats(){{/parentSchema}};
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -39,7 +39,6 @@ class {{classname}}Test extends \PHPUnit_Framework_TestCase
*/ */
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
} }
/** /**
@ -47,7 +46,6 @@ class {{classname}}Test extends \PHPUnit_Framework_TestCase
*/ */
public function setUp() public function setUp()
{ {
} }
/** /**
@ -55,7 +53,6 @@ class {{classname}}Test extends \PHPUnit_Framework_TestCase
*/ */
public function tearDown() public function tearDown()
{ {
} }
/** /**
@ -63,7 +60,6 @@ class {{classname}}Test extends \PHPUnit_Framework_TestCase
*/ */
public static function tearDownAfterClass() public static function tearDownAfterClass()
{ {
} }
/** /**
@ -71,18 +67,15 @@ class {{classname}}Test extends \PHPUnit_Framework_TestCase
*/ */
public function test{{classname}}() public function test{{classname}}()
{ {
} }
{{#vars}} {{#vars}}
/** /**
* Test attribute "{{name}}" * Test attribute "{{name}}"
*/ */
public function testProperty{{nameInCamelCase}}() public function testProperty{{nameInCamelCase}}()
{ {
} }
{{/vars}} {{/vars}}
} }
{{/model}} {{/model}}

View File

@ -0,0 +1 @@
2.2.3-SNAPSHOT

View File

@ -199,6 +199,10 @@ class ApiClient
$url = ($url . '?' . http_build_query($queryParams)); $url = ($url . '?' . http_build_query($queryParams));
} }
if ($this->config->getAllowEncoding()) {
curl_setopt($curl, CURLOPT_ENCODING, '');
}
if ($method === self::$POST) { if ($method === self::$POST) {
curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);

View File

@ -177,6 +177,13 @@ class Configuration
*/ */
protected $proxyPassword; protected $proxyPassword;
/**
* Allow Curl encoding header
*
* @var bool
*/
protected $allowEncoding = false;
/** /**
* Constructor * Constructor
*/ */
@ -445,6 +452,16 @@ class Configuration
return $this; return $this;
} }
/**
* Set whether to accept encoding
* @param bool $allowEncoding
*/
public function setAllowEncoding($allowEncoding)
{
$this->allowEncoding = $allowEncoding;
return $this;
}
/** /**
* Gets the HTTP connect timeout value * Gets the HTTP connect timeout value
* *
@ -455,6 +472,15 @@ class Configuration
return $this->curlConnectTimeout; return $this->curlConnectTimeout;
} }
/**
* Get whether to allow encoding
*
* @return bool
*/
public function getAllowEncoding()
{
return $this->allowEncoding;
}
/** /**
* Sets the HTTP Proxy Host * Sets the HTTP Proxy Host

View File

@ -58,11 +58,24 @@ class ModelReturn implements ArrayAccess
'return' => 'int' 'return' => 'int'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'return' => 'int32'
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -42,16 +42,18 @@ class ObjectSerializer
/** /**
* Serialize data * Serialize data
* *
* @param mixed $data the data to serialize * @param mixed $data the data to serialize
* @param string $type the SwaggerType of the data
* @param string $format the format of the Swagger type of the data
* *
* @return string|object serialized form of $data * @return string|object serialized form of $data
*/ */
public static function sanitizeForSerialization($data) public static function sanitizeForSerialization($data, $type = null, $format = null)
{ {
if (is_scalar($data) || null === $data) { if (is_scalar($data) || null === $data) {
return $data; return $data;
} elseif ($data instanceof \DateTime) { } elseif ($data instanceof \DateTime) {
return $data->format(\DateTime::ATOM); return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
} elseif (is_array($data)) { } elseif (is_array($data)) {
foreach ($data as $property => $value) { foreach ($data as $property => $value) {
$data[$property] = self::sanitizeForSerialization($value); $data[$property] = self::sanitizeForSerialization($value);
@ -59,6 +61,7 @@ class ObjectSerializer
return $data; return $data;
} elseif (is_object($data)) { } elseif (is_object($data)) {
$values = []; $values = [];
$formats = $data::swaggerFormats();
foreach ($data::swaggerTypes() as $property => $swaggerType) { foreach ($data::swaggerTypes() as $property => $swaggerType) {
$getter = $data::getters()[$property]; $getter = $data::getters()[$property];
$value = $data->$getter(); $value = $data->$getter();
@ -68,7 +71,7 @@ class ObjectSerializer
throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'"); throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
} }
if ($value !== null) { if ($value !== null) {
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value); $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $swaggerType, $formats[$property]);
} }
} }
return (object)$values; return (object)$values;

View File

@ -90,8 +90,6 @@ class FakeApi
/** /**
* Operation fakeOuterBooleanSerialize * Operation fakeOuterBooleanSerialize
* *
*
*
* @param \Swagger\Client\Model\OuterBoolean $body Input boolean as post body (optional) * @param \Swagger\Client\Model\OuterBoolean $body Input boolean as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return \Swagger\Client\Model\OuterBoolean * @return \Swagger\Client\Model\OuterBoolean
@ -105,8 +103,6 @@ class FakeApi
/** /**
* Operation fakeOuterBooleanSerializeWithHttpInfo * Operation fakeOuterBooleanSerializeWithHttpInfo
* *
*
*
* @param \Swagger\Client\Model\OuterBoolean $body Input boolean as post body (optional) * @param \Swagger\Client\Model\OuterBoolean $body Input boolean as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return array of \Swagger\Client\Model\OuterBoolean, HTTP status code, HTTP response headers (array of strings) * @return array of \Swagger\Client\Model\OuterBoolean, HTTP status code, HTTP response headers (array of strings)
@ -165,8 +161,6 @@ class FakeApi
/** /**
* Operation fakeOuterCompositeSerialize * Operation fakeOuterCompositeSerialize
* *
*
*
* @param \Swagger\Client\Model\OuterComposite $body Input composite as post body (optional) * @param \Swagger\Client\Model\OuterComposite $body Input composite as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return \Swagger\Client\Model\OuterComposite * @return \Swagger\Client\Model\OuterComposite
@ -180,8 +174,6 @@ class FakeApi
/** /**
* Operation fakeOuterCompositeSerializeWithHttpInfo * Operation fakeOuterCompositeSerializeWithHttpInfo
* *
*
*
* @param \Swagger\Client\Model\OuterComposite $body Input composite as post body (optional) * @param \Swagger\Client\Model\OuterComposite $body Input composite as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return array of \Swagger\Client\Model\OuterComposite, HTTP status code, HTTP response headers (array of strings) * @return array of \Swagger\Client\Model\OuterComposite, HTTP status code, HTTP response headers (array of strings)
@ -240,8 +232,6 @@ class FakeApi
/** /**
* Operation fakeOuterNumberSerialize * Operation fakeOuterNumberSerialize
* *
*
*
* @param \Swagger\Client\Model\OuterNumber $body Input number as post body (optional) * @param \Swagger\Client\Model\OuterNumber $body Input number as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return \Swagger\Client\Model\OuterNumber * @return \Swagger\Client\Model\OuterNumber
@ -255,8 +245,6 @@ class FakeApi
/** /**
* Operation fakeOuterNumberSerializeWithHttpInfo * Operation fakeOuterNumberSerializeWithHttpInfo
* *
*
*
* @param \Swagger\Client\Model\OuterNumber $body Input number as post body (optional) * @param \Swagger\Client\Model\OuterNumber $body Input number as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return array of \Swagger\Client\Model\OuterNumber, HTTP status code, HTTP response headers (array of strings) * @return array of \Swagger\Client\Model\OuterNumber, HTTP status code, HTTP response headers (array of strings)
@ -315,8 +303,6 @@ class FakeApi
/** /**
* Operation fakeOuterStringSerialize * Operation fakeOuterStringSerialize
* *
*
*
* @param \Swagger\Client\Model\OuterString $body Input string as post body (optional) * @param \Swagger\Client\Model\OuterString $body Input string as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return \Swagger\Client\Model\OuterString * @return \Swagger\Client\Model\OuterString
@ -330,8 +316,6 @@ class FakeApi
/** /**
* Operation fakeOuterStringSerializeWithHttpInfo * Operation fakeOuterStringSerializeWithHttpInfo
* *
*
*
* @param \Swagger\Client\Model\OuterString $body Input string as post body (optional) * @param \Swagger\Client\Model\OuterString $body Input string as post body (optional)
* @throws \Swagger\Client\ApiException on non-2xx response * @throws \Swagger\Client\ApiException on non-2xx response
* @return array of \Swagger\Client\Model\OuterString, HTTP status code, HTTP response headers (array of strings) * @return array of \Swagger\Client\Model\OuterString, HTTP status code, HTTP response headers (array of strings)

View File

@ -58,11 +58,25 @@ class AdditionalPropertiesClass implements ArrayAccess
'map_of_map_property' => 'map[string,map[string,string]]' 'map_of_map_property' => 'map[string,map[string,string]]'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'map_property' => null,
'map_of_map_property' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,25 @@ class Animal implements ArrayAccess
'color' => 'string' 'color' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'class_name' => null,
'color' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class AnimalFarm implements ArrayAccess
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -59,11 +59,26 @@ class ApiResponse implements ArrayAccess
'message' => 'string' 'message' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'code' => 'int32',
'type' => null,
'message' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class ArrayOfArrayOfNumberOnly implements ArrayAccess
'array_array_number' => 'float[][]' 'array_array_number' => 'float[][]'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'array_array_number' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class ArrayOfNumberOnly implements ArrayAccess
'array_number' => 'float[]' 'array_number' => 'float[]'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'array_number' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -59,11 +59,26 @@ class ArrayTest implements ArrayAccess
'array_array_of_model' => '\Swagger\Client\Model\ReadOnlyFirst[][]' 'array_array_of_model' => '\Swagger\Client\Model\ReadOnlyFirst[][]'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'array_of_string' => null,
'array_array_of_integer' => 'int64',
'array_array_of_model' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -62,11 +62,29 @@ class Capitalization implements ArrayAccess
'att_name' => 'string' 'att_name' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'small_camel' => null,
'capital_camel' => null,
'small_snake' => null,
'capital_snake' => null,
'sca_eth_flow_points' => null,
'att_name' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class Cat extends Animal implements ArrayAccess
'declawed' => 'bool' 'declawed' => 'bool'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'declawed' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes + parent::swaggerTypes(); return self::$swaggerTypes + parent::swaggerTypes();
} }
public static function swaggerFormats()
{
return self::$swaggerFormats + parent::swaggerFormats();
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,25 @@ class Category implements ArrayAccess
'name' => 'string' 'name' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'id' => 'int64',
'name' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,24 @@ class ClassModel implements ArrayAccess
'_class' => 'string' '_class' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'_class' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class Client implements ArrayAccess
'client' => 'string' 'client' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'client' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class Dog extends Animal implements ArrayAccess
'breed' => 'string' 'breed' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'breed' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes + parent::swaggerTypes(); return self::$swaggerTypes + parent::swaggerTypes();
} }
public static function swaggerFormats()
{
return self::$swaggerFormats + parent::swaggerFormats();
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,25 @@ class EnumArrays implements ArrayAccess
'array_enum' => 'string[]' 'array_enum' => 'string[]'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'just_symbol' => null,
'array_enum' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -37,7 +37,8 @@ namespace Swagger\Client\Model;
* @author Swagger Codegen team * @author Swagger Codegen team
* @link https://github.com/swagger-api/swagger-codegen * @link https://github.com/swagger-api/swagger-codegen
*/ */
class EnumClass { class EnumClass
{
/** /**
* Possible values of this enum * Possible values of this enum
*/ */

View File

@ -60,11 +60,27 @@ class EnumTest implements ArrayAccess
'outer_enum' => '\Swagger\Client\Model\OuterEnum' 'outer_enum' => '\Swagger\Client\Model\OuterEnum'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'enum_string' => null,
'enum_integer' => 'int32',
'enum_number' => 'double',
'outer_enum' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -69,11 +69,36 @@ class FormatTest implements ArrayAccess
'password' => 'string' 'password' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'integer' => null,
'int32' => 'int32',
'int64' => 'int64',
'number' => null,
'float' => 'float',
'double' => 'double',
'string' => null,
'byte' => 'byte',
'binary' => 'binary',
'date' => 'date',
'date_time' => 'date-time',
'uuid' => 'uuid',
'password' => 'password'
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,25 @@ class HasOnlyReadOnly implements ArrayAccess
'foo' => 'string' 'foo' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'bar' => null,
'foo' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,25 @@ class MapTest implements ArrayAccess
'map_of_enum_string' => 'map[string,string]' 'map_of_enum_string' => 'map[string,string]'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'map_map_of_string' => null,
'map_of_enum_string' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -59,11 +59,26 @@ class MixedPropertiesAndAdditionalPropertiesClass implements ArrayAccess
'map' => 'map[string,\Swagger\Client\Model\Animal]' 'map' => 'map[string,\Swagger\Client\Model\Animal]'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'uuid' => 'uuid',
'date_time' => 'date-time',
'map' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -59,11 +59,25 @@ class Model200Response implements ArrayAccess
'class' => 'string' 'class' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'name' => 'int32',
'class' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class ModelList implements ArrayAccess
'_123_list' => 'string' '_123_list' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'_123_list' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,24 @@ class ModelReturn implements ArrayAccess
'return' => 'int' 'return' => 'int'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'return' => 'int32'
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -61,11 +61,27 @@ class Name implements ArrayAccess
'_123_number' => 'int' '_123_number' => 'int'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'name' => 'int32',
'snake_case' => 'int32',
'property' => null,
'_123_number' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class NumberOnly implements ArrayAccess
'just_number' => 'float' 'just_number' => 'float'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'just_number' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -62,11 +62,29 @@ class Order implements ArrayAccess
'complete' => 'bool' 'complete' => 'bool'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'id' => 'int64',
'pet_id' => 'int64',
'quantity' => 'int32',
'ship_date' => 'date-time',
'status' => null,
'complete' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class OuterBoolean implements ArrayAccess
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -59,11 +59,26 @@ class OuterComposite implements ArrayAccess
'my_boolean' => '\Swagger\Client\Model\OuterBoolean' 'my_boolean' => '\Swagger\Client\Model\OuterBoolean'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'my_number' => null,
'my_string' => null,
'my_boolean' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -37,7 +37,8 @@ namespace Swagger\Client\Model;
* @author Swagger Codegen team * @author Swagger Codegen team
* @link https://github.com/swagger-api/swagger-codegen * @link https://github.com/swagger-api/swagger-codegen
*/ */
class OuterEnum { class OuterEnum
{
/** /**
* Possible values of this enum * Possible values of this enum
*/ */

View File

@ -57,11 +57,24 @@ class OuterNumber implements ArrayAccess
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class OuterString implements ArrayAccess
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -62,11 +62,29 @@ class Pet implements ArrayAccess
'status' => 'string' 'status' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'id' => 'int64',
'category' => null,
'name' => null,
'photo_urls' => null,
'tags' => null,
'status' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,25 @@ class ReadOnlyFirst implements ArrayAccess
'baz' => 'string' 'baz' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'bar' => null,
'baz' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -57,11 +57,24 @@ class SpecialModelName implements ArrayAccess
'special_property_name' => 'int' 'special_property_name' => 'int'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'special_property_name' => 'int64'
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -58,11 +58,25 @@ class Tag implements ArrayAccess
'name' => 'string' 'name' => 'string'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'id' => 'int64',
'name' => null
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -64,11 +64,31 @@ class User implements ArrayAccess
'user_status' => 'int' 'user_status' => 'int'
]; ];
/**
* Array of property to format mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerFormats = [
'id' => 'int64',
'username' => null,
'first_name' => null,
'last_name' => null,
'email' => null,
'password' => null,
'phone' => null,
'user_status' => 'int32'
];
public static function swaggerTypes() public static function swaggerTypes()
{ {
return self::$swaggerTypes; return self::$swaggerTypes;
} }
public static function swaggerFormats()
{
return self::$swaggerFormats;
}
/** /**
* Array of attributes where the key is the local name, and the value is the original name * Array of attributes where the key is the local name, and the value is the original name
* @var string[] * @var string[]

View File

@ -42,16 +42,18 @@ class ObjectSerializer
/** /**
* Serialize data * Serialize data
* *
* @param mixed $data the data to serialize * @param mixed $data the data to serialize
* @param string $type the SwaggerType of the data
* @param string $format the format of the Swagger type of the data
* *
* @return string|object serialized form of $data * @return string|object serialized form of $data
*/ */
public static function sanitizeForSerialization($data) public static function sanitizeForSerialization($data, $type = null, $format = null)
{ {
if (is_scalar($data) || null === $data) { if (is_scalar($data) || null === $data) {
return $data; return $data;
} elseif ($data instanceof \DateTime) { } elseif ($data instanceof \DateTime) {
return $data->format(\DateTime::ATOM); return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
} elseif (is_array($data)) { } elseif (is_array($data)) {
foreach ($data as $property => $value) { foreach ($data as $property => $value) {
$data[$property] = self::sanitizeForSerialization($value); $data[$property] = self::sanitizeForSerialization($value);
@ -59,6 +61,7 @@ class ObjectSerializer
return $data; return $data;
} elseif (is_object($data)) { } elseif (is_object($data)) {
$values = []; $values = [];
$formats = $data::swaggerFormats();
foreach ($data::swaggerTypes() as $property => $swaggerType) { foreach ($data::swaggerTypes() as $property => $swaggerType) {
$getter = $data::getters()[$property]; $getter = $data::getters()[$property];
$value = $data->$getter(); $value = $data->$getter();
@ -68,7 +71,7 @@ class ObjectSerializer
throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'"); throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
} }
if ($value !== null) { if ($value !== null) {
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value); $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $swaggerType, $formats[$property]);
} }
} }
return (object)$values; return (object)$values;

View File

@ -4,7 +4,14 @@
<description>Arnes Drupal code checker</description> <description>Arnes Drupal code checker</description>
<rule ref="PSR2" /> <rule ref="PSR2" />
<rule ref="Generic.Files.LineLength">
<!-- Getting rid of extra newlines at the end of generated files is not worth the trouble -->
<rule ref="PSR2.Files.EndFileNewline.TooMany">
<exclude-pattern>*</exclude-pattern>
</rule>
<!-- Avoiding generating code lines to grow too long is not worth the trouble -->
<rule ref="Generic.Files.LineLength.TooLong">
<exclude-pattern>*</exclude-pattern> <exclude-pattern>*</exclude-pattern>
</rule> </rule>
</ruleset> </ruleset>

View File

@ -0,0 +1,34 @@
<?php
namespace Swagger\Client;
use Swagger\Client\Model\FormatTest;
class DateTimeSerializerTest extends \PHPUnit_Framework_TestCase
{
public function testDateTimeSanitazion()
{
$dateTime = new \DateTime('April 30, 1973 17:05 CEST');
$input = new FormatTest([
'date_time' => $dateTime,
]);
$data = ObjectSerializer::sanitizeForSerialization($input);
$this->assertEquals($data->dateTime, '1973-04-30T17:05:00+02:00');
}
public function testDateSanitazion()
{
$dateTime = new \DateTime('April 30, 1973 17:05 CEST');
$input = new FormatTest([
'date' => $dateTime,
]);
$data = ObjectSerializer::sanitizeForSerialization($input);
$this->assertEquals($data->date, '1973-04-30');
}
}

View File

@ -349,7 +349,6 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$response = $pet_api->uploadFile($pet_id, "test meta", "./composer.json"); $response = $pet_api->uploadFile($pet_id, "test meta", "./composer.json");
// return ApiResponse // return ApiResponse
$this->assertInstanceOf('Swagger\Client\Model\ApiResponse', $response); $this->assertInstanceOf('Swagger\Client\Model\ApiResponse', $response);
} }
// test get inventory // test get inventory
@ -402,7 +401,6 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$new_pet = new Model\Pet; $new_pet = new Model\Pet;
// the empty object should be serialised to {} // the empty object should be serialised to {}
$this->assertSame("{}", "$new_pet"); $this->assertSame("{}", "$new_pet");
} }
// test inheritance in the model // test inheritance in the model
@ -493,6 +491,7 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
$pet_host = $pet_api->getApiClient()->getConfig()->getHost(); $pet_host = $pet_api->getApiClient()->getConfig()->getHost();
$this->assertSame($pet_host, $new_default->getHost()); $this->assertSame($pet_host, $new_default->getHost());
Configuration::setDefaultConfiguration($orig_default); // Reset to original to prevent failure of other tests that rely on this state // Reset to original to prevent failure of other tests that rely on this state
Configuration::setDefaultConfiguration($orig_default);
} }
} }

View File

@ -29,6 +29,5 @@ class UserApiTest extends \PHPUnit_Framework_TestCase
$response, $response,
"response string starts with 'logged in user session'" "response string starts with 'logged in user session'"
); );
} }
} }