From d617f7bd19a51128613bcef57279dcb42bff8a02 Mon Sep 17 00:00:00 2001 From: William Arslett Date: Fri, 18 Oct 2019 18:17:27 +0100 Subject: [PATCH 1/2] Created tests to isolate issue --- behat.yml.dist | 1 + features/bootstrap/XmlContext.php | 8 ++ features/xml/input_output.feature | 73 +++++++++++++++++++ .../TestBundle/Entity/ResourceWithBoolean.php | 51 +++++++++++++ .../TestBundle/Entity/ResourceWithFloat.php | 51 +++++++++++++ .../TestBundle/Entity/ResourceWithInteger.php | 51 +++++++++++++ .../TestBundle/Entity/ResourceWithString.php | 51 +++++++++++++ 7 files changed, 286 insertions(+) create mode 100644 features/bootstrap/XmlContext.php create mode 100644 features/xml/input_output.feature create mode 100644 tests/Fixtures/TestBundle/Entity/ResourceWithBoolean.php create mode 100644 tests/Fixtures/TestBundle/Entity/ResourceWithFloat.php create mode 100644 tests/Fixtures/TestBundle/Entity/ResourceWithInteger.php create mode 100644 tests/Fixtures/TestBundle/Entity/ResourceWithString.php diff --git a/behat.yml.dist b/behat.yml.dist index 460251a8e0c..dea427ca410 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -9,6 +9,7 @@ default: - 'HttpHeaderContext' - 'GraphqlContext' - 'JsonContext' + - 'XmlContext' - 'HydraContext' - 'SwaggerContext' - 'HttpCacheContext' diff --git a/features/bootstrap/XmlContext.php b/features/bootstrap/XmlContext.php new file mode 100644 index 00000000000..10aea327e82 --- /dev/null +++ b/features/bootstrap/XmlContext.php @@ -0,0 +1,8 @@ + + + string + + """ + Then the response status code should be 201 + And the response should be in XML + And the header "Content-Type" should be equal to "application/xml; charset=utf-8" + + @createSchema + Scenario Outline: Posting an XML resource with a boolean value + When I send a "POST" request to "/resource_with_booleans" with body: + """ + + + + + """ + Then the response status code should be 201 + And the response should be in XML + And the header "Content-Type" should be equal to "application/xml; charset=utf-8" + Examples: + | value | + | true | + | false | + | True | + | False | + | t | + | f | + | T | + | F | + | 1 | + | 0 | + + @createSchema + Scenario: Posting an XML resource with an integer value + When I send a "POST" request to "/resource_with_integers" with body: + """ + + + 42 + + """ + Then the response status code should be 201 + And the response should be in XML + And the header "Content-Type" should be equal to "application/xml; charset=utf-8" + + + @createSchema + Scenario: Posting an XML resource with an float value + When I send a "POST" request to "/resource_with_floats" with body: + """ + + + 3.14 + + """ + Then the response status code should be 201 + And the response should be in XML + And the header "Content-Type" should be equal to "application/xml; charset=utf-8" diff --git a/tests/Fixtures/TestBundle/Entity/ResourceWithBoolean.php b/tests/Fixtures/TestBundle/Entity/ResourceWithBoolean.php new file mode 100644 index 00000000000..095427d4b5d --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/ResourceWithBoolean.php @@ -0,0 +1,51 @@ +id; + } + + /** + * @return bool + */ + public function getMyBooleanField(): bool + { + return $this->myBooleanField; + } + + /** + * @param bool $myBooleanField + */ + public function setMyBooleanField(bool $myBooleanField): void + { + $this->myBooleanField = $myBooleanField; + } +} diff --git a/tests/Fixtures/TestBundle/Entity/ResourceWithFloat.php b/tests/Fixtures/TestBundle/Entity/ResourceWithFloat.php new file mode 100644 index 00000000000..812d937eda8 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/ResourceWithFloat.php @@ -0,0 +1,51 @@ +id; + } + + /** + * @return float + */ + public function getMyFloatField(): float + { + return $this->myFloatField; + } + + /** + * @param float $myFloatField + */ + public function setMyFloatField(float $myFloatField): void + { + $this->myFloatField = $myFloatField; + } +} diff --git a/tests/Fixtures/TestBundle/Entity/ResourceWithInteger.php b/tests/Fixtures/TestBundle/Entity/ResourceWithInteger.php new file mode 100644 index 00000000000..312388f8336 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/ResourceWithInteger.php @@ -0,0 +1,51 @@ +id; + } + + /** + * @return int + */ + public function getMyIntegerField(): int + { + return $this->myIntegerField; + } + + /** + * @param int $myIntegerField + */ + public function setMyIntegerField(int $myIntegerField): void + { + $this->myIntegerField = $myIntegerField; + } +} diff --git a/tests/Fixtures/TestBundle/Entity/ResourceWithString.php b/tests/Fixtures/TestBundle/Entity/ResourceWithString.php new file mode 100644 index 00000000000..50323eaca3b --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/ResourceWithString.php @@ -0,0 +1,51 @@ +id; + } + + /** + * @return string + */ + public function getMyStringField(): string + { + return $this->myStringField; + } + + /** + * @param string $myStringField + */ + public function setMyStringField(string $myStringField): void + { + $this->myStringField = $myStringField; + } +} From ff18160792e9b99ac8db878bdf91bcd1f0d96585 Mon Sep 17 00:00:00 2001 From: William Arslett Date: Fri, 18 Oct 2019 18:27:56 +0100 Subject: [PATCH 2/2] Made test pass --- features/xml/input_output.feature | 1 - src/Serializer/AbstractItemNormalizer.php | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/features/xml/input_output.feature b/features/xml/input_output.feature index 762af8a55b1..f3a7f9051fc 100644 --- a/features/xml/input_output.feature +++ b/features/xml/input_output.feature @@ -58,7 +58,6 @@ Feature: XML Input and Output And the response should be in XML And the header "Content-Type" should be equal to "application/xml; charset=utf-8" - @createSchema Scenario: Posting an XML resource with an float value When I send a "POST" request to "/resource_with_floats" with body: diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index 7c067184f88..ff077c9af0d 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -701,6 +701,29 @@ private function createAttributeValue($attribute, $value, $format = null, array return $this->serializer->denormalize($value, $className, $format, $context); } + if (is_string($value) && 'string' !== $type->getBuiltinType()) { + + if ('bool' === $type->getBuiltinType()) { + if (in_array($value, ['true', 'True', 'T', 't', "1"], true)) { + $value = true; + } + if (in_array($value, ['false', 'False', 'F', 'f', "0"], true)) { + $value = false; + } + } + + if (is_numeric($value)) { + + if ('int' === $type->getBuiltinType()) { + $value = intval($value); + } + + if ('float' === $type->getBuiltinType()) { + $value = floatval($value); + } + } + } + if ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false) { return $value; }