Skip to content

Commit b935959

Browse files
authored
Merge pull request #68 from 5am-code/65-rollup-with-empty-select-fix
65 rollup with empty select fix
2 parents 21e07d4 + 9aa463a commit b935959

File tree

3 files changed

+190
-4
lines changed

3 files changed

+190
-4
lines changed

src/Entities/Properties/Rollup.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function fillFromRaw(): void
3737
break;
3838
default:
3939
throw new HandlingException("Unexpected rollupType {$this->rollupType}");
40-
}
40+
}
4141
}
4242
}
4343

@@ -89,12 +89,21 @@ private function setRollupContentArray()
8989
// TODO
9090
$rollupPropertyItem['id'] = 'undefined';
9191

92-
$this->content->add(
93-
Property::fromResponse('', $rollupPropertyItem)
94-
);
92+
if ($this->isRollupPropertyContentSet($rollupPropertyItem)) {
93+
$this->content->add(
94+
Property::fromResponse('', $rollupPropertyItem)
95+
);
96+
}
9597
}
9698
}
9799

100+
private function isRollupPropertyContentSet($rollupPropertyItem): bool
101+
{
102+
return Arr::exists($rollupPropertyItem, 'type')
103+
&& Arr::exists($rollupPropertyItem, $rollupPropertyItem['type'])
104+
&& ! is_null($rollupPropertyItem[$rollupPropertyItem['type']]);
105+
}
106+
98107
private function setRollupContentDate()
99108
{
100109
$this->content = new RichDate();

tests/EndpointDatabaseTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,33 @@ public function it_throws_a_notion_exception_bad_request()
157157

158158
Notion::database('8284f3ff77e24d4a939d19459e4d6bdc')->query();
159159
}
160+
161+
/**
162+
* @test
163+
* ! edge-case
164+
*/
165+
public function it_queries_a_database_with_a_rollup_property_with_empty_selects()
166+
{
167+
// success /v1/databases/DATABASE_DOES_EXIST/query
168+
Http::fake([
169+
'https://api.notion.com/v1/databases/11971214ce574df7a58389c1deda61d7/query*' => Http::response(
170+
json_decode(file_get_contents('tests/stubs/endpoints/databases/response_query_rollup_empty_select_200.json'), true),
171+
200,
172+
['Headers']
173+
),
174+
]);
175+
176+
$result = Notion::database('11971214ce574df7a58389c1deda61d7')->query();
177+
178+
$this->assertInstanceOf(PageCollection::class, $result);
179+
180+
$resultCollection = $result->asCollection();
181+
182+
$this->assertIsIterable($resultCollection);
183+
$this->assertContainsOnly(Page::class, $resultCollection);
184+
185+
// check page object
186+
$page = $resultCollection->first();
187+
$this->assertEquals(0, $page->getProperty('Rollup')->getContent()->count());
188+
}
160189
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "page",
6+
"id": "1321e6b6-0771-48bb-9814-6501c2ec3c32",
7+
"created_time": "2022-07-09T10:29:00.000Z",
8+
"last_edited_time": "2022-07-09T10:45:00.000Z",
9+
"created_by": {
10+
"object": "user",
11+
"id": "04536682-601a-4531-a18f-4fa89fdf14a8"
12+
},
13+
"last_edited_by": {
14+
"object": "user",
15+
"id": "04531682-603a-4531-a18f-1fa89fdfb4a8"
16+
},
17+
"cover": null,
18+
"icon": null,
19+
"parent": {
20+
"type": "database_id",
21+
"database_id": "15971224-ce57-4df1-a583-89c1deca63d7"
22+
},
23+
"archived": false,
24+
"properties": {
25+
"Test Rollup Problem": {
26+
"id": "G|zT",
27+
"type": "relation",
28+
"relation": [
29+
{
30+
"id": "dcad104b-222c-4d63-b83e-852f22612f4a"
31+
},
32+
{
33+
"id": "3611ce31-ae52-45dc-bc5a-10626ca19ab5"
34+
}
35+
]
36+
},
37+
"Rollup": {
38+
"id": "JCh`",
39+
"type": "rollup",
40+
"rollup": {
41+
"type": "array",
42+
"array": [
43+
{
44+
"type": "select",
45+
"select": null
46+
}
47+
],
48+
"function": "show_original"
49+
}
50+
},
51+
"Name": {
52+
"id": "title",
53+
"type": "title",
54+
"title": []
55+
}
56+
},
57+
"url": "https:\/\/www.notion.so\/132de616077648bb98146501c2ec3c32"
58+
},
59+
{
60+
"object": "page",
61+
"id": "43dd6b90-6bde-48ea-9f06-79fee96de99c",
62+
"created_time": "2022-07-09T10:29:00.000Z",
63+
"last_edited_time": "2022-07-09T10:29:00.000Z",
64+
"created_by": {
65+
"object": "user",
66+
"id": "04d36682-603a-4531-a18f-4fa19fdfb4a8"
67+
},
68+
"last_edited_by": {
69+
"object": "user",
70+
"id": "04d36682-603a-4531-a18f-4fa891dfb4a8"
71+
},
72+
"cover": null,
73+
"icon": null,
74+
"parent": {
75+
"type": "database_id",
76+
"database_id": "1d9712d4-ce57-4df7-a583-89c1dedac3d7"
77+
},
78+
"archived": false,
79+
"properties": {
80+
"Test Rollup Problem": {
81+
"id": "G|zT",
82+
"type": "relation",
83+
"relation": []
84+
},
85+
"Rollup": {
86+
"id": "JCh`",
87+
"type": "rollup",
88+
"rollup": {
89+
"type": "array",
90+
"array": [],
91+
"function": "show_original"
92+
}
93+
},
94+
"Name": {
95+
"id": "title",
96+
"type": "title",
97+
"title": []
98+
}
99+
},
100+
"url": "https:\/\/www.notion.so\/430d6b9d6b9e48ea9f067cfee96de9d9"
101+
},
102+
{
103+
"object": "page",
104+
"id": "788c67fe-84d2-4cf8-aab6-6cd6448d98b2",
105+
"created_time": "2022-07-09T10:29:00.000Z",
106+
"last_edited_time": "2022-07-09T10:29:00.000Z",
107+
"created_by": {
108+
"object": "user",
109+
"id": "04536682-613a-4531-a18f-4fd89fdfb4a8"
110+
},
111+
"last_edited_by": {
112+
"object": "user",
113+
"id": "04436622-603a-4531-a18f-4fa89fdfb4a8"
114+
},
115+
"cover": null,
116+
"icon": null,
117+
"parent": {
118+
"type": "database_id",
119+
"database_id": "15972224-ce57-4df7-a583-89c1de1a63dd"
120+
},
121+
"archived": false,
122+
"properties": {
123+
"Test Rollup Problem": {
124+
"id": "G|zT",
125+
"type": "relation",
126+
"relation": []
127+
},
128+
"Rollup": {
129+
"id": "JCh`",
130+
"type": "rollup",
131+
"rollup": {
132+
"type": "array",
133+
"array": [],
134+
"function": "show_original"
135+
}
136+
},
137+
"Name": {
138+
"id": "title",
139+
"type": "title",
140+
"title": []
141+
}
142+
},
143+
"url": "https:\/\/www.notion.so\/788c67de84d24cf8dab660d64c8998b2"
144+
}
145+
],
146+
"next_cursor": null,
147+
"has_more": false
148+
}

0 commit comments

Comments
 (0)