feature(PHP): include the current value with enum failure (#7993)

When the value is not in the enum, the exception will now
include the actual value, helping spec designers.
This commit is contained in:
Dalibor Karlović 2020-11-30 14:49:52 +01:00 committed by GitHub
parent a8fbcb0db2
commit adcf9ffa20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 19 deletions

View File

@ -197,7 +197,8 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
$allowedValues = $this->{{getter}}AllowableValues();
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for '{{name}}', must be one of '%s'",
"invalid value '%s' for '{{name}}', must be one of '%s'",
$this->container['{{name}}'],
implode("', '", $allowedValues)
);
}
@ -290,7 +291,8 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for '{{name}}', must be one of '%s'",
"Invalid value '%s' for '{{name}}', must be one of '%s'",
${{{name}}},
implode("', '", $allowedValues)
)
);

View File

@ -233,7 +233,8 @@ class EnumArrays implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getJustSymbolAllowableValues();
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'just_symbol', must be one of '%s'",
"invalid value '%s' for 'just_symbol', must be one of '%s'",
$this->container['just_symbol'],
implode("', '", $allowedValues)
);
}
@ -276,7 +277,8 @@ class EnumArrays implements ModelInterface, ArrayAccess, \JsonSerializable
if (!is_null($just_symbol) && !in_array($just_symbol, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'just_symbol', must be one of '%s'",
"Invalid value '%s' for 'just_symbol', must be one of '%s'",
$just_symbol,
implode("', '", $allowedValues)
)
);

View File

@ -303,7 +303,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getEnumStringAllowableValues();
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'enum_string', must be one of '%s'",
"invalid value '%s' for 'enum_string', must be one of '%s'",
$this->container['enum_string'],
implode("', '", $allowedValues)
);
}
@ -314,7 +315,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getEnumStringRequiredAllowableValues();
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'enum_string_required', must be one of '%s'",
"invalid value '%s' for 'enum_string_required', must be one of '%s'",
$this->container['enum_string_required'],
implode("', '", $allowedValues)
);
}
@ -322,7 +324,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getEnumIntegerAllowableValues();
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'enum_integer', must be one of '%s'",
"invalid value '%s' for 'enum_integer', must be one of '%s'",
$this->container['enum_integer'],
implode("', '", $allowedValues)
);
}
@ -330,7 +333,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getEnumNumberAllowableValues();
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'enum_number', must be one of '%s'",
"invalid value '%s' for 'enum_number', must be one of '%s'",
$this->container['enum_number'],
implode("', '", $allowedValues)
);
}
@ -373,7 +377,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
if (!is_null($enum_string) && !in_array($enum_string, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'enum_string', must be one of '%s'",
"Invalid value '%s' for 'enum_string', must be one of '%s'",
$enum_string,
implode("', '", $allowedValues)
)
);
@ -406,7 +411,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
if (!in_array($enum_string_required, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'enum_string_required', must be one of '%s'",
"Invalid value '%s' for 'enum_string_required', must be one of '%s'",
$enum_string_required,
implode("', '", $allowedValues)
)
);
@ -439,7 +445,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
if (!is_null($enum_integer) && !in_array($enum_integer, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'enum_integer', must be one of '%s'",
"Invalid value '%s' for 'enum_integer', must be one of '%s'",
$enum_integer,
implode("', '", $allowedValues)
)
);
@ -472,7 +479,8 @@ class EnumTest implements ModelInterface, ArrayAccess, \JsonSerializable
if (!is_null($enum_number) && !in_array($enum_number, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'enum_number', must be one of '%s'",
"Invalid value '%s' for 'enum_number', must be one of '%s'",
$enum_number,
implode("', '", $allowedValues)
)
);

View File

@ -235,7 +235,8 @@ class InlineObject2 implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getEnumFormStringAllowableValues();
if (!is_null($this->container['enum_form_string']) && !in_array($this->container['enum_form_string'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'enum_form_string', must be one of '%s'",
"invalid value '%s' for 'enum_form_string', must be one of '%s'",
$this->container['enum_form_string'],
implode("', '", $allowedValues)
);
}
@ -311,7 +312,8 @@ class InlineObject2 implements ModelInterface, ArrayAccess, \JsonSerializable
if (!is_null($enum_form_string) && !in_array($enum_form_string, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'enum_form_string', must be one of '%s'",
"Invalid value '%s' for 'enum_form_string', must be one of '%s'",
$enum_form_string,
implode("', '", $allowedValues)
)
);

View File

@ -244,7 +244,8 @@ class Order implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getStatusAllowableValues();
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'status', must be one of '%s'",
"invalid value '%s' for 'status', must be one of '%s'",
$this->container['status'],
implode("', '", $allowedValues)
);
}
@ -383,7 +384,8 @@ class Order implements ModelInterface, ArrayAccess, \JsonSerializable
if (!is_null($status) && !in_array($status, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'status', must be one of '%s'",
"Invalid value '%s' for 'status', must be one of '%s'",
$status,
implode("', '", $allowedValues)
)
);

View File

@ -250,7 +250,8 @@ class Pet implements ModelInterface, ArrayAccess, \JsonSerializable
$allowedValues = $this->getStatusAllowableValues();
if (!is_null($this->container['status']) && !in_array($this->container['status'], $allowedValues, true)) {
$invalidProperties[] = sprintf(
"invalid value for 'status', must be one of '%s'",
"invalid value '%s' for 'status', must be one of '%s'",
$this->container['status'],
implode("', '", $allowedValues)
);
}
@ -413,7 +414,8 @@ class Pet implements ModelInterface, ArrayAccess, \JsonSerializable
if (!is_null($status) && !in_array($status, $allowedValues, true)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for 'status', must be one of '%s'",
"Invalid value '%s' for 'status', must be one of '%s'",
$status,
implode("', '", $allowedValues)
)
);

View File

@ -26,7 +26,7 @@ class EnumTestTest extends TestCase
$this->assertFalse($enum->valid());
$expected = [
"invalid value for 'enum_string', must be one of 'UPPER', 'lower', ''",
"invalid value '0' for 'enum_string', must be one of 'UPPER', 'lower', ''",
"'enum_string_required' can't be null",
];
$this->assertSame($expected, $enum->listInvalidProperties());