Skip to content

Commit 37c556b

Browse files
fix: combine StreamGeneratorResponse with StreamTraversableResponse
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent 2bf6d6a commit 37c556b

9 files changed

Lines changed: 42 additions & 91 deletions

File tree

apps/dav/lib/Controller/CalendarExportController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1616
use OCP\AppFramework\Http\Attribute\UserRateLimit;
1717
use OCP\AppFramework\Http\DataResponse;
18-
use OCP\AppFramework\Http\StreamGeneratorResponse;
18+
use OCP\AppFramework\Http\StreamTraversableResponse;
1919
use OCP\AppFramework\OCSController;
2020
use OCP\Calendar\CalendarExportOptions;
2121
use OCP\Calendar\ICalendarExport;
@@ -46,7 +46,7 @@ public function __construct(
4646
* @param array{rangeStart:string,rangeCount:positive-int} $options configuration options
4747
* @param string|null $user system user id
4848
*
49-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'text/calendar; charset=UTF-8'|'application/calendar+json; charset=UTF-8'|'application/calendar+xml; charset=UTF-8'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
49+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'text/calendar; charset=UTF-8'|'application/calendar+json; charset=UTF-8'|'application/calendar+xml; charset=UTF-8'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5050
*
5151
* 200: data in requested format
5252
* 400: invalid parameters
@@ -99,7 +99,7 @@ public function export(string $target, ?string $type = null, ?array $options = n
9999
'xcal' => 'application/calendar+xml; charset=UTF-8',
100100
default => 'text/calendar; charset=UTF-8'
101101
};
102-
$response = new StreamGeneratorResponse($this->exportService->export($calendar, $options), $contentType, Http::STATUS_OK);
102+
$response = new StreamTraversableResponse($this->exportService->export($calendar, $options), Http::STATUS_OK, ['Content-Type' => $contentType]);
103103
$response->cacheFor(0);
104104

105105
return $response;

apps/dav/lib/Controller/CalendarImportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2121
use OCP\AppFramework\Http\Attribute\UserRateLimit;
2222
use OCP\AppFramework\Http\DataResponse;
23-
use OCP\AppFramework\Http\StreamGeneratorResponse;
23+
use OCP\AppFramework\Http\StreamTraversableResponse;
2424
use OCP\AppFramework\OCSController;
2525
use OCP\Calendar\CalendarImportOptions;
2626
use OCP\Calendar\IManager;
@@ -53,7 +53,7 @@ public function __construct(
5353
* @param string $data calendar data
5454
* @param string|null $user system user id
5555
*
56-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
56+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5757
*
5858
* 200: NDJSON stream of import event objects
5959
* 400: invalid parameters
@@ -62,7 +62,7 @@ public function __construct(
6262
#[ApiRoute(verb: 'POST', url: '/import', root: '/calendar')]
6363
#[UserRateLimit(limit: 10, period: 3600)]
6464
#[NoAdminRequired]
65-
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamGeneratorResponse {
65+
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamTraversableResponse {
6666
$calendarId = $target;
6767
$format = isset($options['format']) ? $options['format'] : null;
6868
$validation = isset($options['validation']) ? (int)$options['validation'] : null;
@@ -144,6 +144,6 @@ public function import(string $transaction, string $target, array $options, stri
144144
}
145145
})();
146146

147-
return new StreamGeneratorResponse($stream, 'application/x-ndjson');
147+
return new StreamTraversableResponse($stream, Http::STATUS_OK, ['Content-Type' => 'application/x-ndjson']);
148148
}
149149
}

apps/dav/lib/Controller/ContactsImportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2020
use OCP\AppFramework\Http\Attribute\UserRateLimit;
2121
use OCP\AppFramework\Http\DataResponse;
22-
use OCP\AppFramework\Http\StreamGeneratorResponse;
22+
use OCP\AppFramework\Http\StreamTraversableResponse;
2323
use OCP\AppFramework\OCSController;
2424
use OCP\Contacts\ContactsImportOptions;
2525
use OCP\Contacts\IManager;
@@ -53,7 +53,7 @@ public function __construct(
5353
* @param string $data contacts data
5454
* @param string|null $user system user id
5555
*
56-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
56+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5757
*
5858
* 200: NDJSON stream of import event objects
5959
* 400: invalid parameters
@@ -62,7 +62,7 @@ public function __construct(
6262
#[ApiRoute(verb: 'POST', url: '/import', root: '/contacts')]
6363
#[UserRateLimit(limit: 10, period: 3600)]
6464
#[NoAdminRequired]
65-
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamGeneratorResponse {
65+
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamTraversableResponse {
6666
$addressBookId = $target;
6767
$format = isset($options['format']) ? $options['format'] : null;
6868
$validation = isset($options['validation']) ? (int)$options['validation'] : null;
@@ -141,6 +141,6 @@ public function import(string $transaction, string $target, array $options, stri
141141
}
142142
})();
143143

144-
return new StreamGeneratorResponse($stream, 'application/x-ndjson');
144+
return new StreamTraversableResponse($stream, Http::STATUS_OK, ['Content-Type' => 'application/x-ndjson']);
145145
}
146146
}

apps/dav/openapi.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,17 +1660,20 @@
16601660
"content": {
16611661
"text/calendar; charset=UTF-8": {
16621662
"schema": {
1663-
"anyOf": []
1663+
"type": "string",
1664+
"format": "binary"
16641665
}
16651666
},
16661667
"application/calendar+json; charset=UTF-8": {
16671668
"schema": {
1668-
"anyOf": []
1669+
"type": "string",
1670+
"format": "binary"
16691671
}
16701672
},
16711673
"application/calendar+xml; charset=UTF-8": {
16721674
"schema": {
1673-
"anyOf": []
1675+
"type": "string",
1676+
"format": "binary"
16741677
}
16751678
}
16761679
}
@@ -1885,7 +1888,8 @@
18851888
"content": {
18861889
"application/x-ndjson": {
18871890
"schema": {
1888-
"anyOf": []
1891+
"type": "string",
1892+
"format": "binary"
18891893
}
18901894
}
18911895
}
@@ -2088,7 +2092,8 @@
20882092
"content": {
20892093
"application/x-ndjson": {
20902094
"schema": {
2091-
"anyOf": []
2095+
"type": "string",
2096+
"format": "binary"
20922097
}
20932098
}
20942099
}

lib/composer/composer/autoload_classmap.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@
127127
'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => $baseDir . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
128128
'OCP\\AppFramework\\Http\\Response' => $baseDir . '/lib/public/AppFramework/Http/Response.php',
129129
'OCP\\AppFramework\\Http\\StandaloneTemplateResponse' => $baseDir . '/lib/public/AppFramework/Http/StandaloneTemplateResponse.php',
130-
'OCP\\AppFramework\\Http\\StreamGeneratorResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamGeneratorResponse.php',
131130
'OCP\\AppFramework\\Http\\StreamResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamResponse.php',
132131
'OCP\\AppFramework\\Http\\StreamTraversableResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamTraversableResponse.php',
133132
'OCP\\AppFramework\\Http\\TemplateResponse' => $baseDir . '/lib/public/AppFramework/Http/TemplateResponse.php',

lib/composer/composer/autoload_static.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
168168
'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
169169
'OCP\\AppFramework\\Http\\Response' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/Response.php',
170170
'OCP\\AppFramework\\Http\\StandaloneTemplateResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StandaloneTemplateResponse.php',
171-
'OCP\\AppFramework\\Http\\StreamGeneratorResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamGeneratorResponse.php',
172171
'OCP\\AppFramework\\Http\\StreamResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamResponse.php',
173172
'OCP\\AppFramework\\Http\\StreamTraversableResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamTraversableResponse.php',
174173
'OCP\\AppFramework\\Http\\TemplateResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/TemplateResponse.php',

lib/public/AppFramework/Http/StreamGeneratorResponse.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

openapi.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20529,17 +20529,20 @@
2052920529
"content": {
2053020530
"text/calendar; charset=UTF-8": {
2053120531
"schema": {
20532-
"anyOf": []
20532+
"type": "string",
20533+
"format": "binary"
2053320534
}
2053420535
},
2053520536
"application/calendar+json; charset=UTF-8": {
2053620537
"schema": {
20537-
"anyOf": []
20538+
"type": "string",
20539+
"format": "binary"
2053820540
}
2053920541
},
2054020542
"application/calendar+xml; charset=UTF-8": {
2054120543
"schema": {
20542-
"anyOf": []
20544+
"type": "string",
20545+
"format": "binary"
2054320546
}
2054420547
}
2054520548
}
@@ -20754,7 +20757,8 @@
2075420757
"content": {
2075520758
"application/x-ndjson": {
2075620759
"schema": {
20757-
"anyOf": []
20760+
"type": "string",
20761+
"format": "binary"
2075820762
}
2075920763
}
2076020764
}
@@ -20957,7 +20961,8 @@
2095720961
"content": {
2095820962
"application/x-ndjson": {
2095920963
"schema": {
20960-
"anyOf": []
20964+
"type": "string",
20965+
"format": "binary"
2096120966
}
2096220967
}
2096320968
}

tests/lib/AppFramework/Http/StreamGeneratorResponseTest.php renamed to tests/lib/AppFramework/Http/StreamTraversableResponseTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
namespace Test\AppFramework\Http;
1010

11-
use OCP\AppFramework\Http\StreamGeneratorResponse;
11+
use OCP\AppFramework\Http\IOutput;
12+
use OCP\AppFramework\Http\StreamTraversableResponse;
1213

13-
class StreamGeneratorResponseTest extends \Test\TestCase {
14+
class StreamTraversableResponseTest extends \Test\TestCase {
1415

1516
protected function setUp(): void {
1617
parent::setUp();
@@ -21,7 +22,7 @@ public function testConstructor() {
2122
yield 'chunk1';
2223
yield 'chunk2';
2324
};
24-
$response = new StreamGeneratorResponse($generator(), 'text/plain');
25+
$response = new StreamTraversableResponse($generator(), 200, ['Content-Type' => 'text/plain']);
2526

2627
$headers = $response->getHeaders();
2728
$this->assertEquals('text/plain', $headers['Content-Type']);
@@ -36,8 +37,13 @@ public function testCallback() {
3637
$count++;
3738
yield 'chunk2';
3839
};
39-
$response = new StreamGeneratorResponse($generator(), 'text/plain');
40-
$output = $this->createMock(\OCP\AppFramework\Http\IOutput::class);
40+
$response = new StreamTraversableResponse($generator(), 200, ['Content-Type' => 'text/plain']);
41+
$output = $this->createMock(IOutput::class);
42+
$output->expects($this->exactly(2))
43+
->method('setOutput')
44+
->with($this->callback(function ($chunk) {
45+
return in_array($chunk, ['chunk1', 'chunk2'], true);
46+
}));
4147

4248
$response->callback($output);
4349
$this->assertEquals($count, 2);

0 commit comments

Comments
 (0)