Skip to content

Commit 75e12f4

Browse files
committed
api: set report_fields_where_declared:true in doctrine.yaml for doctrine/orm 3
And don't overwrite the doctrine definition for the same field again with the same values. Now we define the Constraints on the getter, because api-platform only allows to define constraints on a getter. Move the example on ContentNode::data to the getter, because then this example was used for all children for the data property instead of the example on the getter. Now the comment for the property still overwrites the comments for the getters, but this is ok as the inheritance is an implementation detail, and must not be exposed in the api. Issue: ecamp#3740
1 parent aa72243 commit 75e12f4

File tree

7 files changed

+55
-33
lines changed

7 files changed

+55
-33
lines changed

api/config/packages/doctrine.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ doctrine:
1414
enable_lazy_ghost_objects: true
1515
naming_strategy: App\Util\CamelPascalNamingStrategy
1616
auto_mapping: true
17+
report_fields_where_declared: true
1718
mappings:
1819
App:
1920
is_bundle: false

api/src/Entity/ContentNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ abstract class ContentNode extends BaseEntity implements BelongsToContentNodeTre
9292
/**
9393
* Holds the actual data of the content node.
9494
*/
95-
#[ApiProperty(example: ['text' => 'dummy text'])]
96-
#[Groups(['read', 'write'])]
9795
#[ORM\Column(type: 'json', nullable: true, options: ['jsonb' => true])]
9896
public ?array $data = null;
9997

@@ -173,6 +171,8 @@ public function getRoot(): ?ColumnLayout {
173171
return $this->root;
174172
}
175173

174+
#[ApiProperty(example: ['text' => 'dummy text'])]
175+
#[Groups(['read', 'write'])]
176176
public function getData(): ?array {
177177
return $this->data;
178178
}

api/src/Entity/ContentNode/ColumnLayout.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,7 @@ class ColumnLayout extends ContentNode implements SupportsContentNodeChildren {
7979
],
8080
];
8181

82-
/**
83-
* Holds the actual data of the content node
84-
* (overridden from abstract class in order to add specific validation).
85-
*/
86-
#[ApiProperty(example: ['columns' => [['slot' => '1', 'width' => 12]]])]
87-
#[Groups(['read', 'write'])]
88-
#[ORM\Column(type: 'json', nullable: true, options: ['jsonb' => true])]
89-
#[Assert\Sequentially(constraints: [
90-
new AssertJsonSchema(schema: self::JSON_SCHEMA),
91-
new AssertColumWidthsSumTo12(),
92-
new AssertNoOrphanChildren(),
93-
])]
94-
#[Assert\NotNull]
95-
public ?array $data = ['columns' => [['slot' => '1', 'width' => 6], ['slot' => '2', 'width' => 6]]];
82+
public const DATA_DEFAULT = '{"columns":[{"slot":"1","width":6},{"slot":"2","width":6}]}';
9683

9784
/**
9885
* All content nodes that are part of this content node tree.
@@ -104,6 +91,26 @@ class ColumnLayout extends ContentNode implements SupportsContentNodeChildren {
10491
public function __construct() {
10592
parent::__construct();
10693
$this->rootDescendants = new ArrayCollection();
94+
$this->data = json_decode(self::DATA_DEFAULT, true);
95+
}
96+
97+
/**
98+
* Holds the actual data of the content node
99+
* (overridden from abstract class in order to add specific validation).
100+
*/
101+
#[ApiProperty(
102+
default: self::DATA_DEFAULT,
103+
example: ['columns' => [['slot' => '1', 'width' => 12]]]
104+
)]
105+
#[Groups(['read', 'write'])]
106+
#[Assert\Sequentially(constraints: [
107+
new AssertJsonSchema(schema: self::JSON_SCHEMA),
108+
new AssertColumWidthsSumTo12(),
109+
new AssertNoOrphanChildren(),
110+
])]
111+
#[Assert\NotNull]
112+
public function getData(): ?array {
113+
return parent::getData();
107114
}
108115

