From adcf9ffa20123608a6c49958980e2a0ddf0e30a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Mon, 30 Nov 2020 14:49:52 +0100 Subject: [PATCH] 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. --- .../main/resources/php/model_generic.mustache | 6 +++-- .../lib/Model/EnumArrays.php | 6 +++-- .../OpenAPIClient-php/lib/Model/EnumTest.php | 24 ++++++++++++------- .../lib/Model/InlineObject2.php | 6 +++-- .../php/OpenAPIClient-php/lib/Model/Order.php | 6 +++-- .../php/OpenAPIClient-php/lib/Model/Pet.php | 6 +++-- .../OpenAPIClient-php/tests/EnumTestTest.php | 2 +- 7 files changed, 37 insertions(+), 19 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/model_generic.mustache b/modules/openapi-generator/src/main/resources/php/model_generic.mustache index fab865ed614..08c9d0b1821 100644 --- a/modules/openapi-generator/src/main/resources/php/model_generic.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_generic.mustache @@ -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) ) ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php index 2275c05aac1..3276d44f4f2 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumArrays.php @@ -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) ) ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php index 118a0a2ae09..923568034b4 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumTest.php @@ -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) ) ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/InlineObject2.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/InlineObject2.php index e932c0923e5..9b27cdd4f61 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/InlineObject2.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/InlineObject2.php @@ -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) ) ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php index 6fce620c942..01c06d02f61 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Order.php @@ -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) ) ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php index 38f84c84ec5..9b95c480fed 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/Pet.php @@ -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) ) ); diff --git a/samples/client/petstore/php/OpenAPIClient-php/tests/EnumTestTest.php b/samples/client/petstore/php/OpenAPIClient-php/tests/EnumTestTest.php index 08151e35382..e1111d1223c 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/tests/EnumTestTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/tests/EnumTestTest.php @@ -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());