Skip to content
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

feat: Added new NodeTypeField.php normalizationContext to alter normalization groups per field basis #29

Merged
merged 7 commits into from
Dec 19, 2024
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
meta {
name: Get a Page by its path wrapped in a WebResponse object
name: Get a WebResponse
type: http
seq: 1
}
Expand All @@ -12,19 +12,18 @@ get {

params:query {
path: /
~path: /articles/article-with-attributes
~properties[item][]: nodeReferences
~properties[item][]: title
~properties[]: item
~properties[item]: title
~path: /articles/article-with-attributes
~properties[]: blocks
~properties[item]: url
~properties[item][]: url
~path: /contact
~_preview: 1
~path: /fr/articles/article-with-attributes
~testsd: sd
~path: /fr/articles
~properties[item][]: nodeReferences
~path: /data:image/svg-xml
~_locale: zh
}

auth:bearer {
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"league/flysystem-aws-s3-v3": "^3.10",
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-symfony": "^1.1.8",
"phpunit/phpunit": "^9.5",
Expand Down
2 changes: 2 additions & 0 deletions config/api_resources/web_response.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
resources:
RZ\Roadiz\CoreBundle\Api\Model\WebResponse:
filters:
- app.property_filter
graphQlOperations: { }
operations:
article_get_by_path:
Expand Down
7 changes: 7 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ services:
app.serializer.normalizer.menu_link_path.json:
class: 'App\Serializer\Normalizer\MenuLinkPathNormalizer'
decorates: 'api_platform.serializer.normalizer.item'

app.property_filter:
parent: 'api_platform.serializer.property_filter'
tags: [ 'api_platform.filter' ]
autowire: false
autoconfigure: false
public: false
3 changes: 2 additions & 1 deletion lib/DocGenerator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
}
},
"require-dev": {
"phpstan/phpstan": "^1.5.3"
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2"
},
"extra": {
"branch-alias": {
Expand Down
1 change: 1 addition & 0 deletions lib/Documents/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"doctrine/doctrine-bundle": "^2.8.1",
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpunit/phpunit": "^9.5"
},
Expand Down
3 changes: 2 additions & 1 deletion lib/DtsGenerator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"symfony/http-foundation": "6.4.*"
},
"require-dev": {
"phpstan/phpstan": "^1.5.3"
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2"
},
"license": "MIT",
"authors": [
Expand Down
1 change: 1 addition & 0 deletions lib/EntityGenerator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"require-dev": {
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpunit/phpunit": "^9.5",
"api-platform/core": "~3.3.11"
},
Expand Down
25 changes: 25 additions & 0 deletions lib/EntityGenerator/src/Field/AbstractFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ protected function getFieldDefaultValueDeclaration(): Literal|string|null
return null;
}

/**
* @return array<string, mixed>|null
*/
protected function getNormalizationContext(): ?array
{
if (\method_exists($this->field, 'getNormalizationContext')) {
$normalizationContext = $this->field->getNormalizationContext();
if (\is_array($normalizationContext) && !empty($normalizationContext['groups'])) {
return $normalizationContext;
}
}

return null;
}

protected function addFieldAttributes(Property $property, PhpNamespace $namespace, bool $exclude = false): self
{
if ($exclude) {
Expand Down Expand Up @@ -134,6 +149,16 @@ protected function addFieldAttributes(Property $property, PhpNamespace $namespac
$this->getSerializationMaxDepth(),
]);
}

/*
* Enable different serialization context for this field.
*/
if (null !== $this->getNormalizationContext()) {
$property->addAttribute('Symfony\Component\Serializer\Attribute\Context', [
'normalizationContext' => $this->getNormalizationContext(),
'groups' => $this->getSerializationGroups(),
]);
}
}

if (
Expand Down
18 changes: 18 additions & 0 deletions lib/EntityGenerator/src/Field/CustomFormsFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ protected function addSerializationAttributes(Property|Method $property): self
return $this;
}

protected function addFieldAnnotation(Property $property): AbstractFieldGenerator
{
parent::addFieldAnnotation($property);

$property->addComment('');
$property->addComment('@var '.$this->options['custom_form_class'].'[]|null');

return $this;
}

protected function getNormalizationContext(): array
{
return [
'groups' => ['nodes_sources', 'urls'],
...(parent::getNormalizationContext() ?? []),
];
}

protected function getDefaultSerializationGroups(): array
{
$groups = parent::getDefaultSerializationGroups();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,18 @@ class NSMock extends NodesSources
/**
* Custom forms field.
* (Virtual field, this var is a buffer)
*
* @var \mock\Entity\CustomForm[]|null
*/
#[JMS\Exclude]
#[Serializer\SerializedName(serializedName: 'theForms')]
#[Serializer\Groups(['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'])]
#[ApiProperty(description: 'Custom forms field')]
#[Serializer\MaxDepth(2)]
#[Serializer\Context(
normalizationContext: ['groups' => ['nodes_sources', 'urls']],
groups: ['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'],
)]
private ?array $theForms = null;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,18 @@ class NSMock extends NodesSources
/**
* Custom forms field.
* (Virtual field, this var is a buffer)
*
* @var \mock\Entity\CustomForm[]|null
*/
#[JMS\Exclude]
#[Serializer\SerializedName(serializedName: 'theForms')]
#[Serializer\Groups(['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'])]
#[ApiProperty(description: 'Custom forms field')]
#[Serializer\MaxDepth(2)]
#[Serializer\Context(
normalizationContext: ['groups' => ['nodes_sources', 'urls']],
groups: ['nodes_sources', 'nodes_sources_default', 'nodes_sources_custom_forms'],
)]
private ?array $theForms = null;

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Jwt/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"symfony/http-client": "6.4.*"
},
"require-dev": {
"phpstan/phpstan": "^1.5.3"
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion lib/Markdown/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"twig/twig": "^3.16"
},
"require-dev": {
"phpstan/phpstan": "^1.5.3"
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2"
},
"license": "MIT",
"authors": [
Expand Down
1 change: 1 addition & 0 deletions lib/Models/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"doctrine/doctrine-bundle": "^2.8.1",
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpunit/phpunit": "^9.5"
},
"autoload": {
Expand Down
3 changes: 2 additions & 1 deletion lib/OpenId/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"symfony/security-http": "6.4.*"
},
"require-dev": {
"phpstan/phpstan": "^1.5.3"
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion lib/Random/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"ext-openssl": "*"
},
"require-dev": {
"phpstan/phpstan": "^1.5.3"
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions lib/RoadizCompatBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-symfony": "^1.1.8",
"roadiz/doc-generator": "2.4.*",
Expand Down
1 change: 1 addition & 0 deletions lib/RoadizCoreBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpunit/phpunit": "^9.5",
"symfony/browser-kit": "6.4.*",
Expand Down
29 changes: 29 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20241218095458.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241218095458 extends AbstractMigration
{
public function getDescription(): string
{
return 'Added normalization_context to NodeTypeField';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE node_type_fields ADD normalization_context JSON DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE node_type_fields DROP normalization_context');
}
}
37 changes: 37 additions & 0 deletions lib/RoadizCoreBundle/src/Entity/NodeTypeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ class NodeTypeField extends AbstractField implements NodeTypeFieldInterface, Ser
]
private ?array $serializationGroups = null;

#[
Serializer\Groups(['node_type']),
SymfonySerializer\Groups(['node_type']),
Serializer\Type('array<string, array>'),
ORM\Column(name: 'normalization_context', type: 'json', nullable: true)
]
private ?array $normalizationContext = null;

#[
Serializer\Groups(['node_type']),
SymfonySerializer\Groups(['node_type']),
Expand Down Expand Up @@ -330,4 +338,33 @@ public function setExcludedFromSerialization(bool $excludedFromSerialization): N

return $this;
}

public function getNormalizationContext(): ?array
{
return $this->normalizationContext;
}

public function setNormalizationContext(?array $normalizationContext): NodeTypeField
{
$this->normalizationContext = $normalizationContext;

return $this;
}

#[SymfonySerializer\Ignore]
public function getNormalizationContextGroups(): ?array
{
return $this->normalizationContext['groups'] ?? [];
}

#[SymfonySerializer\Ignore]
public function setNormalizationContextGroups(?array $normalizationContextGroups): NodeTypeField
{
if (null === $normalizationContextGroups) {
$this->normalizationContext = null;
}
$this->normalizationContext['groups'] = $normalizationContextGroups;

return $this;
}
}
1 change: 1 addition & 0 deletions lib/RoadizFontBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpunit/phpunit": "^9.5",
"roadiz/compat-bundle": "2.4.*",
Expand Down
1 change: 1 addition & 0 deletions lib/RoadizRozierBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpstan/phpstan-symfony": "^1.1.8"
},
Expand Down
1 change: 1 addition & 0 deletions lib/RoadizTwoFactorBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"phpunit/phpunit": "^9.5",
"roadiz/doc-generator": "2.4.*",
Expand Down
1 change: 1 addition & 0 deletions lib/RoadizUserBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"roadiz/doc-generator": "2.4.*",
"roadiz/entity-generator": "2.4.*",
Expand Down
1 change: 1 addition & 0 deletions lib/Rozier/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"require-dev": {
"php-coveralls/php-coveralls": "^2.4",
"phpstan/phpstan": "^1.5.3",
"phpstan/phpdoc-parser": "<2",
"phpstan/phpstan-doctrine": "^1.3",
"roadiz/entity-generator": "2.4.*",
"roadiz/jwt": "2.4.*",
Expand Down
4 changes: 2 additions & 2 deletions lib/Rozier/src/AjaxControllers/AjaxSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public function searchAction(Request $request): Response
foreach ($nodesSources as $source) {
$uniqueKey = null;
if ($source instanceof NodesSources) {
$uniqueKey = 'n_' . $source->getNode()->getId();
$uniqueKey = 'n_'.$source->getNode()->getId();
if (!$this->security->isGranted(NodeVoter::READ, $source)) {
continue;
}
} elseif ($source instanceof PersistableInterface) {
$uniqueKey = 'p_' . $source->getId();
$uniqueKey = 'p_'.$source->getId();
}
if (key_exists($uniqueKey, $data)) {
continue;
Expand Down
Loading
Loading