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/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; } 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; + } +}