-
-
Save Chris53897/24275929277bef3be7cd447865a4ac90 to your computer and use it in GitHub Desktop.
API-Platform: Set skip_null_values for all entites for apip 2.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* problem: skip_null_values can not be set for all entities in config. API-Platform 3 has this as default | |
* | |
* solution: decorate api_platform.serializer.context_builder and set skip_null_values to false for all resources in one place: | |
*/ | |
class AppContextBuilder implements SerializerContextBuilderInterface | |
{ | |
private $decorated; | |
public function __construct(SerializerContextBuilderInterface $decorated) | |
{ | |
$this->decorated = $decorated; | |
} | |
public function createFromRequest(Request $request, bool $normalization, ?array $extractedAttributes = null): array | |
{ | |
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes); | |
$context['skip_null_values'] = false; | |
return $context; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment