Skip to content

Instantly share code, notes, and snippets.

@Chris53897
Created November 20, 2021 12:35
Show Gist options
  • Save Chris53897/24275929277bef3be7cd447865a4ac90 to your computer and use it in GitHub Desktop.
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
/**
* 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