Skip to content

[PHP] [ci skip] Add nullable logic #3493

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

Closed
wants to merge 14 commits into from
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ObjectSerializer
$imploded = implode("', '", $openAPIType::getAllowableEnumValues());
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
}
if ($value !== null) {
if ($data::isNullable($property) && $data->isNullableSetToNull($property) || $value !== null) {
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
}
}
Expand Down Expand Up @@ -307,7 +307,12 @@ class ObjectSerializer
foreach ($instance::openAPITypes() as $property => $type) {
$propertySetter = $instance::setters()[$property];

if (!isset($propertySetter) || !isset($data->{$instance::attributeMap()[$property]})) {
if (!isset($propertySetter)) {
continue;
}

if (!isset($data->{$instance::attributeMap()[$property]})) {
$instance->$propertySetter(null);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
{{/hasMore}}{{/vars}}
];

/**
* Array of nullable properties. Used for (de)serialization
*
* @var boolean[]
*/
protected static $openAPINullables = [
{{#vars}}'{{name}}' => {{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{#hasMore}},
{{/hasMore}}{{/vars}}
];

/**
* If a nullable field gets set to null, insert it here
*
* @var boolean[]
*/
protected $openAPINullablesSetToNull = [];

/**
* Array of property to type mappings. Used for (de)serialization
*
Expand All @@ -49,6 +66,60 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
return self::$openAPIFormats{{#parentSchema}} + parent::openAPIFormats(){{/parentSchema}};
}

/**
* Array of property to nullable mappings. Used for (de)serialization
*
* @return array
*/
public static function openAPINullables()
{
return self::$openAPINullables{{#parentSchema}} + parent::openAPINullables(){{/parentSchema}};
}

/**
* Array of nullable field names deliberately set to null
*
* @return array
*/
public function getOpenAPINullablesSetToNull()
{
return $this->openAPINullablesSetToNull;
}

public function setOpenAPINullablesSetToNull($nullablesSetToNull)
{
$this->openAPINullablesSetToNull=$nullablesSetToNull;

return $this;
}

/**
* Checks if a property is nullable
*
* @return bool
*/
public static function isNullable(string $property): bool
{
if (isset(self::$openAPINullables[$property])) {
return self::$openAPINullables[$property];
}

return false;
}

/**
* Checks if a nullable property is set to null.
*
* @return bool
*/
public function isNullableSetToNull(string $property): bool
{
if (in_array($property, $this->getOpenAPINullablesSetToNull())) {
return true;
}
return false;
}

/**
* Array of attributes where the key is the local name,
* and the value is the original name
Expand Down Expand Up @@ -161,7 +232,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa

{{/parentSchema}}
{{#vars}}
$this->container['{{name}}'] = isset($data['{{name}}']) ? $data['{{name}}'] : {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};
$this->setIfExists('{{name}}', $data, {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}});
{{/vars}}
{{#discriminator}}

Expand All @@ -170,6 +241,17 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
{{/discriminator}}
}

public function setIfExists(string $variableName, $fields, $defaultValue)
{
if (is_array($fields) && array_key_exists($variableName, $fields) && is_null($fields[$variableName]) && self::isNullable($variableName)) {
array_push($this->openAPINullablesSetToNull, $variableName);
}

$this->container[$variableName] = isset($fields[$variableName]) ? $fields[$variableName] : $defaultValue;

return $this;
}

/**
* Show all the invalid properties with reasons.
*
Expand Down Expand Up @@ -340,6 +422,26 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
}
{{/minItems}}
{{/hasValidation}}

{{#isNullable}}
if (is_null(${{name}})) {
array_push($this->openAPINullablesSetToNull, '{{name}}');
} else {
$nullablesSetToNull = $this->getOpenAPINullablesSetToNull();
$index = array_search('{{name}}', $nullablesSetToNull);
if ($index !== FALSE) {
unset($nullablesSetToNull[$index]);
$this->setOpenAPINullablesSetToNull($nullablesSetToNull);
}
}
{{/isNullable}}

{{^isNullable}}
if (is_null(${{name}})) {
throw new \InvalidArgumentException('non-nullable {{name}} cannot be null');
}
{{/isNullable}}

$this->container['{{name}}'] = ${{name}};

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ class AdditionalPropertiesAnyType implements ModelInterface, ArrayAccess
'name' => null
];

/**
* Array of nullable properties. Used for (de)serialization
*
* @var boolean[]
*/
protected static $openAPINullables = [
'name' => false
];

/**
* If a nullable field gets set to null, insert it here
*
* @var boolean[]
*/
protected $openAPINullablesSetToNull = [];

/**
* Array of property to type mappings. Used for (de)serialization
*
Expand All @@ -89,6 +105,60 @@ public static function openAPIFormats()
return self::$openAPIFormats;
}

/**
* Array of property to nullable mappings. Used for (de)serialization
*
* @return array
*/
public static function openAPINullables()
{
return self::$openAPINullables;
}

/**
* Array of nullable field names deliberately set to null
*
* @return array
*/
public function getOpenAPINullablesSetToNull()
{
return $this->openAPINullablesSetToNull;
}

public function setOpenAPINullablesSetToNull($nullablesSetToNull)
{
$this->openAPINullablesSetToNull=$nullablesSetToNull;

return $this;
}

/**
* Checks if a property is nullable
*
* @return bool
*/
public static function isNullable(string $property): bool
{
if (isset(self::$openAPINullables[$property])) {
return self::$openAPINullables[$property];
}

return false;
}

/**
* Checks if a nullable property is set to null.
*
* @return bool
*/
public function isNullableSetToNull(string $property): bool
{
if (in_array($property, $this->getOpenAPINullablesSetToNull())) {
return true;
}
return false;
}

/**
* Array of attributes where the key is the local name,
* and the value is the original name
Expand Down Expand Up @@ -177,7 +247,18 @@ public function getModelName()
*/
public function __construct(array $data = null)
{
$this->container['name'] = isset($data['name']) ? $data['name'] : null;
$this->setIfExists('name', $data, null);
}

public function setIfExists(string $variableName, $fields, $defaultValue)
{
if (is_array($fields) && array_key_exists($variableName, $fields) && is_null($fields[$variableName]) && self::isNullable($variableName)) {
array_push($this->openAPINullablesSetToNull, $variableName);
}

$this->container[$variableName] = isset($fields[$variableName]) ? $fields[$variableName] : $defaultValue;

return $this;
}

/**
Expand Down Expand Up @@ -223,6 +304,12 @@ public function getName()
*/
public function setName($name)
{


if (is_null($name)) {
throw new \InvalidArgumentException('non-nullable name cannot be null');
}

$this->container['name'] = $name;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ class AdditionalPropertiesArray implements ModelInterface, ArrayAccess
'name' => null
];

/**
* Array of nullable properties. Used for (de)serialization
*
* @var boolean[]
*/
protected static $openAPINullables = [
'name' => false
];

/**
* If a nullable field gets set to null, insert it here
*
* @var boolean[]
*/
protected $openAPINullablesSetToNull = [];

/**
* Array of property to type mappings. Used for (de)serialization
*
Expand All @@ -89,6 +105,60 @@ public static function openAPIFormats()
return self::$openAPIFormats;
}

/**
* Array of property to nullable mappings. Used for (de)serialization
*
* @return array
*/
public static function openAPINullables()
{
return self::$openAPINullables;
}

/**
* Array of nullable field names deliberately set to null
*
* @return array
*/
public function getOpenAPINullablesSetToNull()
{
return $this->openAPINullablesSetToNull;
}

public function setOpenAPINullablesSetToNull($nullablesSetToNull)
{
$this->openAPINullablesSetToNull=$nullablesSetToNull;

return $this;
}

/**
* Checks if a property is nullable
*
* @return bool
*/
public static function isNullable(string $property): bool
{
if (isset(self::$openAPINullables[$property])) {
return self::$openAPINullables[$property];
}

return false;
}

/**
* Checks if a nullable property is set to null.
*
* @return bool
*/
public function isNullableSetToNull(string $property): bool
{
if (in_array($property, $this->getOpenAPINullablesSetToNull())) {
return true;
}
return false;
}

/**
* Array of attributes where the key is the local name,
* and the value is the original name
Expand Down Expand Up @@ -177,7 +247,18 @@ public function getModelName()
*/
public function __construct(array $data = null)
{
$this->container['name'] = isset($data['name']) ? $data['name'] : null;
$this->setIfExists('name', $data, null);
}

public function setIfExists(string $variableName, $fields, $defaultValue)
{
if (is_array($fields) && array_key_exists($variableName, $fields) && is_null($fields[$variableName]) && self::isNullable($variableName)) {
array_push($this->openAPINullablesSetToNull, $variableName);
}

$this->container[$variableName] = isset($fields[$variableName]) ? $fields[$variableName] : $defaultValue;

return $this;
}

/**
Expand Down Expand Up @@ -223,6 +304,12 @@ public function getName()
*/
public function setName($name)
{


if (is_null($name)) {
throw new \InvalidArgumentException('non-nullable name cannot be null');
}

$this->container['name'] = $name;

return $this;
Expand Down
Loading