8000 [Serializer] Add flag to require all properties to be listed in the input by christian-kolb · Pull Request #49553 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add flag to require all properties to be listed in the input #49553

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Add flag to require all properties to be listed in the i…
…nput
  • Loading branch information
Christian Kolb authored and nicolas-grekas committed May 5, 2023
commit d62410aa7acd43f80b75a685534a6aef777745a1
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.3
---

* Add `AbstractNormalizer::REQUIRE_ALL_PROPERTIES` context flag to require all properties to be listed in the input instead of falling back to null for nullable ones
* Add `XmlEncoder::SAVE_OPTIONS` context option
* Add `BackedEnumNormalizer::ALLOW_INVALID_VALUES` context option
* Add `UnsupportedFormatException` which is thrown when there is no decoder for a given format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,13 @@ public function withIgnoredAttributes(?array $ignoredAttributes): static
{
return $this->with(AbstractNormalizer::IGNORED_ATTRIBUTES, $ignoredAttributes);
}

/**
* Configures requiring all properties to be listed in the input instead
* of falling back to null for nullable ones.
*/
public function withRequireAllProperties(?bool $requireAllProperties = true): static
{
return $this->with(AbstractNormalizer::REQUIRE_ALL_PROPERTIES, $requireAllProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
*/
public const IGNORED_ATTRIBUTES = 'ignored_attributes';

/**
* Require all properties to be listed in the input instead of falling
* back to null for nullable ones.
*/
public const REQUIRE_ALL_PROPERTIES = 'require_all_properties';

/**
* @internal
*/
Expand Down Expand Up @@ -383,7 +389,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
$params[] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
} elseif ($constructorParameter->isDefaultValueAvailable()) {
$params[] = $constructorParameter->getDefaultValue();
} elseif ($constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
} elseif (!($context[self::REQUIRE_ALL_PROPERTIES] ?? $this->defaultContext[self::REQUIRE_ALL_PROPERTIES] ?? false) && $constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
$params[] = null;
} else {
if (!isset($context['not_normalizable_value_exceptions'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function testWithers(array $values)
->withCallbacks($values[AbstractNormalizer::CALLBACKS])
->withCircularReferenceHandler($values[AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER])
->withIgnoredAttributes($values[AbstractNormalizer::IGNORED_ATTRIBUTES])
->withRequireAllProperties($values[AbstractNormalizer::REQUIRE_ALL_PROPERTIES])
->toArray();

$this->assertEquals($values, $context);
Expand All @@ -65,6 +66,7 @@ public static function withersDataProvider(): iterable
AbstractNormalizer::CALLBACKS => [static function (): void {}],
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => static function (): void {},
AbstractNormalizer::IGNORED_ATTRIBUTES => ['attribute3'],
AbstractNormalizer::REQUIRE_ALL_PROPERTIES => true,
]];

yield 'With null values' => [[
Expand All @@ -77,6 +79,7 @@ public static function withersDataProvider(): iterable
AbstractNormalizer::CALLBACKS => null,
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => null,
AbstractNormalizer::IGNORED_ATTRIBUTES => null,
AbstractNormalizer::REQUIRE_ALL_PROPERTIES => null,
]];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
Expand Down Expand Up @@ -166,6 +167,15 @@ public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInput
$this->assertNull($dummy->getFoo());
}

public function testObjectWithNullableNonOptionalConstructorArgumentWithoutInputAndRequireAllProperties()
{
$normalizer = new ObjectNormalizer();

$this->expectException(MissingConstructorArgumentsException::class);

$normalizer->denormalize([], NullableConstructorArgumentDummy::class, null, [AbstractNormalizer::REQUIRE_ALL_PROPERTIES => true]);
}

/**
* @dataProvider getNormalizer
*/
Expand Down
0