109116
/**

api/src/Entity/ContentNode/MaterialNode.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@
5050
)]
5151
#[ORM\Entity(repositoryClass: MaterialNodeRepository::class)]
5252
class MaterialNode extends ContentNode {
53-
/**
54-
* Holds the actual data of the content node
55-
* (overridden from abstract class in order to add specific validation).
56-
*/
57-
#[ApiProperty(example: null)]
58-
#[Groups(['read', 'write'])]
59-
#[ORM\Column(type: 'json', nullable: true, options: ['jsonb' => true])]
60-
#[Assert\IsNull]
61-
public ?array $data = null;
62-
6353
#[ApiProperty(readableLink: true, writableLink: false)]
6454
#[Groups(['read'])]
6555
#[ORM\OneToMany(targetEntity: MaterialItem::class, mappedBy: 'materialNode', orphanRemoval: true, cascade: ['persist', 'remove'])]
@@ -72,6 +62,17 @@ public function __construct() {
7262
parent::__construct();
7363
}
7464

65+
/**
66+
* Holds the actual data of the content node
67+
* (overridden from abstract class in order to add specific validation).
68+
*/
69+
#[ApiProperty(example: null)]
70+
#[Groups(['read', 'write'])]
71+
#[Assert\IsNull]
72+
public function getData(): ?array {
73+
return $this->data;
74+
}
75+
7576
/**
7677
* @return MaterialItem[]
7778
*/

api/src/Entity/ContentNode/MultiSelect.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ class MultiSelect extends ContentNode {
8181
'natureAndEnvironment' => ['checked' => true],
8282
]])]
8383
#[Groups(['read', 'write'])]
84-
#[ORM\Column(type: 'json', nullable: true, options: ['jsonb' => true])]
8584
#[Assert\IsNull(groups: ['create'])] // create with empty data; default value is populated by data persister
8685
#[Assert\NotNull(groups: ['update'])]
8786
#[AssertJsonSchema(schema: self::JSON_SCHEMA, groups: ['update'])]
88-
public ?array $data = null;
87+
public function getData(): ?array {
88+
return $this->data;
89+
}
8990
}

api/src/Entity/ContentNode/SingleText.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,25 @@ class SingleText extends ContentNode {
5858
],
5959
];
6060

61+
public const DATA_DEFAULT = '{"html":""}';
62+
63+
public function __construct() {
64+
parent::__construct();
65+
$this->data = json_decode(self::DATA_DEFAULT, true);
66+
}
67+
6168
/**
6269
* Holds the actual data of the content node
6370
* (overridden from abstract class in order to add specific validation).
6471
*/
65-
#[ApiProperty(example: ['html' => 'my example text'])]
72+
#[ApiProperty(
73+
default: self::DATA_DEFAULT,
74+
example: ['html' => 'my example text']
75+
)]
6676
#[Groups(['read', 'write'])]
67-
#[ORM\Column(type: 'json', nullable: true, options: ['jsonb' => true])]
6877
#[AssertJsonSchema(schema: self::JSON_SCHEMA)]
6978
#[Assert\NotNull]
70-
public ?array $data = ['html' => ''];
79+
public function getData(): ?array {
80+
return $this->data;
81+
}
7182
}

api/src/Entity/ContentNode/Storyboard.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ class Storyboard extends ContentNode {
9999
'column3' => '',
100100
'position' => 0, ], ]])]
101101
#[Groups(['read', 'write'])]
102-
#[ORM\Column(type: 'json', nullable: true, options: ['jsonb' => true])]
103102
#[AssertJsonSchema(schema: self::JSON_SCHEMA)]
104103
#[Assert\NotNull(groups: ['update'])] // if created with empty data, then default value is populated in data persister
105-
public ?array $data = null;
104+
public function getData(): ?array {
105+
return $this->data;
106+
}
106107
}

0 commit comments

Comments
 (0)